1   package eu.fbk.dh.tint.verb;
2   
3   import org.slf4j.Logger;
4   import org.slf4j.LoggerFactory;
5   
6   import java.io.BufferedReader;
7   import java.io.FileInputStream;
8   import java.io.InputStream;
9   import java.io.InputStreamReader;
10  import java.util.HashSet;
11  import java.util.Set;
12  
13  /**
14   * Created by alessio on 26/09/16.
15   */
16  public class VerbModel {
17  
18      private static final Logger LOGGER = LoggerFactory.getLogger(VerbModel.class);
19      private static VerbModel ourInstance = null;
20      Set<String> transitiveVerbs;
21  
22      public static VerbModel getInstance() {
23          return getInstance(null);
24      }
25  
26      public static VerbModel getInstance(String fileName) {
27          Set<String> transitiveVerbs = new HashSet<>();
28          if (ourInstance == null) {
29  
30              try {
31                  InputStream configStream;
32  
33                  if (fileName == null) {
34                      configStream = VerbModel.class.getResourceAsStream("/transitivi.txt");
35                  } else {
36                      configStream = new FileInputStream(fileName);
37                  }
38                  if (configStream != null) {
39                      BufferedReader reader = new BufferedReader(new InputStreamReader(configStream));
40                      String line;
41                      while ((line = reader.readLine()) != null) {
42                          line = line.trim();
43                          if (line.length() == 0) {
44                              continue;
45                          }
46                          if (line.startsWith("#")) {
47                              continue;
48                          }
49                          transitiveVerbs.add(line);
50                      }
51                      reader.close();
52                  }
53                  ourInstance = new VerbModel(transitiveVerbs);
54              } catch (Exception e) {
55                  e.printStackTrace();
56              }
57          } else {
58              LOGGER.info("Verb model already loaded");
59          }
60          return ourInstance;
61      }
62  
63      private VerbModel(Set<String> transitiveVerbs) {
64          this.transitiveVerbs = transitiveVerbs;
65      }
66  
67      public Set<String> getTransitiveVerbs() {
68          return transitiveVerbs;
69      }
70  
71  }