forked from stanfordnlp/CoreNLP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpanishMorphoFeatureSpecification.java
More file actions
271 lines (222 loc) · 7.65 KB
/
SpanishMorphoFeatureSpecification.java
File metadata and controls
271 lines (222 loc) · 7.65 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package edu.stanford.nlp.international.spanish;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import edu.stanford.nlp.international.morph.MorphoFeatureSpecification;
import edu.stanford.nlp.international.morph.MorphoFeatures;
/**
* If MorphoFeatureType.OTHER is active, then the "CC tagset" is produced (see Tbl.2
* of (Crabbe and Candito, 2008). Additional support exists for GEN, NUM, and PER, which
* are (mostly) marked in the FTB annotation.
* <p>
* The actual CC tag is placed in the altTag field of the MorphoFeatures object.
*
* @author Spence Green
*
*/
public class SpanishMorphoFeatureSpecification extends MorphoFeatureSpecification {
private static final long serialVersionUID = -8560629684352048892L;
public static final String[] genVals = {"M","F"};
public static final String[] numVals = {"SG","PL"};
public static final String[] perVals = {"1","2","3"};
// TODO could support tense, mood
@Override
public List<String> getValues(MorphoFeatureType feat) {
if(feat == MorphoFeatureType.GEN)
return Arrays.asList(genVals);
else if(feat == MorphoFeatureType.NUM)
return Arrays.asList(numVals);
else if(feat == MorphoFeatureType.PER)
return Arrays.asList(perVals);
else
throw new IllegalArgumentException("Spanish does not support feature type: " + feat.toString());
}
@Override
public MorphoFeatures strToFeatures(String spec) {
MorphoFeatures feats = new MorphoFeatures();
//Usually this is the boundary symbol
if(spec == null || spec.equals(""))
return feats;
boolean isOtherActive = isActive(MorphoFeatureType.OTHER);
if(spec.startsWith("ADV")) {
feats.setAltTag("ADV");
if(spec.contains("int")) {
if (isOtherActive) {
feats.addFeature(MorphoFeatureType.OTHER, "advint");
}
feats.setAltTag("ADVWH");
}
} else if(spec.startsWith("A")) {
feats.setAltTag("ADJ");
if(spec.contains("int")) {
if (isOtherActive) {
feats.addFeature(MorphoFeatureType.OTHER, "adjint");
}
feats.setAltTag("ADJWH");
}
addPhiFeatures(feats,spec);
} else if(spec.equals("CC") || spec.equals("C-C")) {
if (isOtherActive) {
feats.addFeature(MorphoFeatureType.OTHER, "Cc");
}
feats.setAltTag("CC");
} else if(spec.equals("CS") || spec.equals("C-S")) {
if (isOtherActive) {
feats.addFeature(MorphoFeatureType.OTHER, "Cs");
}
feats.setAltTag("CS");
} else if(spec.startsWith("CL")) {
feats.setAltTag("CL");
if(spec.contains("suj") || spec.equals("CL-S-3fp")) {//"CL-S-3fp" is equivalent to suj
if (isOtherActive) {
feats.addFeature(MorphoFeatureType.OTHER,"Sbj");
}
feats.setAltTag("CLS");
} else if(spec.contains("obj")) {
if (isOtherActive) {
feats.addFeature(MorphoFeatureType.OTHER, "Obj");
}
feats.setAltTag("CLO");
} else if(spec.contains("refl")) {
if (isOtherActive) {
feats.addFeature(MorphoFeatureType.OTHER, "Rfl");
}
feats.setAltTag("CLR");
}
addPhiFeatures(feats,spec);
} else if(spec.startsWith("D")) {
feats.setAltTag("DET");
if(spec.contains("int")) {
if (isOtherActive) {
feats.addFeature(MorphoFeatureType.OTHER, "dint");
}
feats.setAltTag("DETWH");
}
addPhiFeatures(feats,spec);
} else if(spec.startsWith("N")) {
feats.setAltTag("N");//TODO These are usually N-card...make these CD?
if(spec.contains("P")) {
if (isOtherActive) {
feats.addFeature(MorphoFeatureType.OTHER, "Np");
}
feats.setAltTag("NPP");
} else if(spec.contains("C")) {
if (isOtherActive) {
feats.addFeature(MorphoFeatureType.OTHER, "Nc");
}
feats.setAltTag("NC");
}
addPhiFeatures(feats,spec);
} else if(spec.startsWith("PRO")) {
feats.setAltTag("PRO");
if(spec.contains("int")) {
if (isOtherActive) {
feats.addFeature(MorphoFeatureType.OTHER,"Ni");
}
feats.setAltTag("PROWH");
} else if(spec.contains("rel")) {
if (isOtherActive) {
feats.addFeature(MorphoFeatureType.OTHER, "Nr");
}
feats.setAltTag("PROREL");
}
addPhiFeatures(feats,spec);
} else if(spec.startsWith("V")) {
feats.setAltTag("V");
if(spec.contains("Y")) {
if (isOtherActive) {
feats.addFeature(MorphoFeatureType.OTHER,"Vp");
}
feats.setAltTag("VIMP");
} else if(spec.contains("W")) {
if (isOtherActive) {
feats.addFeature(MorphoFeatureType.OTHER, "Vf");
}
feats.setAltTag("VINF");
} else if(spec.contains("S") || spec.contains("T")) {
if (isOtherActive) {
feats.addFeature(MorphoFeatureType.OTHER, "Vs");
}
feats.setAltTag("VS");
} else if(spec.contains("K")) {
if (isOtherActive) {
feats.addFeature(MorphoFeatureType.OTHER, "Vp");
}
feats.setAltTag("VPP");
} else if(spec.contains("G")) {
if (isOtherActive) {
feats.addFeature(MorphoFeatureType.OTHER, "Vr");
}
feats.setAltTag("VPR");
}
addPhiFeatures(feats,spec);
} else if(spec.equals("P") || spec.equals("I")) {
feats.setAltTag(spec);
}
// else {
// System.err.println("Could not map spec: " + spec);
// }
return feats;
}
private void addPhiFeatures(MorphoFeatures feats, String spec) {
String[] toks = spec.split("\\-+");
String morphStr;
if(toks.length == 3 && toks[0].equals("PRO") && toks[2].equals("neg"))
morphStr = toks[1];
else
morphStr = toks[toks.length-1];
//wsg2011: The analyses have mixed casing....
morphStr = morphStr.toLowerCase();
if(isActive(MorphoFeatureType.GEN)) {
if(morphStr.contains("m"))
feats.addFeature(MorphoFeatureType.GEN, genVals[0]);
else if(morphStr.contains("f"))
feats.addFeature(MorphoFeatureType.GEN, genVals[1]);
}
if(isActive(MorphoFeatureType.PER)) {
if(morphStr.contains("1"))
feats.addFeature(MorphoFeatureType.PER, perVals[0]);
else if(morphStr.contains("2"))
feats.addFeature(MorphoFeatureType.PER, perVals[1]);
else if(morphStr.contains("3"))
feats.addFeature(MorphoFeatureType.PER, perVals[2]);
}
if(isActive(MorphoFeatureType.NUM)) {
if(morphStr.contains("s"))
feats.addFeature(MorphoFeatureType.NUM, numVals[0]);
else if(morphStr.contains("p"))
feats.addFeature(MorphoFeatureType.NUM, numVals[1]);
}
}
/**
* For debugging
*
* @param args
*/
public static void main(String[] args) {
if(args.length != 1) {
System.err.printf("Usage: java %s file%n", SpanishMorphoFeatureSpecification.class.getName());
System.exit(-1);
}
try {
BufferedReader br = new BufferedReader(new FileReader(args[0]));
MorphoFeatureSpecification mfs = new SpanishMorphoFeatureSpecification();
//Activate all features for debugging
mfs.activate(MorphoFeatureType.GEN);
mfs.activate(MorphoFeatureType.NUM);
mfs.activate(MorphoFeatureType.PER);
for(String line; (line = br.readLine()) != null;) {
MorphoFeatures feats = mfs.strToFeatures(line);
System.out.printf("%s\t%s%n", line.trim(),feats.toString());
}
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}