Skip to content

Commit 0dc6e10

Browse files
committed
Support one specific case of Map<String, String> in AnnotationLookup, the morphological features. Adds that as a feature to ssurgeon
1 parent cb6a2f6 commit 0dc6e10

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

src/edu/stanford/nlp/ling/AnnotationLookup.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ private enum KeyLookup {
4242
ANSWER_KEY(CoreAnnotations.AnswerAnnotation.class, "answer"),
4343
GOLDANSWER_Key(CoreAnnotations.GoldAnswerAnnotation.class, "goldAnswer"),
4444
FEATURES_KEY(CoreAnnotations.FeaturesAnnotation.class, "features"),
45+
MORPHOLOGICAL_FEATURES_KEY(CoreAnnotations.CoNLLUFeats.class, "morphofeatures"),
4546
INTERPRETATION_KEY(CoreAnnotations.InterpretationAnnotation.class, "interpretation"),
4647
ROLE_KEY(CoreAnnotations.RoleAnnotation.class, "srl"),
4748
GAZETTEER_KEY(CoreAnnotations.GazetteerAnnotation.class, "gazetteer"),

src/edu/stanford/nlp/ling/CoreLabel.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.TreeMap;
77
import java.util.function.Consumer;
88

9+
import edu.stanford.nlp.trees.ud.CoNLLUUtils;
910
import edu.stanford.nlp.util.ArrayCoreMap;
1011
import edu.stanford.nlp.util.CoreMap;
1112
import edu.stanford.nlp.util.Generics;
@@ -197,6 +198,8 @@ private void initFromStrings(String[] keys, String[] values) {
197198
this.set(coreKeyClass, Double.parseDouble(values[i]));
198199
} else if(valueClass == Long.class) {
199200
this.set(coreKeyClass, Long.parseLong(values[i]));
201+
} else if (coreKeyClass == CoreAnnotations.CoNLLUFeats.class) {
202+
this.set(coreKeyClass, CoNLLUUtils.parseFeatures(values[i]));
200203
} else {
201204
throw new UnsupportedOperationException("CORE: CoreLabel.initFromStrings: " +
202205
"Can't handle " + valueClass + " (key " + key + ")");

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,49 @@ public void readXMLAddDepRelativePosition() {
747747
assertEquals("blue", blueVertex.value());
748748
}
749749

750+
751+
/**
752+
* Add a word, this time setting the morphological features as well
753+
*/
754+
@Test
755+
public void readXMLAddDepMorphoFeatures() {
756+
Ssurgeon inst = Ssurgeon.inst();
757+
758+
// use "dep" as the dependency so as to be language-agnostic in this test
759+
String add = String.join(newline,
760+
"<ssurgeon-pattern-list>",
761+
" <ssurgeon-pattern>",
762+
" <uid>38</uid>",
763+
" <notes>Add a word before antennae using the position</notes>",
764+
// have to bomb-proof the pattern
765+
" <semgrex>" + XMLUtils.escapeXML("{word:antennae}=antennae !> {word:blue}") + "</semgrex>",
766+
" <edit-list>addDep -gov antennae -reln dep -word blue -position -antennae -morphofeatures a=b|c=d</edit-list>",
767+
" </ssurgeon-pattern>",
768+
"</ssurgeon-pattern-list>");
769+
List<SsurgeonPattern> patterns = inst.readFromString(add);
770+
assertEquals(patterns.size(), 1);
771+
SsurgeonPattern addSsurgeon = patterns.get(0);
772+
773+
SemanticGraph sg = SemanticGraph.valueOf("[has-2 nsubj> Jennifer-1 obj> antennae-3]");
774+
IndexedWord blueVertex = sg.getNodeByIndexSafe(4);
775+
assertNull(blueVertex);
776+
SemanticGraph newSG = addSsurgeon.iterate(sg);
777+
SemanticGraph expected = SemanticGraph.valueOf("[has-2 nsubj> Jennifer-1 obj> [antennae-4 dep> blue-3]]");
778+
assertEquals(expected, newSG);
779+
// the Ssurgeon we just created should not put a tag on the word
780+
// but it SHOULD put blue immediately before antennae
781+
blueVertex = newSG.getNodeByIndexSafe(3);
782+
assertNotNull(blueVertex);
783+
assertNull(blueVertex.tag());
784+
assertEquals("blue", blueVertex.value());
785+
786+
Map<String, String> expectedFeatures = new HashMap<String, String>() {{
787+
put("a", "b");
788+
put("c", "d");
789+
}};
790+
assertEquals(expectedFeatures, blueVertex.get(CoreAnnotations.CoNLLUFeats.class));
791+
}
792+
750793
/**
751794
* Set the language when adding a dep. Should create a UniversalEnglish dependency
752795
*/

0 commit comments

Comments
 (0)