CONLLReader09.java
8.64 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package is2.io;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import is2.data.Instances;
import is2.data.SentenceData09;
import is2.util.DB;
/**
* This class reads files in the CONLL-09 format.
*
* @author Bernd Bohnet
*/
public class CONLLReader09 extends IOGenerals {
private BufferedReader inputReader;
public static final boolean NORMALIZE = true;
public static final boolean NO_NORMALIZE = false;
public boolean normalizeOn = true;
static public String joint = "";
private int format = 0;
private int lineNumber = 0;
public CONLLReader09(boolean normalize) {
normalizeOn = normalize;
}
public CONLLReader09(String file) {
lineNumber = 0;
try {
inputReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"), 32768);
} catch (Exception e) {
e.printStackTrace();
}
}
public CONLLReader09(String file, boolean normalize) {
this(file);
normalizeOn = normalize;
}
/**
* Sets the input format:
*
* CONLL09 is standard, ONE_LINE
*
* @param format
* the fomrat (see the constants starting with F_).
*/
public void setInputFormat(int format) {
this.format = format;
}
/**
*
*/
public CONLLReader09() {
}
/**
* @param testfile
* @param formatTask
*/
public CONLLReader09(String testfile, int formatTask) {
this(testfile);
}
public void startReading(String file) {
lineNumber = 0;
try {
inputReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"), 32768);
} catch (Exception e) {
e.printStackTrace();
}
}
public SentenceData09 getNext() {
if (F_ONE_LINE == format)
return getNextOneLine();
else
return getNextCoNLL09();
}
/**
* @return
*/
private SentenceData09 getNextOneLine() {
String line = null;
int i = 0;
try {
line = inputReader.readLine();
lineNumber++;
if (line == null) {
inputReader.close();
return null;
}
String[] tokens = line.split(" ");
int length = tokens.length;
if (line.isEmpty())
length = 0;
SentenceData09 it = new SentenceData09();
it.forms = new String[length + 1];
it.plemmas = new String[length + 1];
// it.ppos = new String[length+1];
it.gpos = new String[length + 1];
it.labels = new String[length + 1];
it.heads = new int[length + 1];
it.pheads = new int[length + 1];
it.plabels = new String[length + 1];
it.ppos = new String[length + 1];
it.lemmas = new String[length + 1];
it.fillp = new String[length + 1];
it.feats = new String[length + 1][];
it.ofeats = new String[length + 1];
it.pfeats = new String[length + 1];
it.id = new String[length + 1];
it.forms[0] = ROOT;
it.plemmas[0] = ROOT_LEMMA;
it.fillp[0] = "N";
it.lemmas[0] = ROOT_LEMMA;
it.gpos[0] = ROOT_POS;
it.ppos[0] = ROOT_POS;
it.labels[0] = NO_TYPE;
it.heads[0] = -1;
it.plabels[0] = NO_TYPE;
it.pheads[0] = -1;
it.ofeats[0] = NO_TYPE;
it.id[0] = "0";
// root is 0 therefore start with 1
for (i = 1; i <= length; i++) {
it.id[i] = "" + i;
it.forms[i] = this.normalizeOn ? normalize(tokens[i - 1]) : tokens[i - 1]; // normalize(
}
return it;
} catch (Exception e) {
System.out.println("\n!!! Error in input file sentence before line: " + lineNumber + " (in sentence line "
+ i + " ) " + e.toString());
e.printStackTrace();
// throw new Exception();
return null;
}
}
/**
* i.forms[heads[l]-1]+" "+rel+" "+ Read a instance
*
* @return a instance
* @throws Exception
*/
public SentenceData09 getNextCoNLL09() {
String line = null;
int i = 0;
try {
ArrayList<String[]> lineList = new ArrayList<String[]>();
line = inputReader.readLine();
lineNumber++;
while (line != null && line.length() == 0) {
line = inputReader.readLine();
lineNumber++;
System.out.println("skip empty line at line " + lineNumber);
}
while (line != null && line.length() != 0 && !line.startsWith(STRING) && !line.startsWith(REGEX)) {
lineList.add(line.split(REGEX));
line = inputReader.readLine();
lineNumber++;
}
int length = lineList.size();
if (length == 0) {
inputReader.close();
return null;
}
SentenceData09 it = new SentenceData09();
it.forms = new String[length + 1];
it.plemmas = new String[length + 1];
// it.ppos = new String[length+1];
it.gpos = new String[length + 1];
it.labels = new String[length + 1];
it.heads = new int[length + 1];
it.pheads = new int[length + 1];
it.plabels = new String[length + 1];
it.ppos = new String[length + 1];
it.lemmas = new String[length + 1];
it.fillp = new String[length + 1];
it.feats = new String[length + 1][];
it.ofeats = new String[length + 1];
it.pfeats = new String[length + 1];
it.id = new String[length + 1];
it.forms[0] = ROOT;
it.plemmas[0] = ROOT_LEMMA;
it.fillp[0] = "N";
it.lemmas[0] = ROOT_LEMMA;
it.gpos[0] = ROOT_POS;
it.ppos[0] = ROOT_POS;
it.labels[0] = NO_TYPE;
it.heads[0] = -1;
it.plabels[0] = NO_TYPE;
it.pheads[0] = -1;
it.ofeats[0] = NO_TYPE;
it.id[0] = "0";
// root is 0 therefore start with 1
for (i = 1; i <= length; i++) {
String[] info = lineList.get(i - 1);
it.id[i] = info[0];
it.forms[i] = info[1]; // normalize(
if (info.length < 3)
continue;
it.lemmas[i] = info[2];
it.plemmas[i] = info[3];
it.gpos[i] = info[4];
if (info.length < 5)
continue;
it.ppos[i] = info[5];// .split("\\|")[0];
// feat 6
// now we try underscore
it.ofeats[i] = info[6].equals(CONLLWriter09.DASH) ? "_" : info[6];
if (info[7].equals(CONLLWriter09.DASH))
it.feats[i] = null;
else {
it.feats[i] = info[7].split(PIPE);
it.pfeats[i] = info[7];
}
if (info[8].equals(US))
it.heads[i] = -1;
else
it.heads[i] = Integer.parseInt(info[8]);// head
it.pheads[i] = info[9].equals(US) ? it.pheads[i] = -1 : Integer.parseInt(info[9]);// head
it.labels[i] = info[10];
it.plabels[i] = info[11];
it.fillp[i] = info[12];
if (info.length > 13) {
if (!info[13].equals(US))
it.addPredicate(i, info[13]);
for (int k = 14; k < info.length; k++)
it.addArgument(i, k - 14, info[k]);
}
}
return it;
} catch (Exception e) {
System.out.println("\n!!! Error in input file sentence before line: " + lineNumber + " (in sentence line "
+ i + " ) " + e.toString());
e.printStackTrace();
System.exit(0);
// throw new Exception();
return null;
}
}
/**
* Read a instance an store it in a compressed format
*
* @param is
* @return
* @throws IOException
*/
final public SentenceData09 getNext(Instances is) {
SentenceData09 it = getNext();
if (is != null)
insert(is, it);
return it;
}
final public boolean insert(Instances is, SentenceData09 it) {
try {
if (it == null) {
inputReader.close();
return false;
}
int i = is.createInstance09(it.length());
for (int p = 0; p < it.length(); p++) {
is.setForm(i, p, normalize(it.forms[p]));
// is.setFormOrg(i, p, it.forms[p]);
is.setGPos(i, p, it.gpos[p]);
// System.out.println(""+is.gpos[i][p]);
if (it.ppos[p] == null || it.ppos[p].equals(US)) {
is.setPPoss(i, p, it.gpos[p]);
} else
is.setPPoss(i, p, it.ppos[p]);
if (it.plemmas[p] == null || it.plemmas[p].equals(US)) {
is.setLemma(i, p, normalize(it.forms[p]));
} else
is.setLemma(i, p, normalize(it.plemmas[p]));
if (it.lemmas != null)
if (it.lemmas[p] == null) { // ||it.org_lemmas[p].equals(US)
// that harms a lot the
// lemmatizer
is.setGLemma(i, p, it.plemmas[p]);
} else
is.setGLemma(i, p, it.lemmas[p]);
if (it.feats != null && it.feats[p] != null)
is.setFeats(i, p, it.feats[p]);
if (it.ofeats != null)
is.setFeature(i, p, it.ofeats[p]);
if (it.pfeats != null)
is.setPFeature(i, p, it.pfeats[p]);
is.setRel(i, p, it.labels[p]);
if (it.plabels != null)
is.setPRel(i, p, it.plabels[p]);
is.setHead(i, p, it.heads[p]);
if (it.pheads != null)
is.setPHead(i, p, it.pheads[p]);
if (it.fillp != null && it.fillp[p] != null && it.fillp[p].startsWith("Y"))
is.pfill[i].set(p);
else
is.pfill[i].clear(p);
}
if (is.createSem(i, it)) {
DB.println("count " + i + " len " + it.length());
DB.println(it.printSem());
}
} catch (Exception e) {
DB.println("head " + it);
e.printStackTrace();
}
return true;
}
public String normalize(String s) {
if (!normalizeOn)
return s;
if (s.matches(NUMBER))
return NUM;
return s;
}
}