TigerReader.java
4.41 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
/**
*
*/
package is2.io;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
import is2.data.PSTree;
/**
* @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
*/
@Override
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;
}
}