CONLLWriter09.java
7.63 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
package is2.io;
import is2.data.SentenceData09;
import is2.util.DB;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.StringTokenizer;
public class CONLLWriter09 extends IOGenerals {
int format =0;
public static final String DASH = "_";
public static final boolean NO_ROOT = true, ROOT = false;
protected BufferedWriter writer;
public CONLLWriter09 () {
try {
writer = new BufferedWriter(new OutputStreamWriter(System.out,"UTF8"));
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[]) throws IOException {
if (args.length==2) {
File f = new File(args[0]);
File f2 = new File(args[1]);
BufferedReader ir = new BufferedReader(new InputStreamReader(new FileInputStream(f),"UTF-8"),32768);
BufferedWriter br = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f2),"UTF8"));;
boolean found =false;
boolean tab =false;
while(true) {
String l = ir.readLine();
if (l==null) break;
String x =l.trim();
if (x.endsWith("\t")) tab=true;
br.write(x);
br.newLine();
if (!l.equals(x)) found =true;
}
ir.close();
br.flush();
br.close();
if (found) DB.println("found diff. found tab? "+tab);
} else if (args.length==3) {
File f1 = new File(args[1]);
File f2 = new File(args[2]);
BufferedReader ir1 = new BufferedReader(new InputStreamReader(new FileInputStream(f1),"UTF-8"),32768);
BufferedReader ir2 = new BufferedReader(new InputStreamReader(new FileInputStream(f2),"UTF-8"),32768);
int line =0, alltabs1=0,alltabs2=0;
while(true) {
String l1 = ir1.readLine();
String l2 = ir2.readLine();
if (l1==null && l2!=null) DB.println("files do not end at the same line ");
if (l1!=null && l2==null) DB.println("files do not end at the same line ");
if (l1==null ) break;
StringTokenizer t1 = new StringTokenizer(l1,"\t");
StringTokenizer t2 = new StringTokenizer(l2,"\t");
int tabs1=0;
while(t1.hasMoreTokens()) {
t1.nextElement();
tabs1++;
alltabs1++;
}
int tabs2=0;
while(t2.hasMoreTokens()) {
t2.nextElement();
tabs2++;
alltabs2++;
}
line ++;
if (tabs1!=tabs2) {
DB.println("number of tabs different in line "+line+" file1-tabs "+tabs1+" file2-tabs "+tabs2);
System.exit(0);
}
}
DB.println("checked lines "+line+" with tabs in file 1 "+alltabs1+" in file2 "+alltabs2);
} else {
File f = new File(args[0]);
String[] dir =f.list();
for(String fx :dir) {
BufferedReader ir = new BufferedReader(new InputStreamReader(new FileInputStream(args[0]+File.separatorChar+fx),"UTF-8"),32768);
System.out.println("check file "+fx);
while(true) {
String l = ir.readLine();
if (l==null) break;
if (l.endsWith("\t")) {
DB.println("found tab in file "+fx);
break;
}
}
ir.close();
}
}
}
public CONLLWriter09 (String file) {
try {
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),"UTF8"));
} catch (Exception e) {
e.printStackTrace();
}
}
public CONLLWriter09 (Writer writer) {
this.writer = new BufferedWriter(writer);
}
public CONLLWriter09(String outfile, int formatTask) {
this(outfile);
}
public void write(SentenceData09 inst) throws IOException {
write(inst, NO_ROOT);
}
/**
*
* @param inst
* @param root true: remove root node
* @throws IOException
*/
public void write(SentenceData09 inst, boolean root) throws IOException {
int i, mod;
if(root&&(inst.forms[0].startsWith("<root")||(inst.lemmas[0]!=null&&inst.lemmas[0].startsWith("<root")))){
i=1; mod=0;
} else {
i=0; mod=1;
}
//=()?1:0;
if (format == this.F_ONE_LINE) {
boolean first =true;
for (; i<inst.length(); i++) {
if (first ){
first=false;
} else writer.write(" ");
writer.write(inst.plemmas[i]);
}
writer.newLine();
return ;
}
for (; i<inst.length(); i++) {
if (inst.id==null|| inst.id[i]==null) {writer.write(Integer.toString(i+mod)); writer.write('\t');} // id
else { writer.write(inst.id[i]); writer.write('\t');}
writer.write(inst.forms[i]); writer.write('\t'); // form
if (inst.lemmas!=null && inst.lemmas[i]!=null) {
writer.write(inst.lemmas[i]);
}
else writer.write(DASH); // lemma
writer.write('\t');
if (inst.plemmas!=null && inst.plemmas[i]!=null) writer.write(inst.plemmas[i]);
else writer.write(DASH); // plemma
writer.write('\t');
if (inst.gpos[i]!=null) writer.write(inst.gpos[i]); // gpos
else writer.write(DASH);
writer.write('\t');
if (inst.ppos!=null && inst.ppos[i]!=null) writer.write(inst.ppos[i]);
else writer.write(DASH); // ppos
writer.write('\t');
if (inst.ofeats!=null&& inst.ofeats[i]!=null) writer.write(inst.ofeats[i]);
else writer.write(DASH);
writer.write('\t');
//writer.write(DASH); writer.write('\t'); // feat
if (inst.pfeats!=null&&inst.pfeats[i]!=null) {
//System.out.println(""+inst.pfeats[i]);
writer.write(inst.pfeats[i]);
}
else writer.write(DASH);
writer.write('\t');
writer.write(Integer.toString(inst.heads[i])); writer.write('\t'); // head
if (inst.pheads!=null ) writer.write(Integer.toString(inst.pheads[i]));
else writer.write(DASH);
writer.write('\t'); // phead
if (inst.labels[i]!=null) writer.write(inst.labels[i]); // rel
else writer.write(DASH);
writer.write('\t');
if (inst.plabels!=null &&inst.plabels[i]!=null) writer.write(inst.plabels[i]); // rel
else writer.write(DASH);
writer.write('\t');
if (inst.fillp!=null && inst.fillp[i]!=null) writer.write(inst.fillp[i]); // fill p
else {
writer.write(DASH);
}
// writer.write('\t');
if (inst.sem==null) {
writer.write('\t');
writer.write(DASH);
} else {
boolean foundPred =false;
// print the predicate
for (int p =0;p< inst.sem.length;p++) {
if (inst.semposition[p]==i) {
foundPred=true;
// System.out.println("write pred "+inst.sem[p] );
writer.write('\t'); writer.write(inst.sem[p]);
// if (inst.sem[p].startsWith(".")) DB.println("error "+inst.sem[p]);
}
}
if (!foundPred ) {
writer.write('\t');
writer.write(DASH);
// writer.write('\t');
// writer.write(DASH);
}
// print the arguments
for (int p =0;p< inst.sem.length;p++) {
boolean found =false;
if (inst.arg!=null &&inst.arg.length>p&&inst.arg[p]!=null)
for(int a = 0; a<inst.arg[p].length;a++) {
if (i==inst.argposition[p][a]) {
writer.write('\t'); writer.write(inst.arg[p][a]);
found = true;
break;
}
}
if (!found) {
writer.write('\t');
writer.write(DASH);
}
}
}
writer.newLine();
}
writer.newLine();
writer.flush();
}
public void finishWriting () throws IOException {
writer.flush();
writer.close();
}
/**
* Sets the output format such as CoNLL or one line for the lemmata of the sentence (see F_xxxx constants).
* @param formatTask
*/
public void setOutputFormat(int formatTask) {
format =formatTask;
}
}