1   package eu.fbk.dh.tint.digimorph;
2   
3   import eu.fbk.utils.core.CommandLine;
4   import org.apache.commons.cli.*;
5   import org.slf4j.LoggerFactory;
6   
7   import java.io.File;
8   import java.util.LinkedList;
9   import java.util.List;
10  import java.util.Scanner;
11  
12  public class Main {
13  
14      private static void printUsage(Options opt) {
15          HelpFormatter formatter = new HelpFormatter();
16          formatter.printHelp(
17                  "echo <word> | java -jar DigiMorph.jar \n       cat <path to file> | java -jar DigiMorph.jar\n\n", opt);
18          System.exit(1);
19      }
20  
21      private static void retrain(File filepath, File outputPath, boolean include_lemma) {
22          DigiMorph.re_train(filepath, outputPath, include_lemma);
23          System.exit(0);
24      }
25  
26      public static void main(String[] args) {
27  
28          try {
29              final eu.fbk.utils.core.CommandLine cmd = eu.fbk.utils.core.CommandLine
30                      .parser()
31                      .withName("run-digimorph")
32                      .withHeader("Run or retrain DigiMorph tool")
33                      .withOption("r", "retrain-input-file", "Input file in Morph-IT for retraining", "FILE",
34                              eu.fbk.utils.core.CommandLine.Type.FILE_EXISTING, true, false, false)
35                      .withOption("w", "retrain-output-file", "Output file for retraining", "FILE",
36                              eu.fbk.utils.core.CommandLine.Type.FILE, true, false, false)
37                      .withOption("l", "lemma", "Include lemma")
38                      .withOption("v", "version", "Print the tool version")
39                      .withLogger(LoggerFactory.getLogger("eu.fbk")).parse(args);
40  
41              if (cmd.hasOption("version")) {
42                  System.out.println(DigiMorph.getVersion());
43                  System.exit(0);
44              }
45  
46              File retrainInputFile = cmd.getOptionValue("retrain-input-file", File.class);
47              File retrainOutputFile = cmd.getOptionValue("retrain-output-file", File.class);
48              boolean lemma = cmd.hasOption("lemma");
49  
50              if (retrainInputFile != null || retrainOutputFile != null) {
51                  if (retrainInputFile == null || retrainOutputFile == null) {
52                      retrain(retrainInputFile, retrainOutputFile, lemma);
53                  } else {
54                      throw new CommandLine.Exception("Input file or output path missing for retrain");
55                  }
56              }
57  
58          } catch (Exception e) {
59              CommandLine.fail(e);
60          }
61  
62          List<String> text = new LinkedList<String>();
63          Scanner scanner = new Scanner(System.in);
64          String line;
65          while (scanner.hasNextLine()) {
66              line = scanner.nextLine();
67              if (line.compareToIgnoreCase("morph()") == 0) {
68                  break;
69              }
70              text.add(line);
71  
72          }
73          DigiMorph dm = new DigiMorph();
74  
75          for (String s : dm.getMorphology(text)) {
76              System.out.println(s);
77          }
78  
79      }
80  }
81