1   package eu.fbk.dh.tint.readability;
2   
3   import edu.stanford.nlp.ling.CoreLabel;
4   import edu.stanford.nlp.ling.IndexedWord;
5   import edu.stanford.nlp.semgraph.SemanticGraph;
6   import edu.stanford.nlp.semgraph.SemanticGraphEdge;
7   import eu.fbk.dh.tint.verb.VerbMultiToken;
8   import eu.fbk.utils.core.FrequencyHashSet;
9   
10  import java.util.*;
11  
12  /**
13   * Created by alessio on 09/03/17.
14   */
15  
16  public class SemanticGraphUtils {
17  
18      public static void smartRemoveEdge(SemanticGraph semanticGraph, IndexedWord indexedWord) {
19          Set<IndexedWord> parents = semanticGraph.getParents(indexedWord);
20  //        if (parents)
21      }
22  
23      public static Integer getHead(VerbMultiToken verb, SemanticGraph semanticGraph) {
24          FrequencyHashSet<Integer> frequencies = new FrequencyHashSet<>();
25          Set<Integer> indexes = new HashSet<>();
26  
27          for (CoreLabel token : verb.getTokens()) {
28              int index = token.index();
29              indexes.add(index);
30              try {
31                  IndexedWord node = semanticGraph.getNodeByIndex(index);
32                  frequencies.add(index);
33                  List<IndexedWord> pathToRoot = semanticGraph.getPathToRoot(node);
34                  for (IndexedWord indexedWord : pathToRoot) {
35                      frequencies.add(indexedWord.index());
36                  }
37  
38              } catch (Exception e) {
39                  // ignored
40  //                System.out.println("ERR: no node for index " + index);
41  //                System.out.println(token);
42              }
43          }
44  
45          Set<Integer> keys = new HashSet<>();
46          for (Integer key : frequencies.keySet()) {
47              keys.add(key);
48          }
49  
50          for (Integer index : keys) {
51              if (!indexes.contains(index)) {
52                  frequencies.remove(index);
53              }
54          }
55  
56          return frequencies.mostFrequent();
57      }
58  
59      public static Map<Integer, String> getParent(VerbMultiToken verb, SemanticGraph semanticGraph) {
60          Map<Integer, String> parents = new HashMap<>();
61          Set<Integer> removeIndexes = new HashSet<>();
62  
63          for (CoreLabel token : verb.getTokens()) {
64              int index = token.index();
65              removeIndexes.add(index);
66              try {
67                  IndexedWord node = semanticGraph.getNodeByIndex(index);
68                  List<SemanticGraphEdge> incomingEdgesSorted = semanticGraph
69                          .getIncomingEdgesSorted(node);
70  //                System.out.println(token + " --- " + incomingEdgesSorted);
71                  for (SemanticGraphEdge edge : incomingEdgesSorted) {
72                      int parentIndex = edge.getGovernor().index();
73                      parents.put(parentIndex, edge.getRelation().getShortName());
74                  }
75              } catch (Exception e) {
76                  // ignored
77  //                System.out.println("ERR: no node for index " + index);
78  //                System.out.println(token);
79              }
80          }
81  
82          for (Integer removeIndex : removeIndexes) {
83              parents.remove(removeIndex);
84          }
85  
86          return parents;
87      }
88  }