1   package eu.fbk.dh.tint.verb;
2   
3   import com.google.common.collect.Iterables;
4   import edu.stanford.nlp.ling.CoreLabel;
5   import eu.fbk.utils.corenlp.CustomAnnotations;
6   
7   import java.util.ArrayList;
8   import java.util.Collection;
9   import java.util.List;
10  import java.util.Map;
11  
12  /**
13   * Created by alessio on 08/03/17.
14   */
15  
16  public class VerbMultiToken {
17  
18      List<CoreLabel> tokens = new ArrayList<>();
19      boolean isPassive = false;
20      String tense;
21      String mood;
22      Integer person = null;
23      String gender = null;
24  
25      public boolean isPassive() {
26          return isPassive;
27      }
28  
29      public String getTense() {
30          return tense;
31      }
32  
33      public String getMood() {
34          return mood;
35      }
36  
37      public Integer getPerson() {
38          return person;
39      }
40  
41      public String getGender() {
42          return gender;
43      }
44  
45      public List<CoreLabel> getTokens() {
46          return tokens;
47      }
48  
49      public void setTokens(List<CoreLabel> tokens) {
50          this.tokens = tokens;
51      }
52  
53      public void addToken(VerbModel model, CoreLabel token) {
54          addToken(model, token, false);
55      }
56  
57      public void addToken(VerbModel model, CoreLabel token, boolean last) {
58          this.tokens.add(token);
59          Map<String, Collection<String>> features = token.get(CustomAnnotations.FeaturesAnnotation.class);
60  //        System.out.println(features);
61          try {
62              gender = Iterables.getFirst(features.get("Gender"), null);
63          } catch (NullPointerException e) {
64              gender = null;
65          }
66          if (last) {
67              String lemma = this.tokens.get(this.tokens.size() - 1).lemma();
68              boolean isTransitive = false;
69              if (model.getTransitiveVerbs().contains(lemma)) {
70                  isTransitive = true;
71              }
72  
73              switch (this.tokens.size()) {
74              case 1:
75                  setTense(this.tokens.get(0));
76                  isPassive = false;
77  
78                  if (isTransitive && mood.equals("Part") && tense.equals("Past")) {
79                      isPassive = true;
80                  }
81                  break;
82              case 2:
83                  String auxLemma = this.tokens.get(0).lemma();
84                  if (auxLemma.equals("avere")) {
85                      isPassive = false;
86                  } else {
87                      if (isTransitive) {
88                          isPassive = true;
89                      } else {
90                          isPassive = false;
91                      }
92                  }
93  
94                  setTense(this.tokens.get(0));
95                  if (!isPassive) {
96                      try {
97                          addStep();
98                      } catch (NullPointerException e) {
99                          // ignored
100                     }
101                 }
102                 break;
103             default:
104                 isPassive = true;
105                 setTense(this.tokens.get(0));
106                 addStep();
107             }
108         }
109     }
110 
111     private void addStep() throws NullPointerException {
112         if (tense == null) {
113             return;
114         }
115         switch (mood) {
116         case "Ind":
117         case "Conj":
118             switch (tense) {
119             case "Pres":
120                 tense = "PrPast";
121                 break;
122             case "Imp":
123                 tense = "TrPast";
124                 break;
125             case "Past":
126                 tense = "RemPast";
127                 break;
128             case "Fut":
129                 tense = "AntFut";
130                 break;
131             }
132         default:
133             switch (tense) {
134             case "Pres":
135                 tense = "Past";
136             }
137         }
138     }
139 
140     private void setTense(CoreLabel coreLabel) {
141         Map<String, Collection<String>> features = coreLabel.get(CustomAnnotations.FeaturesAnnotation.class);
142         try {
143             tense = Iterables.getFirst(features.get("Tense"), null);
144         } catch (NullPointerException e) {
145             tense = null;
146         }
147         try {
148             mood = Iterables.getFirst(features.get("Mood"), null);
149         } catch (NullPointerException e) {
150             mood = null;
151         }
152         if (mood == null) {
153             mood = Iterables.getFirst(features.get("VerbForm"), null);
154         }
155         if (mood != null && mood.equals("Sub")) {
156             mood = "Conj";
157         }
158         String txtPerson = null;
159         try {
160             txtPerson = Iterables.getFirst(features.get("Person"), null);
161         } catch (NullPointerException e) {
162             // ignore
163         }
164         if (txtPerson != null) {
165             person = Integer.parseInt(txtPerson);
166         }
167     }
168 
169     @Override public String toString() {
170         return "VerbMultiToken{" +
171                 "tokens=" + tokens +
172                 ", isPassive=" + isPassive +
173                 ", tense='" + tense + '\'' +
174                 ", mood='" + mood + '\'' +
175                 ", person=" + person +
176                 ", gender='" + gender + '\'' +
177                 '}';
178     }
179 }