-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
EditNode.java
113 lines (98 loc) · 3.37 KB
/
EditNode.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package edu.stanford.nlp.semgraph.semgrex.ssurgeon;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.ling.IndexedWord;
import edu.stanford.nlp.semgraph.SemanticGraph;
import edu.stanford.nlp.semgraph.semgrex.SemgrexMatcher;
import edu.stanford.nlp.trees.ud.CoNLLUUtils;
/**
* Edit an existing node to have new attributes.
*
* @author John Bauer
*/
public class EditNode extends SsurgeonEdit {
public static final String LABEL = "editNode";
final String nodeName;
final Map<String, String> attributes;
final Map<String, String> updateMorphoFeatures;
public EditNode(String nodeName, Map<String, String> attributes, String updateMorphoFeatures) {
if (nodeName == null) {
throw new SsurgeonParseException("Cannot make an EditNode with no nodeName");
}
if (attributes.size() == 0 && updateMorphoFeatures == null) {
throw new SsurgeonParseException("Cannot make an EditNode with no attributes or updated morphological features");
}
AddDep.checkIllegalAttributes(attributes);
this.nodeName = nodeName;
this.attributes = new TreeMap<>(attributes);
if (updateMorphoFeatures != null) {
this.updateMorphoFeatures = CoNLLUUtils.parseFeatures(updateMorphoFeatures);
} else {
this.updateMorphoFeatures = Collections.emptyMap();
}
}
/**
* Emits a parseable instruction string.
*/
@Override
public String toEditString() {
StringBuilder buf = new StringBuilder();
buf.append(LABEL); buf.append("\t");
buf.append(Ssurgeon.NODENAME_ARG);buf.append(" ");
buf.append(nodeName); buf.append("\t");
for (String key : attributes.keySet()) {
buf.append("-");
buf.append(key);
buf.append(" ");
buf.append(attributes.get(key));
// TODO: why the stray quote characters?
buf.append("\"\t");
}
if (this.updateMorphoFeatures.size() > 0) {
buf.append(Ssurgeon.UPDATE_MORPHO_FEATURES);
buf.append(" ");
buf.append(CoNLLUUtils.toFeatureString(this.updateMorphoFeatures));
}
return buf.toString();
}
public boolean evaluate(SemanticGraph sg, SemgrexMatcher sm) {
IndexedWord word = sm.getNode(nodeName);
if (word == null)
return false;
CoreLabel other = AddDep.fromCheapStrings(attributes);
boolean changed = false;
for (Class key : other.keySet()) {
Object thisV = word.get(key);
Object otherV = other.get(key);
if (thisV == null || otherV == null) {
if (thisV != null || otherV != null) {
changed = true;
word.set(key, otherV);
}
} else {
if (!thisV.equals(otherV)) {
changed = true;
word.set(key, otherV);
}
}
}
for (String key : updateMorphoFeatures.keySet()) {
HashMap<String, String> features = word.get(CoreAnnotations.CoNLLUFeats.class);
if (features == null) {
changed = true;
features = new HashMap<>();
word.set(CoreAnnotations.CoNLLUFeats.class, features);
}
// this test will catch null, eg not yet assigned, features as well
if (!updateMorphoFeatures.get(key).equals(features.get(key))) {
changed = true;
features.put(key, updateMorphoFeatures.get(key));
}
}
return changed;
}
}