1   package eu.fbk.dh.tint.resources.morpho;
2   
3   
4   import javax.annotation.Nullable;
5   import java.io.*;
6   import java.util.ArrayList;
7   import java.util.Arrays;
8   import java.util.HashSet;
9   
10  /**
11   * Created by alessio on 20/07/15.
12   */
13  
14  public class FstanRunner {
15  
16      String command, model;
17  
18      public FstanRunner(String command, String model) {
19          this.command = command;
20          this.model = model;
21      }
22  
23      public ArrayList<String[]> run(ArrayList<String> requests) {
24  
25          ArrayList<String[]> ret = new ArrayList<>();
26  
27          try {
28  
29              ProcessBuilder pb = new ProcessBuilder(command, model);
30              pb.redirectErrorStream(true);
31  
32              Process process;
33              OutputStream stdin;
34              InputStream stdout;
35              BufferedReader reader;
36              process = pb.start();
37              stdin = process.getOutputStream();
38              stdout = process.getInputStream();
39              reader = new BufferedReader(new InputStreamReader(stdout));
40              String line;
41  
42              BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));
43  
44              for (String request : requests) {
45                  writer.write(request);
46                  writer.newLine();
47              }
48  
49              writer.flush();
50              writer.close();
51  
52              while ((line = reader.readLine()) != null) {
53                  String[] parts = line.trim().split("\\s+");
54                  ret.add(parts);
55              }
56  
57          } catch (Exception e) {
58              e.printStackTrace();
59          }
60  
61          return ret;
62      }
63  
64      public HashSet<String> getTypes(String word) {
65          ArrayList<String> requests = new ArrayList<>();
66          requests.add(word);
67  
68          ArrayList<String[]> strings = new ArrayList<>();
69          strings = run(requests);
70  
71          HashSet<String> types = new HashSet<>();
72          for (String[] parts : strings) {
73              for (String part : parts) {
74                  String[] subparts = part.trim().split("\\+");
75                  if (subparts.length > 1) {
76                      types.add(subparts[1]);
77                  }
78              }
79  
80          }
81  
82          return types;
83      }
84  
85      public HashSet<String[]> getParts(String word) {
86          ArrayList<String> requests = new ArrayList<>();
87          requests.add(word);
88  
89          ArrayList<String[]> strings = new ArrayList<>();
90          strings = run(requests);
91  
92          HashSet<String[]> types = new HashSet<>();
93          for (String[] parts : strings) {
94              for (String part : parts) {
95                  String[] subparts = part.trim().split("\\+");
96                  if (subparts.length > 1) {
97                      types.add(subparts);
98                  }
99              }
100 
101         }
102 
103         return types;
104     }
105 
106     public ArrayList<String> get(String word) {
107         return get(word, null);
108     }
109 
110     public ArrayList<String> get(String word, @Nullable String type) {
111         return get(word, type, null);
112     }
113 
114     public ArrayList<String> get(String word, @Nullable String type, @Nullable ArrayList<String[]> rq) {
115         ArrayList<String> requests = new ArrayList<>();
116         requests.add(word);
117 
118         HashSet<String> words = new HashSet<>();
119 
120         if (type == null) {
121             type = "";
122         } else {
123             if (type.equals("a")) {
124                 type = "adj";
125             }
126             if (type.equals("r")) {
127                 type = "adv";
128             }
129             if (type.equals("NOUN")) {
130                 type = "n";
131             }
132             if (type.equals("VERB")) {
133                 type = "v";
134             }
135             if (type.equals("ADJECTIVE")) {
136                 type = "adj";
137             }
138             if (type.equals("ADVERB")) {
139                 type = "adv";
140             }
141             if (type.equals("PREPOSITION")) {
142                 type = "prep";
143             }
144             if (type.equals("DETERMINER")) {
145                 type = "art";
146             }
147             if (type.equals("PRONOUN")) {
148                 type = "pron";
149             }
150             if (type.equals("PUNCTUATION")) {
151                 type = "punc";
152             }
153             if (type.equals("OTHER")) {
154                 type = "";
155             }
156         }
157 
158         try {
159             if (rq == null) {
160                 rq = run(requests);
161             }
162 
163             for (String[] parts : rq) {
164                 for (int i = 1; i < parts.length; i++) {
165                     if (parts[i].length() > 0) {
166                         String[] subparts = parts[i].trim().split("\\+");
167                         if (type.length() > 0) {
168                             try {
169                                 if (subparts[1].equals(type)) {
170                                     String[] subsubparts = subparts[0].trim().split("~");
171                                     words.add(subsubparts[subsubparts.length - 1]);
172                                 }
173                             } catch (Exception e) {
174                                 words.add(subparts[0]);
175                                 // System.out.println(Arrays.toString(subparts));
176                             }
177                         } else {
178                             for (@SuppressWarnings("unused") String part : subparts) {
179                                 String[] subsubparts = subparts[0].trim()
180                                         .split("~"); //TODO: shouldn't this be part instead of subparts?
181                                 words.add(subsubparts[subsubparts.length - 1]);
182                             }
183                         }
184                     }
185                 }
186             }
187         } catch (Exception e) {
188             e.printStackTrace(System.out);
189         }
190 
191         ArrayList<String> ret = new ArrayList<String>();
192         ret.addAll(words);
193         if (ret.size() < 1) {
194             ret.add(word);
195         }
196         return ret;
197     }
198 
199     public static void main(String[] args) {
200         String command = "/Users/alessio/Documents/scripts/textpro/modules/MorphoPro/bin/fstan/x86_64/fstan";
201         String model = "/Users/alessio/Documents/scripts/textpro/modules/MorphoPro/models/italian-utf8.fsa";
202 
203         FstanRunner runner = new FstanRunner(command, model);
204         ArrayList<String> strings = new ArrayList<>();
205         strings.add("vincerlo");
206 
207         ArrayList<String[]> list = runner.run(strings);
208         for (String[] strings1 : list) {
209             System.out.println(Arrays.toString(strings1));
210         }
211 
212     }
213 
214 }