Package grammar.output

Source Code of grammar.output.Main

package grammar.output;

import grammar.input.refdata.InsufficientReferenceDataException;
import grammar.model.Language;
import grammar.model.verbs.Tense;
import grammar.util.Utilities;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class Main {
  private static final int PADDING = 36;
 
  private static final String HELP_TEXT_HEADER =
    "ROMANCE CONJUGATOR v0.1.1 - COPYLEFT DUNCAN ROBERTS (2008-2009)\n\n"+
    Utilities.pad("SYNTAX", PADDING)+              "ACTION\n"+
    Utilities.pad("test nouns", PADDING)+            "Given a noun, guess its gender/noun class.\n"+
    Utilities.pad("test verbs", PADDING)+            "Given an infinitive, provide a particular inflected form.\n"+
    Utilities.pad("conjugate [the <tense> of] <verb>", PADDING)+"Displays inflections of a given verb.\n";
 
  private static final String HELP_TEXT_FOOTER =
    Utilities.pad("help", PADDING)+                "Displays this text.\n"+
    Utilities.pad("exit", PADDING)+                "Exits the program.";
 
  private static ApplicationPluginManager pluginManager;
 
  private static String getHelpText(Language language) { // Shouldn't be language-specific...
    StringBuilder sb = new StringBuilder(HELP_TEXT_HEADER);
    List<ApplicationPlugin> plugins = pluginManager.getPlugins();
    for (ApplicationPlugin plugin : plugins) {
      sb.append(Utilities.pad(plugin.getSyntax(), 36));
      sb.append(Utilities.pad(plugin.getAction(), 36));
      sb.append('\n');
    }
    sb.append(HELP_TEXT_FOOTER);
    return sb.toString();
  }
 
  public static void main(String[] args) throws IOException {
    System.out.println("What language would you like to practice?");
   
    Language language = null;
    while (language == null) {
      try {
        language = Language.valueOf(Utilities.readLineFromStdin());
      }
      catch (IllegalArgumentException ioe) {
        System.out.println("Language not recognised. Choose from "+
            Utilities.formatForPrinting(Arrays.asList(Language.values()))+".");
        System.out.println("(Accents/diacritics are optional)");
      }
    }
   
    pluginManager = ApplicationPluginManager.getInstance(language);
   
    System.out.println("Type help for commands.");

    Conjugator conj = new Conjugator();
    while (true) {
      try {
        String response = Utilities.readLineFromStdin();
       
        if (response.equalsIgnoreCase("test nouns")) {
          new NounClassTester(language).play();
        }
        else if (response.equalsIgnoreCase("test verbs")) {
          new FormTester(language).play();
        }
        else if (response.startsWith("conjugate") && response.contains("of")) {
          response = response.substring("conjugate the ".length()).replace(" tense", "");
          String tense = response.substring(0, response.indexOf(" of"));
          String infinitive = response.substring(response.lastIndexOf(' ')+1);
 
          Tense t = Tense.valueOf(language, tense);
          conj.conjugate(infinitive, language, false, Arrays.asList(new Tense[] {t}));
        }
        else if (response.startsWith("conjugate")) {
          //System.err.println("Main.main:"+new Date());
          conj.conjugate(response.substring("conjugate ".length()), language, false);
        }
        else if (response.equals("help")) {
          System.out.println(getHelpText(language));
        }
        else if (response.equals("exit")) {
          System.exit(0);
        }
        else {
          boolean matched = false;
          for (ApplicationPlugin plugin : pluginManager.getPlugins()) {
            if (plugin.matches(response)) {
              plugin.execute();
              matched = true;
            }
          }
          if (!matched) {
            System.out.println("Command not understood. Valid command syntax:");
            System.out.println(getHelpText(language));
          }
        }
      }
      catch (InsufficientReferenceDataException irde) {
        System.out.println(irde.getMessage()); // locally defined, so message guaranteed to be sensible
      }
    }
  }
}
TOP

Related Classes of grammar.output.Main

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.