-
Notifications
You must be signed in to change notification settings - Fork 1
/
AccurateUnaries.java
355 lines (298 loc) · 8.82 KB
/
AccurateUnaries.java
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
/**
*
*/
package edu.berkeley.nlp.PCFGLA;
import java.lang.Math;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.PrintStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;
import java.util.List;
import java.util.LinkedList;
import java.util.Map;
import java.util.HashMap;
import edu.berkeley.nlp.PCFGLA.Corpus.TreeBankType;
import edu.berkeley.nlp.PCFGLA.Corpus;
import edu.berkeley.nlp.ling.StateSet;
import edu.berkeley.nlp.ling.Tree;
import edu.berkeley.nlp.ling.Trees.PennTreeReader;
import edu.berkeley.nlp.util.Numberer;
import edu.berkeley.nlp.util.Option;
import edu.berkeley.nlp.util.OptionParser;
import edu.berkeley.nlp.util.CounterMap;
import edu.berkeley.nlp.util.Counter;
import edu.berkeley.nlp.util.ScalingTools;
public class AccurateUnaries
{
Grammar grammar;
SophisticatedLexicon lexicon;
ArrayParser labeler;
Numberer tagNumberer;
Binarization binarization;
CounterMap<String, String> unaryTable;
public static class Options {
@Option(name = "-gr", required = true, usage = "Input File for Grammar (Required)\n")
public String inFileName;
@Option(name = "-inputFile", usage = "Read input from this file instead of reading it from STDIN.")
public String inputFile;
@Option(name="-out", usage="Stem of filename for output files.")
public String outName;
}
public AccurateUnaries(Grammar grammar, SophisticatedLexicon lexicon,
int labelLevel, Binarization bin)
{
unaryTable = new CounterMap<String, String>();
if (labelLevel==-1)
{
this.grammar = grammar.copyGrammar(false);
this.lexicon = lexicon.copyLexicon();
}
else
{ // need to project
int[][] fromMapping = grammar.computeMapping(1);
int[][] toSubstateMapping =
grammar.computeSubstateMapping(labelLevel);
int[][] toMapping =
grammar.computeToMapping(labelLevel,toSubstateMapping);
double[] condProbs =
grammar.computeConditionalProbabilities(fromMapping,toMapping);
this.grammar =
grammar.projectGrammar(condProbs,
fromMapping,toSubstateMapping);
this.lexicon =
lexicon.projectLexicon(condProbs,
fromMapping,toSubstateMapping);
this.grammar.splitRules();
double filter = 1.0e-10;
this.grammar.removeUnlikelyRules(filter,1.0);
this.lexicon.removeUnlikelyTags(filter,1.0);
}
this.labeler = new ArrayParser(this.grammar, this.lexicon);
this.tagNumberer = Numberer.getGlobalNumberer("tags");
this.binarization = bin;
}
private void record(Tree<StateSet> stateSetTree)
{
try
{
labeler.doInsideOutsideScores(stateSetTree, false, false);
}
catch(Exception e)
{
System.err.println("Fail: "+stateSetTree);
System.err.println(e);
throw new RuntimeException(e);
}
// System.err.println(stateSetTree);
double sentenceScore = stateSetTree.getLabel().getIScore(0);
int sentenceScale = stateSetTree.getLabel().getIScale();
if(sentenceScore > 0)
{
addTreeCounts(stateSetTree, sentenceScore, sentenceScale);
}
else
{
System.err.println("Parse fail: "+stateSetTree);
}
}
private void addTreeCounts(Tree<StateSet> tree, double sentenceScore,
int sentenceScale)
{
List<Tree<StateSet>> children = tree.getChildren();
boolean unary = children.size() == 1;
if(tree.isLeaf())
{
return;
}
else if(tree.isPreTerminal())
{
//far as I know, preterms are pretty safe
}
else
{
//System.err.println("intermediate node");
if(unary)
{
StateSet currentState = tree.getLabel();
int nSubStates = currentState.numSubStates();
short tag = currentState.getState();
String label = (String)tagNumberer.object(tag);
double norm = 0;
for(short substate=0; substate<nSubStates; substate++)
{
String lhs = label + "_" + substate;
double score = currentState.getOScore(substate) *
currentState.getIScore(substate);
double scalingFactor =
ScalingTools.calcScaleFactor(currentState.getOScale() +
currentState.getIScale() -
sentenceScale);
double weight = (score * scalingFactor) / sentenceScore;
norm += weight;
StateSet childState = children.get(0).getLabel();
int nChildSubStates = childState.numSubStates();
short childTag = childState.getState();
String childLabel = (String)tagNumberer.object(childTag);
for(short childSubstate=0;
childSubstate<nChildSubStates; childSubstate++)
{
String rhs = childLabel + "_" + childSubstate;
double childScore =
childState.getOScore(childSubstate) *
childState.getIScore(childSubstate);
double childScalingFactor =
ScalingTools.calcScaleFactor(
childState.getOScale() +
childState.getIScale() -
sentenceScale);
double childWeight =
(childScore * childScalingFactor) /
sentenceScore;
Double totalWeight = new Double(weight * childWeight);
unaryTable.incrementCount(lhs, rhs, totalWeight);
if(totalWeight.isNaN())
{
throw new RuntimeException("Bad!");
}
}
}
if(Math.abs(1.0 - norm) > 1e-5)
{
throw new RuntimeException(
"Probabilities not properly normalized.");
}
}
//System.err.println("score "+sentenceScore+" "+
//sentenceScale+" norm "+norm);
}
//System.err.println(tree);
for (Tree<StateSet> child : children)
{
addTreeCounts(child, sentenceScore, sentenceScale);
}
}
void printUnaries(PrintStream out)
{
unaryTable.normalize();
for(String lhs : unaryTable.keySet())
{
Counter<String> vals = unaryTable.getCounter(lhs);
for(String val : vals.keySet())
{
Double dval = new Double(vals.getCount(val));
if(dval != 0 && !dval.isNaN())
{
out.println(dval + " " + lhs + " -> " + val);
}
}
}
}
/*
* Allocate the inside and outside score arrays for the whole tree
*/
static void allocate(Tree<StateSet> tree) {
tree.getLabel().allocate();
for (Tree<StateSet> child : tree.getChildren()) {
allocate(child);
}
}
public static void main(String[] args)
{
OptionParser optParser = new OptionParser(Options.class);
Options opts = (Options) optParser.parse(args, true);
// provide feedback on command-line arguments
System.err.println("Calling with " + optParser.getPassedInOptions());
String inFileName = opts.inFileName;
if (inFileName==null)
{
throw new Error("Did not provide a grammar.");
}
System.err.println("Loading grammar from "+inFileName+".");
ParserData pData = ParserData.Load(inFileName);
if (pData==null)
{
System.out.println("Failed to load grammar from file"+
inFileName+".");
System.exit(1);
}
System.err.println("Loaded.");
String outName = opts.outName;
System.err.println("Output to "+outName+".xxx");
Grammar grammar = pData.getGrammar();
grammar.splitRules();
SophisticatedLexicon lexicon = (SophisticatedLexicon)pData.getLexicon();
Numberer.setNumberers(pData.getNumbs());
Numberer tagNumberer = Numberer.getGlobalNumberer("tags");
int topLevel = 0;
for(int i=0; i<grammar.splitTrees.length; ++i)
{
int dep = grammar.splitTrees[i].getDepth();
if(dep > topLevel)
{
topLevel = dep;
}
}
topLevel -= 1;
System.err.println("Max level: "+topLevel);
for(int level = 0; level <= topLevel; ++level)
{
String useOutName = outName+"-lvl"+level;
AccurateUnaries acc =
new AccurateUnaries(grammar, lexicon, level, pData.bin);
short[] numSubstates = acc.grammar.numSubStates;
try
{
InputStreamReader inputData = (opts.inputFile==null) ? new InputStreamReader(System.in) : new InputStreamReader(new FileInputStream(opts.inputFile), "UTF-8");
PennTreeReader treeReader = new PennTreeReader(inputData);
int trees = 0;
Tree<String> tree = null;
while(treeReader.hasNext())
{
tree = treeReader.next();
if (tree.getYield().get(0).equals(""))
{
// empty tree -> parse failure
continue;
}
tree =
TreeAnnotations.processTree(
tree, pData.v_markov, pData.h_markov,pData.bin,false);
Tree<StateSet> stateSetTree =
StateSetTreeList.stringTreeToStatesetTree(
tree, numSubstates, false, tagNumberer);
allocate(stateSetTree);
acc.record(stateSetTree);
++trees;
}
System.err.println(trees+" trees read.");
}
catch(Exception ex)
{
ex.printStackTrace();
throw new RuntimeException(ex);
}
System.err.println("Probs computed.");
try
{
PrintStream output =
new PrintStream(
new GZIPOutputStream(
new FileOutputStream(
useOutName+".unaries.gz")));
acc.printUnaries(output);
output.println();
output.flush();
output.close();
}
catch(IOException e)
{
throw new RuntimeException(e);
}
}
System.exit(0);
}
}