Skip to content

Commit 0240694

Browse files
committed
Error check that no one tries to update idx, sentIdx, or DocId
1 parent b284006 commit 0240694

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public AddDep(String govNodeName, GrammaticalRelation relation, Map<String, Stri
6060
}
6161
}
6262

63+
checkIllegalAttributes(attributes);
64+
6365
this.attributes = new TreeMap<>(attributes);
6466
this.relation = relation;
6567
this.govNodeName = govNodeName;
@@ -200,6 +202,22 @@ public boolean evaluate(SemanticGraph sg, SemgrexMatcher sm) {
200202
return true;
201203
}
202204

205+
/**
206+
* Certain attributes cannot be edited, especially docid, sentid, idx,
207+
* or they mess up the hashmaps in the SemanticGraph
208+
*/
209+
public static void checkIllegalAttributes(Map<String, String> attributes) {
210+
if (attributes.containsKey("idx")) {
211+
throw new SsurgeonParseException("Cannot manually set the index attribute. If you need a moveWord operation, please file an issue on github.");
212+
}
213+
if (attributes.containsKey("sentIndex")) {
214+
throw new SsurgeonParseException("Cannot manually change the sentence index. If you need an operation to change an entire sentence's sentIndex, please file an issue on github.");
215+
}
216+
if (attributes.containsKey("docID")) {
217+
throw new SsurgeonParseException("Cannot manually change a document ID. If you need an operation to change an entire sentence's document ID, please file an issue on github.");
218+
}
219+
}
220+
203221
/**
204222
* Given the keys and values of the CoreAnnotation attributes,
205223
* build a CoreLabel to use as the new word

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public EditNode(String nodeName, Map<String, String> attributes) {
2626
if (attributes.size() == 0) {
2727
throw new SsurgeonParseException("Cannot make an EditNode with no attributes");
2828
}
29+
AddDep.checkIllegalAttributes(attributes);
2930
this.nodeName = nodeName;
3031
this.attributes = new TreeMap<>(attributes);
3132
}

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,47 @@ public void readXMLEditNode() {
862862
assertEquals("blue", blueVertex.value());
863863
}
864864

865+
866+
/**
867+
* Test that we don't allow changing a word index, for example, in EditNode or AddDep
868+
*/
869+
@Test
870+
public void forbidIllegalAttributes() {
871+
Ssurgeon inst = Ssurgeon.inst();
872+
873+
// use "dep" as the dependency so as to be language-agnostic in this test
874+
String add = String.join(newline,
875+
"<ssurgeon-pattern-list>",
876+
" <ssurgeon-pattern>",
877+
" <uid>38</uid>",
878+
" <notes>Edit a node</notes>",
879+
" <semgrex>" + XMLUtils.escapeXML("{word:green}=blue") + "</semgrex>",
880+
" <edit-list>EditNode -node blue -idx 5</edit-list>",
881+
" </ssurgeon-pattern>",
882+
"</ssurgeon-pattern-list>");
883+
try {
884+
List<SsurgeonPattern> patterns = inst.readFromString(add);
885+
throw new AssertionError("Expected a parse exception!");
886+
} catch(SsurgeonParseException e) {
887+
// yay
888+
}
889+
add = String.join(newline,
890+
"<ssurgeon-pattern-list>",
891+
" <ssurgeon-pattern>",
892+
" <uid>38</uid>",
893+
" <notes>Edit a node</notes>",
894+
" <semgrex>" + XMLUtils.escapeXML("{word:green}=blue") + "</semgrex>",
895+
" <edit-list>addDep -gov antennae -reln dep -headidx blue -idx 5</edit-list>",
896+
" </ssurgeon-pattern>",
897+
"</ssurgeon-pattern-list>");
898+
try {
899+
List<SsurgeonPattern> patterns = inst.readFromString(add);
900+
throw new AssertionError("Expected a parse exception!");
901+
} catch(SsurgeonParseException e) {
902+
// yay
903+
}
904+
}
905+
865906
/**
866907
* Simple test of an Ssurgeon edit script. This instances a simple semantic graph,
867908
* a semgrex pattern, and then the resulting actions over the named nodes in the

0 commit comments

Comments
 (0)