Skip to content

Commit 1c19313

Browse files
committed
Check that a couple annotation types we won't be able to translate cause exceptions
1 parent 01a61cc commit 1c19313

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ public AddDep(String govNodeName, GrammaticalRelation relation, Map<String, Stri
4646

4747
public AddDep(String govNodeName, GrammaticalRelation relation, Map<String, String> attributes, double weight) {
4848
// if there's an exception, we'll barf here rather than at runtime
49-
CoreLabel newNodeObj = fromCheapStrings(attributes);
49+
try {
50+
CoreLabel newNodeObj = fromCheapStrings(attributes);
51+
} catch (UnsupportedOperationException e) {
52+
throw new SsurgeonParseException("Unable to process keys for AddDep operation", e);
53+
}
5054

5155
this.attributes = new TreeMap<>(attributes);
5256
this.relation = relation;
@@ -65,8 +69,7 @@ public String toEditString() {
6569
buf.write(govNodeName); buf.write("\t");
6670
buf.write(Ssurgeon.RELN_ARG);buf.write(" ");
6771
buf.write(relation.toString()); buf.write("\t");
68-
buf.write(Ssurgeon.NODE_PROTO_ARG);buf.write(" ");
69-
buf.write("\"");
72+
7073
for (String key : attributes.keySet()) {
7174
buf.write("-");
7275
buf.write(key);
@@ -123,7 +126,7 @@ public static CoreLabel fromCheapStrings(Map<String, String> attributes) {
123126
values[idx] = value;
124127
++idx;
125128
}
126-
CoreLabel newWord = new CoreLabel(keys, values);
129+
final CoreLabel newWord = new CoreLabel(keys, values);
127130
if (newWord.value() == null && newWord.word() != null) {
128131
newWord.setValue(newWord.word());
129132
}

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,53 @@ public void readXMLAddDepBrokenAnnotation() {
671671
}
672672
}
673673

674+
@Test
675+
public void checkAnnotationConversionErrors() {
676+
Ssurgeon inst = Ssurgeon.inst();
677+
678+
// this should exist, but a string will not produce it
679+
assertNotNull(AnnotationLookup.toCoreKey("SPAN"));
680+
681+
// This will fail because IntPair cannot be converted from a String
682+
String add = String.join(newline,
683+
"<ssurgeon-pattern-list>",
684+
" <ssurgeon-pattern>",
685+
" <uid>38</uid>",
686+
" <notes>Remove all incoming edges for a node</notes>",
687+
// have to bomb-proof the pattern
688+
" <semgrex>" + XMLUtils.escapeXML("{word:antennae}=antennae !> {word:blue}") + "</semgrex>",
689+
" <edit-list>addDep -gov antennae -reln dep -SPAN blue</edit-list>",
690+
" </ssurgeon-pattern>",
691+
"</ssurgeon-pattern-list>");
692+
693+
try {
694+
List<SsurgeonPattern> patterns = inst.readFromString(add);
695+
throw new AssertionError("Expected a failure because IntPair is not readable from a String in CoreLabel");
696+
} catch (SsurgeonParseException e) {
697+
// yay
698+
}
699+
700+
assertNotNull(AnnotationLookup.toCoreKey("headidx"));
701+
// This will also fail, this time because Integer cannot be converted from a String
702+
add = String.join(newline,
703+
"<ssurgeon-pattern-list>",
704+
" <ssurgeon-pattern>",
705+
" <uid>38</uid>",
706+
" <notes>Remove all incoming edges for a node</notes>",
707+
// have to bomb-proof the pattern
708+
" <semgrex>" + XMLUtils.escapeXML("{word:antennae}=antennae !> {word:blue}") + "</semgrex>",
709+
" <edit-list>addDep -gov antennae -reln dep -headidx blue</edit-list>",
710+
" </ssurgeon-pattern>",
711+
"</ssurgeon-pattern-list>");
712+
713+
try {
714+
List<SsurgeonPattern> patterns = inst.readFromString(add);
715+
throw new AssertionError("Expected a failure because IntPair is not readable from a String in CoreLabel");
716+
} catch (SsurgeonParseException e) {
717+
// yay
718+
}
719+
}
720+
674721
/**
675722
* Simple test of an Ssurgeon edit script. This instances a simple semantic graph,
676723
* a semgrex pattern, and then the resulting actions over the named nodes in the

0 commit comments

Comments
 (0)