1   package eu.fbk.dh.tint.simplifier.rules;
2   
3   import com.google.common.collect.HashMultimap;
4   import edu.stanford.nlp.ling.CoreAnnotations;
5   import edu.stanford.nlp.ling.CoreLabel;
6   import edu.stanford.nlp.ling.IndexedWord;
7   import edu.stanford.nlp.pipeline.Annotation;
8   import edu.stanford.nlp.semgraph.SemanticGraph;
9   import edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations;
10  import edu.stanford.nlp.semgraph.SemanticGraphEdge;
11  import edu.stanford.nlp.util.CoreMap;
12  import eu.fbk.dh.tint.digimorph.annotator.DigiMorphAnnotations;
13  import eu.fbk.dh.tint.inverse.digimorph.annotator.InverseDigiMorph;
14  
15  import java.util.*;
16  
17  import static eu.fbk.dh.tint.simplifier.Simplifier.getHistory;
18  
19  /**
20   * Created by alessio on 15/02/17.
21   */
22  
23  public class ReplaceSubordinateRule implements SimplificationRule {
24  
25      static TreeSet<String> getPersons(SemanticGraph semanticGraph, IndexedWord word, CoreMap sentence) {
26          Stack<IndexedWord> wordsToCheck = new Stack<>();
27          wordsToCheck.add(word);
28  
29          int index = word.index();
30  
31          while (!wordsToCheck.isEmpty()) {
32              IndexedWord thisWord = wordsToCheck.pop();
33              List<SemanticGraphEdge> outEdgesSorted = semanticGraph.getOutEdgesSorted(thisWord);
34              for (SemanticGraphEdge semanticGraphEdge : outEdgesSorted) {
35                  IndexedWord dependent = semanticGraphEdge.getDependent();
36                  String pos = dependent.get(CoreAnnotations.PartOfSpeechAnnotation.class);
37                  if (pos.equals("VA")) {
38                      index = Math.min(index, dependent.index());
39                      wordsToCheck.push(dependent);
40                  }
41              }
42          }
43  
44          CoreLabel token = sentence.get(CoreAnnotations.TokensAnnotation.class).get(index - 1);
45          String morpho = token.get(DigiMorphAnnotations.MorphoAnnotation.class);
46          String[] parts = morpho.split("\\s+");
47          TreeSet<String> persons = new TreeSet<>();
48          for (int i = 1; i < parts.length; i++) {
49              String[] vParts = parts[i].split("\\+");
50              if (!vParts[1].equals("v")) {
51                  continue;
52              }
53  
54              persons.add(vParts[5] + "+" + vParts[6]);
55          }
56          return persons;
57      }
58  
59      @Override public String apply(Annotation annotation, Map<Integer, HashMultimap<Integer, Integer>> children) {
60  
61          InverseDigiMorph dm = new InverseDigiMorph();
62  
63          int conj = 0;
64          List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class);
65          CoreMap sentence = sentences.get(0);
66  
67          //
68  
69          SemanticGraph semanticGraph = sentence
70                  .get(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class);
71  
72          IndexedWord node = semanticGraph.getNodeByIndex(conj + 1);
73          List<IndexedWord> history = getHistory(semanticGraph, node);
74          if (history.size() == 1) {
75              return null;
76          }
77          IndexedWord verb = history.get(1);
78          CoreLabel token = sentence.get(CoreAnnotations.TokensAnnotation.class).get(verb.index() - 1);
79          String pos = token.get(CoreAnnotations.PartOfSpeechAnnotation.class);
80          if (!pos.startsWith("V")) {
81              return null;
82          }
83  
84          // todo: check subject in parse tree
85          // todo: check clitics
86  
87          String morpho = token.get(DigiMorphAnnotations.MorphoAnnotation.class);
88          String[] parts = morpho.split("\\s+");
89          TreeSet<String> persons = new TreeSet<>();
90          String tempo = null;
91          for (int i = 1; i < parts.length; i++) {
92              String[] vParts = parts[i].split("\\+");
93              if (!vParts[1].equals("v")) {
94                  continue;
95              }
96  
97              String modo = vParts[2];
98              if (!modo.equals("cong")) {
99                  continue;
100             }
101 
102             tempo = vParts[3];
103             persons.add(vParts[5] + "+" + vParts[6]);
104         }
105 
106         IndexedWord next = null;
107         if (persons.size() != 1) {
108             for (int i = 2; i < history.size(); i++) {
109                 if (history.get(i).get(CoreAnnotations.PartOfSpeechAnnotation.class).startsWith("V")) {
110                     next = history.get(i);
111                     break;
112                 }
113             }
114             persons = getPersons(semanticGraph, next, sentence);
115         }
116 
117         StringBuffer stringBuffer = new StringBuffer();
118         stringBuffer.append(token.lemma());
119         stringBuffer.append("+v+indic+").append(tempo);
120         stringBuffer.append("+nil+");
121 
122         // Add person
123         stringBuffer.append(persons.last());
124 
125         String find = stringBuffer.toString();
126 
127         System.out.println(find);
128         String inverseMorphology = dm.getInverseMorphology(find);
129 
130         System.out.println(inverseMorphology);
131         System.out.println(morpho);
132         System.out.println(tempo);
133         System.out.println(persons);
134 
135 //        System.out.println(annotation.get(UDPipeAnnotations.UDPipeOriginalAnnotation.class));
136 //        System.out.println(sentence.get(CoreAnnotations.TokensAnnotation.class).get(2)
137 //                .get(UDPipeAnnotations.FeaturesAnnotation.class));
138 //        System.out.println(token
139 //                .get(UDPipeAnnotations.FeaturesAnnotation.class));
140 //
141 //        System.out.println(children.get(0).get(verb.index()));
142 //        System.out.println(children);
143 //        System.out.println(verb.get(UDPipeAnnotations.FeaturesAnnotation.class));
144 
145 //        try {
146 //            System.out.println(JSONOutputter.jsonPrint(annotation));
147 //        } catch (IOException e) {
148 //            e.printStackTrace();
149 //        }
150 //        System.out.println(getHistory(semanticGraph, node));
151 //        System.out.println(semanticGraph.getOutEdgesSorted(node));
152 //        System.out.println(semanticGraph.getIncomingEdgesSorted(node));
153 //        System.out.println(node);
154         return null;
155     }
156 }