1
1
package edu .stanford .nlp .semgraph .semgrex .ssurgeon ;
2
2
3
+ import java .util .Collections ;
4
+ import java .util .HashMap ;
3
5
import java .util .Map ;
4
6
import java .util .TreeMap ;
5
7
8
+ import edu .stanford .nlp .ling .CoreAnnotations ;
6
9
import edu .stanford .nlp .ling .CoreLabel ;
7
10
import edu .stanford .nlp .ling .IndexedWord ;
8
11
import edu .stanford .nlp .semgraph .SemanticGraph ;
9
12
import edu .stanford .nlp .semgraph .semgrex .SemgrexMatcher ;
13
+ import edu .stanford .nlp .trees .ud .CoNLLUUtils ;
10
14
11
15
/**
12
16
* Edit an existing node to have new attributes.
@@ -18,17 +22,23 @@ public class EditNode extends SsurgeonEdit {
18
22
19
23
final String nodeName ;
20
24
final Map <String , String > attributes ;
25
+ final Map <String , String > updateMorphoFeatures ;
21
26
22
- public EditNode (String nodeName , Map <String , String > attributes ) {
27
+ public EditNode (String nodeName , Map <String , String > attributes , String updateMorphoFeatures ) {
23
28
if (nodeName == null ) {
24
29
throw new SsurgeonParseException ("Cannot make an EditNode with no nodeName" );
25
30
}
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 " );
28
33
}
29
34
AddDep .checkIllegalAttributes (attributes );
30
35
this .nodeName = nodeName ;
31
36
this .attributes = new TreeMap <>(attributes );
37
+ if (updateMorphoFeatures != null ) {
38
+ this .updateMorphoFeatures = CoNLLUUtils .parseFeatures (updateMorphoFeatures );
39
+ } else {
40
+ this .updateMorphoFeatures = Collections .emptyMap ();
41
+ }
32
42
}
33
43
34
44
@@ -47,9 +57,16 @@ public String toEditString() {
47
57
buf .append (key );
48
58
buf .append (" " );
49
59
buf .append (attributes .get (key ));
60
+ // TODO: why the stray quote characters?
50
61
buf .append ("\" \t " );
51
62
}
52
63
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
+
53
70
return buf .toString ();
54
71
}
55
72
@@ -76,6 +93,21 @@ public boolean evaluate(SemanticGraph sg, SemgrexMatcher sm) {
76
93
}
77
94
}
78
95
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
+
79
111
return changed ;
80
112
}
81
113
}
0 commit comments