TigerReader.java
8.88 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
/**
*
*/
package is2.io;
import is2.data.PSTree;
import is2.util.DB;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Stack;
import java.util.StringTokenizer;
/**
* @author Dr. Bernd Bohnet, 17.01.2011
*
* Reads a sentences in Penn Tree Bank bracket style and return sentences.
*/
public class TigerReader implements PSReader {
BufferedReader inputReader;
ArrayList<File> psFiles = new ArrayList<File>();
ArrayList<PSTree> psCache = new ArrayList<PSTree>();
String filter[] = null;
int startFilter =-1;
int endFilter =-1;
public TigerReader() {}
public TigerReader(String file ) {
try {
inputReader = new BufferedReader(new InputStreamReader(new FileInputStream(file),"ISO-8859-1"),32768);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param ps
*/
@Override
public void startReading(String file, String[] filter) {
try {
this.filter =filter;
startFilter =filter==null?-1:1;
endFilter =filter==null?-1:1;
inputReader = new BufferedReader(new InputStreamReader(new FileInputStream(file),"ISO-8859-1"),32768);
} catch (Exception e) {
e.printStackTrace();
}
}
public static class Line {
String form;
String lemma;
String morph;
String pos;
int parent;
String edge;
}
static int stop=0;
/**
* @return
*/
public PSTree getNext() {
PSTree ps = null;
String l =null;
ArrayList<Line> lines = new ArrayList<Line>();
try {
int state=1, terminals=0, nonterminals=0;
while((l = inputReader.readLine())!=null) {
if (startFilter==1 && l.startsWith("#BOS "+filter[0]) ) {
System.out.println("found start "+l);
startFilter=2;
}
if (endFilter==1 && l.startsWith("#EOS "+filter[1]) ){
System.out.println("found end "+l);
endFilter=2;
}
if (startFilter==1||endFilter==2) continue;
if (l.startsWith("#BOS")) {
state=2;
continue;
}
if (l.startsWith("#500")) state=3;
if (l.startsWith("#EOS")) state=4;
if (state<2) continue;
if ( state==4) {
ps = new PSTree();
ps.create(terminals, nonterminals);
// System.out.println("terminals "+terminals);
//build ps tree
int cnt=0;
// ps.entries[0] =CONLLReader09.ROOT;
// ps.head[0]=-1;
int root=-1;
for(Line line : lines) {
/* if (cnt==terminals) {
// insert root
root =cnt;
cnt++;
}
*/
ps.entries[cnt] = line.form;
if (cnt<terminals) ps.pos[cnt] = line.pos;
else ps.entries[cnt] =line.pos;
ps.lemmas[cnt] = line.lemma;
ps.head[cnt] = line.parent==0?lines.size()-1:line.parent>=500?line.parent-500+terminals:line.parent;
// ps.head[cnt] = line.parent==0?lines.size()-1:line.parent>=500?line.parent-500+terminals:line.parent;
ps.morph[cnt]=line.morph;
cnt++;
}
if (root==-1) root= terminals;
ps.head[cnt-1]=0; // root
ps.terminalCount=terminals;
lines.clear();
state=1;
/*
for(int k=0;k<ps.head.length;k++) {
if (ps.head[k]<terminals && k!=root) {
ps.head[k]=root;
// DB.println("error "+k+" "+ps.head[k]);
}
}
*/
// System.out.println(""+ps.toString());
// if (stop++ == 4)System.exit(0);
return ps;
}
StringTokenizer t = new StringTokenizer(l,"\t");
int tc=0;
Line line = new Line();
lines.add(line);
while(t.hasMoreTokens()) {
String token = t.nextToken();
if (token.equals("\t"))continue;
if (tc==0) {
if (token.startsWith("#5")||token.startsWith("#6") ) {
nonterminals++;
}
else {
terminals++;
//change it back to the wrong format since the conll stuff was derived from this.
// if (token.equals("durchblicken")) token="durchblikken";
line.form = token;
}
} else if (tc==1) {
line.lemma=token;
} else if (tc==2) {
line.pos=token;
} else if (tc==3) {
line.morph=token;
} else if (tc==4) {
line.edge=token;
} else if (tc==5) {
line.parent=Integer.parseInt(token);
}
if (token.length()>0)tc++;
}
// read till #EOS
}
} catch(Exception e) {
e.printStackTrace();
}
return ps;
}
/**
* @param tree
*/
private void removeTraces(ArrayList<Object> tree) {
Stack<ArrayList<Object>> s = new Stack<ArrayList<Object>>();
s.push(tree);
ArrayList<Object> list =null;
while (!s.isEmpty()) {
ArrayList<Object> last =list;
list = s.pop();
for(int k=0;k<list.size();k++) {
Object o = list.get(k);
if(o instanceof String) {
String t = (String)o;
if ((t.endsWith("-1")||t.endsWith("-2")||t.endsWith("-3")||t.endsWith("-4")) && list.size()>(k+1)) {
t = t.substring(0, t.length()-2);
list.set(k, t);
}
if (t.startsWith("-NONE-")) {
// remove the bigger surrounding phrase, e.g. (NP (-NONE- *))
if (last.size()==2 && last.get(0) instanceof String && last.contains(list)) {
ArrayList<Object> rest = remove(tree, last);
if (rest!=null && rest.size()==1){
rest = remove(tree, rest);
}
}
// remove the phrase only, e.g. (NP (AP nice small) (-NONE- *))
else {
// there might a phrase with two empty elements (VP (-NONE- *) (-NONE- ...))
// System.out.println("last "+last+" list "+list );
ArrayList<Object> rest = remove(tree, list);
removeTraces(rest);
if (rest.size()==1) {
rest = remove(tree, rest);
if (rest!=null && rest.size()==1){
System.out.println("rest "+rest);
System.exit(0);
}
}
}
continue;
}
}
if (o instanceof ArrayList) {
s.push((ArrayList<Object>)o);
}
}
}
}
/**
* Remove from tree p
* @param tree phrase structure tree
* @param p elment to remove
*/
private ArrayList<Object> remove(ArrayList<Object> tree, Object p) {
Stack<ArrayList<Object>> s = new Stack<ArrayList<Object>>();
s.push(tree);
while (!s.isEmpty()) {
ArrayList<Object> list = s.pop();
for(int k=0;k<list.size();k++) {
Object o = list.get(k);
if (o == p) {
list.remove(p);
return list ;
}
if (o instanceof ArrayList) {
s.push((ArrayList<Object>)o);
}
}
}
return null;
}
/**
* Count the terminals
* @param current
* @return
*/
private int countTerminals(ArrayList<Object> current) {
int count =0;
boolean found =false, all =true ;
for(Object o : current) {
if (o instanceof String) found =true;
else {
all =false;
if (o instanceof ArrayList) count +=countTerminals((ArrayList<Object>)o);
}
}
if (found && all) {
// System.out.println(""+current);
count++;
}
return count;
}
/**
* Count the terminals
* @param current
* @return
*/
private int insert(PSTree ps, ArrayList<Object> current, Integer terminal, Integer xxx, int head) {
boolean found =false, all =true;
String term =null;
String pos =null;
for(Object o : current) {
if (o instanceof String) {
if (found) term =(String)o;
if (!found) pos =(String)o;
found =true;
} else {
all =false;
// if (o instanceof ArrayList) count +=countTerminals((ArrayList<Object>)o);
}
}
if (found && all) {
if(term.equals("-LRB-")) term="(";
if(term.equals("-RRB-")) term=")";
if(term.equals("-LCB-")) term="{";
if(term.equals("-RCB-")) term="}";
if(term.contains("1\\/2-year")) term=term.replace("\\/", "/");
if(term.contains("1\\/2-foot-tall")) term=term.replace("\\/", "/");
ps.entries[ps.terminalCount] =term;
ps.pos[ps.terminalCount]=pos;
ps.head[ps.terminalCount]=head;
// System.out.println("terminal "+term+" "+ps.terminal+" head "+head);
ps.terminalCount ++;
} else if (found && ! all) {
if(pos.startsWith("NP-SBJ")) pos="NP-SBJ";
if(pos.startsWith("WHNP")) pos="WHNP";
ps.entries[ps.non] =pos;
ps.head[ps.non]=head;
// System.out.println("non terminal "+pos+" "+ps.non+" head "+ head);
int non =ps.non ++;
for (Object o : current) {
if (o instanceof ArrayList) {
insert(ps,(ArrayList<Object>)o,terminal,ps.non, non);
}
}
}
if(!all && !found)for (Object o : current) {
if (o instanceof ArrayList) {
insert(ps,(ArrayList<Object>)o,terminal,0, ps.non-1);
}
}
return terminal;
}
/**
* Count the terminals
* @param current
* @return
*/
private int countNonTerminals(ArrayList<Object> current) {
int count =0;
boolean found =false, all =true ;
for(Object o : current) {
if (o instanceof String) found =true;
else {
all =false;
if (o instanceof ArrayList) count +=countNonTerminals((ArrayList<Object>)o);
}
}
if (found && !all) count++;
return count;
}
}