Skip to content

Commit fde788f

Browse files
committed
Update named edges in the SemgrexMatcher when rearranging nodes in the SemanticGraph. Includes a test of that functionality
1 parent 90886db commit fde788f

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

src/edu/stanford/nlp/semgraph/semgrex/ssurgeon/AddDep.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ public String toEditString() {
9797
return buf.toString();
9898
}
9999

100-
// TODO: update the SemgrexMatcher's edges as well
100+
// TODO: make the updating of named nodes & edges faster,
101+
// possibly by building a cache from node/edge to name?
101102
public static void moveNode(SemanticGraph sg, SemgrexMatcher sm, IndexedWord word, int newIndex) {
102103
List<SemanticGraphEdge> outgoing = sg.outgoingEdgeList(word);
103104
List<SemanticGraphEdge> incoming = sg.incomingEdgeList(word);
@@ -124,11 +125,25 @@ public static void moveNode(SemanticGraph sg, SemgrexMatcher sm, IndexedWord wor
124125

125126
for (SemanticGraphEdge oldEdge : outgoing) {
126127
SemanticGraphEdge newEdge = new SemanticGraphEdge(newWord, oldEdge.getTarget(), oldEdge.getRelation(), oldEdge.getWeight(), oldEdge.isExtra());
128+
129+
for (String name : sm.getEdgeNames()) {
130+
if (sm.getEdge(name) == oldEdge) {
131+
sm.putNamedEdge(name, newEdge);
132+
}
133+
}
134+
127135
sg.addEdge(newEdge);
128136
}
129137

130138
for (SemanticGraphEdge oldEdge : incoming) {
131139
SemanticGraphEdge newEdge = new SemanticGraphEdge(oldEdge.getSource(), newWord, oldEdge.getRelation(), oldEdge.getWeight(), oldEdge.isExtra());
140+
141+
for (String name : sm.getEdgeNames()) {
142+
if (sm.getEdge(name) == oldEdge) {
143+
sm.putNamedEdge(name, newEdge);
144+
}
145+
}
146+
132147
sg.addEdge(newEdge);
133148
}
134149
}

test/src/edu/stanford/nlp/semgraph/semgrex/ssurgeon/SsurgeonTest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,47 @@ public void readXMLCheckSMNodes() {
815815
assertEquals("blue", blueVertex.value());
816816
}
817817

818+
819+
/**
820+
* The AddDep should update the edge matches in the SemgrexMatcher.
821+
* If that isn't done correctly, then moving the words first
822+
* and then trying to update an edge that matched would not work
823+
*/
824+
@Test
825+
public void readXMLCheckSMEdges() {
826+
Ssurgeon inst = Ssurgeon.inst();
827+
828+
// use "dep" as the dependency so as to be language-agnostic in this test
829+
String add = String.join(newline,
830+
"<ssurgeon-pattern-list>",
831+
" <ssurgeon-pattern>",
832+
" <uid>38</uid>",
833+
" <notes>Add a word before antennae using the position</notes>",
834+
// have to bomb-proof the pattern
835+
" <semgrex>" + XMLUtils.escapeXML("{word:antennae}=antennae <obj=obj {} !> {word:blue}") + "</semgrex>",
836+
" <edit-list>addDep -gov antennae -reln dep -word blue -position -antennae</edit-list>",
837+
" <edit-list>relabelNamedEdge -edge obj -reln dep</edit-list>",
838+
" </ssurgeon-pattern>",
839+
"</ssurgeon-pattern-list>");
840+
List<SsurgeonPattern> patterns = inst.readFromString(add);
841+
assertEquals(patterns.size(), 1);
842+
SsurgeonPattern addSsurgeon = patterns.get(0);
843+
844+
SemanticGraph sg = SemanticGraph.valueOf("[has-2 nsubj> Jennifer-1 obj> antennae-3]");
845+
IndexedWord blueVertex = sg.getNodeByIndexSafe(4);
846+
assertNull(blueVertex);
847+
SemanticGraph newSG = addSsurgeon.iterate(sg);
848+
// the edge update to change the name of the edge to "dep" should fire
849+
SemanticGraph expected = SemanticGraph.valueOf("[has-2 nsubj> Jennifer-1 dep> [antennae-4 dep> blue-3]]");
850+
assertEquals(expected, newSG);
851+
// the Ssurgeon we just created should not put a tag on the word
852+
// but it SHOULD put blue immediately before antennae
853+
blueVertex = newSG.getNodeByIndexSafe(3);
854+
assertNotNull(blueVertex);
855+
assertNull(blueVertex.tag());
856+
assertEquals("blue", blueVertex.value());
857+
}
858+
818859
/**
819860
* Test that types which can't be converted from String
820861
* are detected when making an AddDep

0 commit comments

Comments
 (0)