Skip to content

Commit 2823808

Browse files
committed
Upgrade Ssurgeon RemoveNamedEdge to remove edges based on their name (it was currently unused everyone, including RTE, so changing the interface should be no harm done)
1 parent f9ee36f commit 2823808

File tree

3 files changed

+41
-28
lines changed

3 files changed

+41
-28
lines changed

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

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import edu.stanford.nlp.ling.IndexedWord;
66
import edu.stanford.nlp.semgraph.semgrex.SemgrexMatcher;
7-
import edu.stanford.nlp.trees.GrammaticalRelation;
87
import edu.stanford.nlp.semgraph.SemanticGraph;
98
import edu.stanford.nlp.semgraph.SemanticGraphEdge;
109

@@ -23,14 +22,10 @@
2322
public class RemoveNamedEdge extends SsurgeonEdit {
2423
public static final String LABEL = "removeNamedEdge";
2524

26-
protected String edgeName; // Name of the matched edge in the SemgrexPattern
27-
protected String govName; // Name of governor of this reln, in match
28-
protected String depName; // Name of the dependent in this reln, in match
25+
protected final String edgeName; // Name of the matched edge in the SemgrexPattern
2926

30-
public RemoveNamedEdge(String edgeName, String govName, String depName) {
27+
public RemoveNamedEdge(String edgeName) {
3128
this.edgeName = edgeName;
32-
this.govName = govName;
33-
this.depName = depName;
3429
}
3530

3631
@Override
@@ -39,10 +34,6 @@ public String toEditString() {
3934
buf.write(LABEL); buf.write("\t");
4035
buf.write(Ssurgeon.EDGE_NAME_ARG);buf.write(" ");
4136
buf.write(edgeName); buf.write("\t");
42-
buf.write(Ssurgeon.GOV_NODENAME_ARG);buf.write(" ");
43-
buf.write(govName); buf.write("\t");
44-
buf.write(Ssurgeon.DEP_NODENAME_ARG);buf.write(" ");
45-
buf.write(depName);
4637
return buf.toString();
4738
}
4839

@@ -56,10 +47,7 @@ public String toEditString() {
5647
*/
5748
@Override
5849
public boolean evaluate(SemanticGraph sg, SemgrexMatcher sm) {
59-
String relation = sm.getRelnString(edgeName);
60-
IndexedWord govNode = getNamedNode(govName, sm);
61-
IndexedWord depNode = getNamedNode(depName, sm);
62-
SemanticGraphEdge edge = sg.getEdge(govNode, depNode, GrammaticalRelation.valueOf(relation));
50+
SemanticGraphEdge edge = sm.getEdge(edgeName);
6351

6452
if (edge != null) {
6553
sg.removeEdge(edge);
@@ -68,16 +56,7 @@ public boolean evaluate(SemanticGraph sg, SemgrexMatcher sm) {
6856
return false;
6957
}
7058

71-
public String getDepName() {
72-
return depName;
73-
}
74-
7559
public String getEdgeName() {
7660
return edgeName;
7761
}
78-
79-
public String getGovName() {
80-
return govName;
81-
}
82-
8362
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ public static SsurgeonEdit parseEditLine(String editLine) {
362362
} else if (command.equalsIgnoreCase(RemoveEdge.LABEL)) {
363363
retEdit = new RemoveEdge(GrammaticalRelation.valueOf(argsBox.reln), argsBox.govNodeName, argsBox.dep);
364364
} else if (command.equalsIgnoreCase(RemoveNamedEdge.LABEL)) {
365-
retEdit = new RemoveNamedEdge(argsBox.edge, argsBox.govNodeName, argsBox.dep);
365+
retEdit = new RemoveNamedEdge(argsBox.edge);
366366
} else if (command.equalsIgnoreCase(SetRoots.LABEL)) {
367367
String[] names = tuples1[1].split("\\s+");
368368
List<String> newRoots = Arrays.asList(names);

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

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,40 @@ public void readXMLRemoveEdgeIterate() {
117117
assertEquals(newSg, expected);
118118
}
119119

120+
121+
/**
122+
* Test that removing a named edge will remove all matching edges
123+
*/
124+
@Test
125+
public void readXMLRemoveNamedEdgeIterate() {
126+
String doc = String.join(newline,
127+
"<ssurgeon-pattern-list>",
128+
" <ssurgeon-pattern>",
129+
" <uid>38</uid>",
130+
" <notes>This is a simple test of RemoveNamedEdge</notes>",
131+
" <semgrex>" + XMLUtils.escapeXML("{}=a1 >dep~foo {}=a2") + "</semgrex>",
132+
" <edit-list>removeNamedEdge -edge foo</edit-list>",
133+
" </ssurgeon-pattern>",
134+
"</ssurgeon-pattern-list>");
135+
Ssurgeon inst = Ssurgeon.inst();
136+
List<SsurgeonPattern> patterns = inst.readFromString(doc);
137+
assertEquals(patterns.size(), 1);
138+
SsurgeonPattern pattern = patterns.get(0);
139+
140+
SemanticGraph sg = SemanticGraph.valueOf("[A-0 obj> B-1 obj> C-2 nsubj> [D-3 obj> E-4]]");
141+
SemanticGraph newSg = pattern.iterate(sg);
142+
SemanticGraph expected = SemanticGraph.valueOf("[A-0 obj> B-1 obj> C-2 nsubj> [D-3 obj> E-4]]");
143+
assertEquals(newSg, sg);
144+
145+
sg = SemanticGraph.valueOf("[A-0 obj> B-1 dep> B-1 obj> C-2 nsubj> [D-3 obj> E-4]]");
146+
newSg = pattern.iterate(sg);
147+
assertEquals(newSg, expected);
148+
149+
sg = SemanticGraph.valueOf("[A-0 obj> B-1 dep> B-1 obj> C-2 dep> C-2 nsubj> [D-3 obj> E-4 dep> E-4]]");
150+
newSg = pattern.iterate(sg);
151+
assertEquals(newSg, expected);
152+
}
153+
120154
/**
121155
* Check that cutting a graph with two nodes into two pieces, then
122156
* pruning any disjoint pieces, results in a graph with just the root
@@ -241,16 +275,16 @@ public void readXMLPruneNodesResetRoots() {
241275
@Test
242276
public void simpleTest() {
243277
SemanticGraph sg = SemanticGraph.valueOf("[mixed/VBN nsubj>[Joe/NNP appos>[bartender/NN det>the/DT]] obj>[drink/NN det>a/DT]]");
244-
SemgrexPattern semgrexPattern = SemgrexPattern.compile("{}=a1 >appos=e1 {}=a2 <nsubj=e2 {}=a3");
278+
SemgrexPattern semgrexPattern = SemgrexPattern.compile("{}=a1 >appos~e1 {}=a2 <nsubj~e2 {}=a3");
245279
SsurgeonPattern pattern = new SsurgeonPattern(semgrexPattern);
246280

247281
System.out.println("Start = "+sg.toCompactString());
248282

249283
// Find and snip the appos and root to nsubj links
250-
SsurgeonEdit apposSnip = new RemoveNamedEdge("e1", "a1", "a2");
284+
SsurgeonEdit apposSnip = new RemoveNamedEdge("e1");
251285
pattern.addEdit(apposSnip);
252286

253-
SsurgeonEdit nsubjSnip = new RemoveNamedEdge("e2", "a3", "a1");
287+
SsurgeonEdit nsubjSnip = new RemoveNamedEdge("e2");
254288
pattern.addEdit(nsubjSnip);
255289

256290
// Attach Joe to be the nsubj of bartender

0 commit comments

Comments
 (0)