-
Notifications
You must be signed in to change notification settings - Fork 0
/
nonTerminal.java
executable file
·297 lines (259 loc) · 9.56 KB
/
nonTerminal.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
package BMM_labels;
/*
* nonTerminal.java
*
* Created on 24 november 2005, 19:45
*/
/**
*
* @author peter
* @author gideon
*/
import java.util.*;
import BMM_labels.Utils;
/**
* a nonTerminal has a name, and a set of rules associated with it
* the rules have a fixed order, according to which the nonTerminal branches
* (expands) in parsing
* extends Constituent, so you can combine nonTerminals with Terminals
*/
public class nonTerminal extends Constituent implements Cloneable {
private String nonTerminalName;
private ArrayList<Rule> associatedRules;
private double logLikelihood = 0d;
private double logDirichletPrior = 0d;
private double logPoissonPart = 0d;
private int symbolsInRHS = 0;
private int nonTerminalsInRHS = 0;
private int totalRuleCount = 0;
/**
* Creates a new instance of nonTerminal with given capitalized name
* (at initialization of grammar: every word gets a category)
*/
public nonTerminal(String myName) {
this.nonTerminalName = myName;
this.associatedRules = new ArrayList<Rule>();
}
/**
* add a rule of the form X->YZ to nonTerminal, after chunking two nonTerminals together
*/
public Rule addRule(String cat1, String cat2) {
Rule mynewRule = new Rule(cat1, cat2);
this.associatedRules.add(mynewRule );
return mynewRule;
}
/**
* add a rule of the form X->a to nonTerminal
*/
public void addRule(String word) {
this.associatedRules.add(new Rule(word));
}
/**
* add a rule of the form X->any ArrayList of Constituents to nonTerminal
*/
public Rule addRule(ArrayList<String> constituentArray) {
Rule mynewRule = new Rule(constituentArray);
this.associatedRules.add(mynewRule);
return mynewRule;
}
/**
* iterates over all associated rules and substitutes constituents
*/
public void substituteConstituentsinRules(String oldConstituent, String newConstituent) {
for (Rule myRule : this.associatedRules) {
myRule.substituteConstituent( oldConstituent, newConstituent);
}
}
/**
* iterates over all associated rules and returns count of rules
*/
public int countRules() {
return this.associatedRules.size();
}
public void computeTotalRuleCount() {
int totalCountOfRules = 0;
for (Rule myRule : this.associatedRules) {
totalCountOfRules += myRule.getCount();
}
this.totalRuleCount = totalCountOfRules;
}
public int getTotalRuleCount() {
return this.totalRuleCount;
}
public Rule getRandomRule() {
// TODO: error if nrRule > totalRules
int randomRule = (int)(Math.random() * this.associatedRules.size());
return this.associatedRules.get(randomRule);
}
public ArrayList<Rule> getRules() {
return this.associatedRules;
}
public boolean removeDuplicateRules() {
HashSet<ArrayList<String>> mySet = new HashSet<ArrayList<String>>();
Rule tempRule = null;
ArrayList<Rule> duplicateRules = new ArrayList<Rule>();
int keepTrackOfHowManyRemoved = 0;
boolean blnRuleRemoved = false;
int ruleCount = 0;
for (ListIterator<Rule> it = this.associatedRules.listIterator(); it.hasNext();) {
tempRule = (Rule) it.next();
if (!mySet.add(tempRule.getRightHandSide())){
duplicateRules.add(tempRule);
blnRuleRemoved =true;
keepTrackOfHowManyRemoved ++;
// remove the rule from arraylist
it.remove();
}
}
if (Main.PRINT_REMOVED_RULES_TO_SCREEN && keepTrackOfHowManyRemoved > 0)
System.out.println("Removed " + keepTrackOfHowManyRemoved + " rules in nonT: " + this.getName());
// update te count of remaining rule in arraylist
StringBuffer rhsBuff ;
for (Rule myRule : this.associatedRules) {
// add myRule
for (Rule myDuplicateRule : duplicateRules) {
if (myRule.getRightHandSide().equals(myDuplicateRule.getRightHandSide())) {
myRule.increaseCount(myDuplicateRule.getCount());
// print myDuplicateRule
if (Main.PRINT_REMOVED_RULES_TO_SCREEN) {
rhsBuff = new StringBuffer();
rhsBuff.append(this.nonTerminalName).append("#");
for (String myWord : myRule.getRightHandSide()){
rhsBuff.append(myWord).append("#");
}
System.out.println(rhsBuff.toString().substring(0, rhsBuff.toString().length()-1) + "@Stolcke");
}
}
}
}
return blnRuleRemoved;
}
public int getCountOfSymbolsinRHS() {
return this.symbolsInRHS;
}
public int getCountOfNonTerminalsinRHS() {
return this.nonTerminalsInRHS;
}
public String getName() {
return this.nonTerminalName;
}
public void computePriorsAndLikelihoodForNonTerminal(Grammar myGrammar) {
this.computenrOfSymbolsinRHS();
this.computenrOfNonTerminalsInRHS(myGrammar);
if (Main.INCLUDE_POISSON) this.computePoissonPartOfStructurePrior();
if (Main.INCLUDE_DIRICHLET) this.computelogDirichlet();
this.computelogLikelihood();
}
public int computenrOfSymbolsinRHS() {
int totalConstituents = 0;
for(Rule myRule : this.associatedRules) {
totalConstituents += myRule.getRightHandSide().size();
}
this.symbolsInRHS = totalConstituents;
return totalConstituents;
}
public int computenrOfNonTerminalsInRHS(Grammar myGrammar) {
int totalNonTerminalsInRHS = 0;
for (Rule myRule : this.associatedRules) {
// check for every symbol of rule
for (String mySymbol : myRule.getRightHandSide()) {
if (!myGrammar.Terminals.containsKey(mySymbol)) totalNonTerminalsInRHS++;
}
// if size of rule = 1 you may assume it is terminal, except for
// when LHS=TOP, in which RHS of size 1 is nonT
}
this.nonTerminalsInRHS = totalNonTerminalsInRHS;
return totalNonTerminalsInRHS;
}
/**
* for every rule with k nonTerminals in RHS:
* 2log(Poisson(k;mu)) + k*2log(nrNonTerminalsInGrammar)
* for every lexical rule (only terminals in RHS):
* 2log(Grammar.nrTerminalsInGrammar)
* 2logX = lnX/ln2; Poisson(k,MU) = epow(-MU)*MUpow(k)/k!
*/
public double computePoissonPartOfStructurePrior() {
double logPoisson = 0d;
double ePOW_mu = Math.exp(-Main.MU);
double kMin1, k_fac, poisson;
int k = 0;
for (Rule myRule : this.getRules()) {
k = myRule.getRightHandSide().size();
logPoisson += Utils.PoissonLookupTable.get(new Integer(k));
}
this.logPoissonPart = logPoisson;
// if only lexical rules it should return 0
return logPoisson;
}
public double getPoissonPartOfStructurePrior() {
return this.logPoissonPart;
}
public double computelogLikelihood(){
// enumerate rules
int totalCount = 0;
double logLikelihood = 0d;
for (Rule myRule : this.getRules()) {
totalCount += myRule.getCount();
}
// temp e-grids approach
for (Rule myRule : this.getRules())
logLikelihood += ((double) myRule.getCount()) * (Math.log((double) myRule.getCount())/Math.log(2.) - Math.log((double) totalCount)/Math.log(2.));
this.logLikelihood = -logLikelihood;
return -logLikelihood;
}
public double getLikelihood() {
return this.logLikelihood;
}
public void setLikelihoodZero() {
this.logLikelihood=0d;
}
/*
* log P(Theta) = 1/Beta * Sum_i(alfa_i - 1)log Theta_i
* Theta_i = rule probability = myRule.getCount()/totalCount
*/
public double computelogDirichlet() {
// enumerate rules
int totalCount = 0;
int totalRules = this.getRules().size();
if (totalRules == 1) {
this.logDirichletPrior = 0d;
return 0d;
}
double logDirichlet = 0d;
for (Rule myRule : this.getRules()) {
totalCount += myRule.getCount();
}
for (Rule myRule : this.getRules()) {
logDirichlet += (Math.log((double) myRule.getCount()) - Math.log((double) totalCount))/Math.log(2.);
}
// multiply by alfa_i-1;alfa_i = 1/#rules
this.logDirichletPrior = logDirichlet * (1d - ((double) totalRules)) / ((double) totalRules);
return this.logDirichletPrior;
}
public double getDirichlet() {
return this.logDirichletPrior;
}
public boolean equals(Object obj) {
if (!(obj instanceof nonTerminal)) {
return false;
}
nonTerminal other = (nonTerminal) obj;
if (this.nonTerminalName.equals(other.nonTerminalName) && this.associatedRules.equals(other.associatedRules))
return true;
// TODO is this correct?
return false;
}
public int hashCode() {
return nonTerminalName.hashCode();
}
public Object clone() {
try {
nonTerminal aobj = (nonTerminal) super.clone();
aobj.associatedRules = (ArrayList<Rule>) associatedRules.clone();
return aobj;
}
catch (CloneNotSupportedException e) {
throw new InternalError(e.toString());
}
}
}