|
2 | 2 |
|
3 | 3 | import java.io.StringWriter;
|
4 | 4 | import java.util.*;
|
5 |
| -import java.util.function.Function; |
6 |
| -import java.util.stream.Collectors; |
7 | 5 |
|
8 | 6 | import edu.stanford.nlp.ling.CoreLabel;
|
9 | 7 | import edu.stanford.nlp.ling.IndexedWord;
|
10 | 8 | import edu.stanford.nlp.semgraph.SemanticGraph;
|
11 |
| -import edu.stanford.nlp.semgraph.SemanticGraphEdge; |
12 | 9 | import edu.stanford.nlp.semgraph.SemanticGraphUtils;
|
13 | 10 | import edu.stanford.nlp.semgraph.semgrex.SemgrexMatcher;
|
14 |
| -import edu.stanford.nlp.trees.EnglishGrammaticalRelations; |
15 | 11 | import edu.stanford.nlp.trees.GrammaticalRelation;
|
16 | 12 |
|
17 | 13 | /**
|
@@ -90,72 +86,6 @@ public String toEditString() {
|
90 | 86 | return buf.toString();
|
91 | 87 | }
|
92 | 88 |
|
93 |
| - // TODO: make the updating of named nodes & edges faster, |
94 |
| - // possibly by building a cache from node/edge to name? |
95 |
| - public static void moveNode(SemanticGraph sg, SemgrexMatcher sm, IndexedWord word, int newIndex) { |
96 |
| - List<SemanticGraphEdge> outgoing = sg.outgoingEdgeList(word); |
97 |
| - List<SemanticGraphEdge> incoming = sg.incomingEdgeList(word); |
98 |
| - boolean isRoot = sg.isRoot(word); |
99 |
| - sg.removeVertex(word); |
100 |
| - |
101 |
| - IndexedWord newWord = new IndexedWord(word.backingLabel()); |
102 |
| - newWord.setIndex(newIndex); |
103 |
| - |
104 |
| - // could be more expensive than necessary if we move multiple roots, |
105 |
| - // but the expectation is there is usually only the 1 root |
106 |
| - if (isRoot) { |
107 |
| - Set<IndexedWord> newRoots = new HashSet<>(sg.getRoots()); |
108 |
| - newRoots.remove(word); |
109 |
| - newRoots.add(newWord); |
110 |
| - sg.setRoots(newRoots); |
111 |
| - } |
112 |
| - |
113 |
| - for (String name : sm.getNodeNames()) { |
114 |
| - if (sm.getNode(name) == word) { |
115 |
| - sm.putNode(name, newWord); |
116 |
| - } |
117 |
| - } |
118 |
| - |
119 |
| - for (SemanticGraphEdge oldEdge : outgoing) { |
120 |
| - SemanticGraphEdge newEdge = new SemanticGraphEdge(newWord, oldEdge.getTarget(), oldEdge.getRelation(), oldEdge.getWeight(), oldEdge.isExtra()); |
121 |
| - |
122 |
| - for (String name : sm.getEdgeNames()) { |
123 |
| - if (sm.getEdge(name) == oldEdge) { |
124 |
| - sm.putNamedEdge(name, newEdge); |
125 |
| - } |
126 |
| - } |
127 |
| - |
128 |
| - sg.addEdge(newEdge); |
129 |
| - } |
130 |
| - |
131 |
| - for (SemanticGraphEdge oldEdge : incoming) { |
132 |
| - SemanticGraphEdge newEdge = new SemanticGraphEdge(oldEdge.getSource(), newWord, oldEdge.getRelation(), oldEdge.getWeight(), oldEdge.isExtra()); |
133 |
| - |
134 |
| - for (String name : sm.getEdgeNames()) { |
135 |
| - if (sm.getEdge(name) == oldEdge) { |
136 |
| - sm.putNamedEdge(name, newEdge); |
137 |
| - } |
138 |
| - } |
139 |
| - |
140 |
| - sg.addEdge(newEdge); |
141 |
| - } |
142 |
| - } |
143 |
| - |
144 |
| - /** |
145 |
| - * reverse: operate in reverse order, highest index to first. You want true if moving indices up, false if moving indices down |
146 |
| - */ |
147 |
| - public static void moveNodes(SemanticGraph sg, SemgrexMatcher sm, Function<Integer, Boolean> shouldMove, Function<Integer, Integer> destination, boolean reverse) { |
148 |
| - // iterate first, then move, so that we don't screw up the graph while iterating |
149 |
| - List<IndexedWord> toMove = sg.vertexSet().stream().filter(x -> shouldMove.apply(x.index())).collect(Collectors.toList()); |
150 |
| - Collections.sort(toMove); |
151 |
| - if (reverse) { |
152 |
| - Collections.reverse(toMove); |
153 |
| - } |
154 |
| - for (IndexedWord word : toMove) { |
155 |
| - moveNode(sg, sm, word, destination.apply(word.index())); |
156 |
| - } |
157 |
| - } |
158 |
| - |
159 | 89 | /**
|
160 | 90 | * TODO: bombproof if this gov, dep, and reln already exist.
|
161 | 91 | */
|
@@ -210,8 +140,8 @@ public boolean evaluate(SemanticGraph sg, SemgrexMatcher sm) {
|
210 | 140 | if (position != null && !position.equals("+")) {
|
211 | 141 | // the payoff for tempIndex == maxIndex + 2:
|
212 | 142 | // everything will be moved one higher, unless it's the new node
|
213 |
| - moveNodes(sg, sm, x -> (x >= newIndex && x != tempIndex), x -> x+1, true); |
214 |
| - moveNode(sg, sm, newNode, newIndex); |
| 143 | + SsurgeonUtils.moveNodes(sg, sm, x -> (x >= newIndex && x != tempIndex), x -> x+1, true); |
| 144 | + SsurgeonUtils.moveNode(sg, sm, newNode, newIndex); |
215 | 145 | }
|
216 | 146 |
|
217 | 147 | return true;
|
|
0 commit comments