-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.java
More file actions
386 lines (326 loc) · 9 KB
/
Copy pathParser.java
File metadata and controls
386 lines (326 loc) · 9 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
package parsing;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.io.*;
public class Parser {
public Map<String, Integer> cat2num = new HashMap<String, Integer>();
public Map<Integer, String> num2cat = new HashMap<Integer, String>();
// parent --> child --> count
public Map<Integer, Map<Integer, Integer>> uCountsb =
new HashMap<Integer, Map<Integer, Integer>>();
// right --> left --> par --> count
public Map<Integer, Map<Integer, Map<Integer, Integer>>> biCountsc =
new HashMap<Integer, Map<Integer, Map<Integer, Integer>>>();
// child --> parent --> count
public Map<Integer, Map<Integer, Double>> uRho =
new HashMap<Integer, Map<Integer, Double>>();
// right --> left --> parent --> count
public Map<Integer, Map<Integer, Map<Integer, Double>>> biRho =
new HashMap<Integer, Map<Integer, Map<Integer, Double>>>();
public Map<Integer, Integer> totalRules = new HashMap<Integer, Integer>();
/**
*
* @param file
*/
public void getCounts(String file){
try{
BufferedReader r = new BufferedReader(new FileReader(file));
String line = r.readLine();
String[] split;
int code = 0;
int count;
Integer lastTotal;
while (line != null){
split = line.split(" ");
count = Integer.parseInt(split[0]);
Integer left = cat2num.get(split[3]);
if (left == null){
left = code;
this.cat2num.put(split[3], code);
this.num2cat.put(code, split[3]);
code++;
}
Integer parent = cat2num.get(split[1]);
if (parent == null){
parent = code;
this.cat2num.put(split[1], code);
this.num2cat.put(code, split[1]);
code++;
}
lastTotal = this.totalRules.get(parent);
if (lastTotal == null){lastTotal = 0;}
this.totalRules.put(parent, lastTotal + count);
if (split.length == 4){
Map<Integer, Integer> leftToCount = this.uCountsb.get(parent);
if (leftToCount == null){leftToCount = new HashMap<Integer, Integer>();}
leftToCount.put(left, count);
this.uCountsb.put(parent, leftToCount);
} else {
Integer right = cat2num.get(split[4]);
if (right == null){
right = code;
this.cat2num.put(split[4], right);
this.num2cat.put(right, split[4]);
code++;
}
Map<Integer, Map<Integer, Integer>> leftToPar = this.biCountsc.get(right);
if (leftToPar == null){leftToPar = new HashMap<Integer, Map<Integer, Integer>>();}
Map<Integer, Integer> parToCount = leftToPar.get(left);
if (parToCount == null){parToCount = new HashMap<Integer, Integer>();}
parToCount.put(parent, count);
leftToPar.put(left, parToCount);
this.biCountsc.put(right, leftToPar);
}
line = r.readLine();
}
} catch (IOException e){
e.printStackTrace();
}
}
/**
* calculate rho values based on counts
*/
public void getRho(){
double newVal;
double parentTotal;
/**
* unary rhos
*/
for (int parent: this.uCountsb.keySet()){
parentTotal = this.totalRules.get(parent).doubleValue();
for (int left: this.uCountsb.get(parent).keySet()){
Map<Integer, Double> parentToRho = this.uRho.get(left);
if (parentToRho == null){parentToRho = new HashMap<Integer, Double>();}
newVal = this.uCountsb.get(parent).get(left).doubleValue() / parentTotal;
parentToRho.put(parent, newVal);
this.uRho.put(left, parentToRho);
}
}
/**
* binary rhos
*/
for (int right: this.biCountsc.keySet()){
for (int left: this.biCountsc.get(right).keySet()){
for (int par: this.biCountsc.get(right).get(left).keySet()){
if (this.biRho.get(right) == null){
this.biRho.put(right, new HashMap<Integer, Map<Integer, Double>>());
}
if (this.biRho.get(right).get(left) == null){
this.biRho.get(right).put(left, new HashMap<Integer, Double>());
}
double rho = this.biCountsc.get(right).get(left).get(par).doubleValue() / this.totalRules.get(par);;
this.biRho.get(right).get(left).put(par, rho);
}
}
}
}
/**
* CONSTITUENT class
*
*/
private class Constituent{
public int label;
public Constituent left;
public Constituent right;
public double mu;
public Constituent(int label, Constituent left, Constituent right, double mu){
this.label = label;
this.left = left;
this.right = right;
this.mu = mu;
}
/**
* recursively build parentheses notation version of tree structures
*/
@Override
public String toString(){
String toReturn;
String label = num2cat.get(this.label);
if (this.left == null){
toReturn = label;
} else if(this.right == null){
toReturn = "(" + label + " " + this.left.toString() + ")";
} else {
toReturn = "(" + label + " " + this.left.toString() + " " + this.right.toString() + ")";
}
return toReturn;
}
}
/**
* basically a wrapper for all the constituents.
*
* I hash every constituent by its parent label, since there should only be one
* constituent per category label.
*
*/
private class Cell{
public Map<Integer, Constituent> cell;
public Cell(){
this.cell = new HashMap<Integer, Constituent>();
}
}
/**
* square array
*
*/
private class Chart{
public Cell[][] chart;
public Chart(int N){
chart = new Cell[N][N+1];
}
}
/**
*
* builds the chart for a sentence
*
*/
public Chart parse(String sentence){
String[] split = sentence.split(" ");
int L = split.length;
if(L > 25){
return null;
}
List<Integer> words = new ArrayList<Integer>(L);
for (String w: split){
words.add(this.cat2num.get(w));
}
Chart C = new Chart(L);
for (int l=1; l<=L; l++){
for (int s=0; s<=L-l; s++){
C.chart[s][s+l] = new Cell();
this.fill(C.chart[s][s+l], s, s+l, C, words);
}
}
return C;
}
/**
*
* just writes the toString() for a chart to a file
*
*/
public void writeChart(Chart C, BufferedWriter w){
try{
if (C == null){
w.write("*IGNORE*");
} else {
Constituent top = C.chart[0][C.chart.length-1].cell.get(this.cat2num.get("TOP"));
if (top == null){
} else {
w.write(top.toString());
}
}
} catch (IOException e){
e.printStackTrace();
}
}
/**
*
* see the book
*
*/
public void fill(Cell c, int i, int k, Chart C, List<Integer> words){
if (k == i+1){
Constituent word = new Constituent(words.get(i), null, null, 1);
c.cell.put(words.get(i), word);
} else {
for (int j=i+1; j<=k-1; j++){
for (Constituent c2: C.chart[j][k].cell.values()){
Map<Integer, Map<Integer, Double>> leftToParent = this.biRho.get(c2.label);
if (leftToParent != null){
for (Integer left: leftToParent.keySet()){
Constituent c1 = C.chart[i][j].cell.get(left);
if (c1 != null){
Map<Integer, Double> parentToRho = leftToParent.get(left);
if (leftToParent != null){
for (int parent: parentToRho.keySet()){
Constituent newc = new Constituent(
parent, c1, c2, Math.log(parentToRho.get(parent))+c1.mu+c2.mu);
Constituent oldc = c.cell.get(parent);
if (oldc != null){
if (newc.mu > oldc.mu){
c.cell.put(parent, newc);
}
} else {
c.cell.put(parent, newc);
}
}
}
}
}
}
}
}
}
this.unary(c);
}
/**
* keep checking the whole cell for possible new unary constituents until there aren't any more
*
*/
public void unary(Cell c){
boolean changing = true;
while (changing){
Constituent newc = this.unaryHelp(c);
if (newc == null){
changing = false;
} else {
changing = true;
c.cell.put(newc.label, newc);
}
}
}
/**
* Check the whole cell for new unary constituents until you find one, then return it
*
*/
public Constituent unaryHelp(Cell c){
Map<Integer, Constituent> news = new HashMap<Integer, Constituent>();
for (int label: c.cell.keySet()){
Map<Integer, Double> parentToRho = this.uRho.get(label);
if (parentToRho != null){
for (int parent: parentToRho.keySet()){
Constituent daughter = c.cell.get(label);
Constituent newc = new Constituent(
parent, daughter, null, Math.log(parentToRho.get(parent))+daughter.mu);
Constituent oldc = c.cell.get(parent);
if (oldc == null || oldc.mu < newc.mu){
return newc;
}
}
}
}
return null;
}
/**
* read file, parse, and write line by line
*
*/
public void parseFile(String input, String output){
try{
BufferedReader r = new BufferedReader(new FileReader(input));
BufferedWriter w = new BufferedWriter(new FileWriter(output));
String line = r.readLine();
while (line != null){
Chart C = this.parse(line);
this.writeChart(C, w);
w.write("\n");
line = r.readLine();
}
r.close();
w.close();
} catch (IOException e){
e.printStackTrace();
}
}
public static void main(String[] args){
String rules = args[0];
String sentences = args[1];
String output = args[2];
Parser p = new Parser();
p.getCounts(rules);
p.getRho();
p.parseFile(sentences, output);
}
}