11package edu .stanford .nlp .semgraph .semgrex .ssurgeon ;
22
3+ import java .util .Collections ;
4+ import java .util .HashMap ;
35import java .util .Map ;
46import java .util .TreeMap ;
57
8+ import edu .stanford .nlp .ling .CoreAnnotations ;
69import edu .stanford .nlp .ling .CoreLabel ;
710import edu .stanford .nlp .ling .IndexedWord ;
811import edu .stanford .nlp .semgraph .SemanticGraph ;
912import edu .stanford .nlp .semgraph .semgrex .SemgrexMatcher ;
13+ import edu .stanford .nlp .trees .ud .CoNLLUUtils ;
1014
1115/**
1216 * Edit an existing node to have new attributes.
@@ -18,17 +22,23 @@ public class EditNode extends SsurgeonEdit {
1822
1923 final String nodeName ;
2024 final Map <String , String > attributes ;
25+ final Map <String , String > updateMorphoFeatures ;
2126
22- public EditNode (String nodeName , Map <String , String > attributes ) {
27+ public EditNode (String nodeName , Map <String , String > attributes , String updateMorphoFeatures ) {
2328 if (nodeName == null ) {
2429 throw new SsurgeonParseException ("Cannot make an EditNode with no nodeName" );
2530 }
26- if (attributes .size () == 0 ) {
27- throw new SsurgeonParseException ("Cannot make an EditNode with no attributes" );
31+ if (attributes .size () == 0 && updateMorphoFeatures == null ) {
32+ throw new SsurgeonParseException ("Cannot make an EditNode with no attributes or updated morphological features " );
2833 }
2934 AddDep .checkIllegalAttributes (attributes );
3035 this .nodeName = nodeName ;
3136 this .attributes = new TreeMap <>(attributes );
37+ if (updateMorphoFeatures != null ) {
38+ this .updateMorphoFeatures = CoNLLUUtils .parseFeatures (updateMorphoFeatures );
39+ } else {
40+ this .updateMorphoFeatures = Collections .emptyMap ();
41+ }
3242 }
3343
3444
@@ -47,9 +57,16 @@ public String toEditString() {
4757 buf .append (key );
4858 buf .append (" " );
4959 buf .append (attributes .get (key ));
60+ // TODO: why the stray quote characters?
5061 buf .append ("\" \t " );
5162 }
5263
64+ if (this .updateMorphoFeatures .size () > 0 ) {
65+ buf .append (Ssurgeon .UPDATE_MORPHO_FEATURES );
66+ buf .append (" " );
67+ buf .append (CoNLLUUtils .toFeatureString (this .updateMorphoFeatures ));
68+ }
69+
5370 return buf .toString ();
5471 }
5572
@@ -76,6 +93,21 @@ public boolean evaluate(SemanticGraph sg, SemgrexMatcher sm) {
7693 }
7794 }
7895
96+ for (String key : updateMorphoFeatures .keySet ()) {
97+ HashMap <String , String > features = word .get (CoreAnnotations .CoNLLUFeats .class );
98+ if (features == null ) {
99+ changed = true ;
100+ features = new HashMap <>();
101+ word .set (CoreAnnotations .CoNLLUFeats .class , features );
102+ }
103+
104+ // this test will catch null, eg not yet assigned, features as well
105+ if (!updateMorphoFeatures .get (key ).equals (features .get (key ))) {
106+ changed = true ;
107+ features .put (key , updateMorphoFeatures .get (key ));
108+ }
109+ }
110+
79111 return changed ;
80112 }
81113}
0 commit comments