-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKNBigramLanguageModel.java
More file actions
223 lines (197 loc) · 7.26 KB
/
Copy pathKNBigramLanguageModel.java
File metadata and controls
223 lines (197 loc) · 7.26 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
package nlp.assignments.lm;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import nlp.langmodel.LanguageModel;
import nlp.util.Counter;
import nlp.util.CounterMap;
/**
* A dummy language model -- uses empirical unigram counts, plus a single
* ficticious count for unknown words.
*/
public class KNBigramLanguageModel implements LanguageModel {
static final String START = "<S>";
static final String STOP = "</S>";
static final String UNKNOWN = "*UNKNOWN*";
static final int cutOff = 5;
static final double discount = 0.6 ;
Counter<String> wordCounter = new Counter<String>();
CounterMap<String, String> bigramCounter = new CounterMap<String, String>();
Counter<String> probabilities = new Counter<String>();
Counter<String> backoffs = new Counter<String>();
Counter<String> discountedWordCounter = new Counter<String>();
Counter<String> discountedBigramCounter = new Counter<String>();
public double getBigramProbability(String previousWord, String word)
{
double bigramProb = 0 ;
double bigramCount = bigramCounter.getCount(previousWord, word);
double unigramCount = wordCounter.getCount(previousWord);
if ( unigramCount == 0 )
{
unigramCount = wordCounter.getCount(UNKNOWN) ;
}
if (bigramCount > 0 )
{
bigramProb = ((bigramCount - discount) /unigramCount) ;
}
else
{
double alpha = backoffs.getCount(previousWord);
if (alpha == 0)
{
if (probabilities.getCount(previousWord) == 0)
{
alpha = 1.0;
}
}
double unigramContextCount = bigramCounter.getCounter(previousWord).totalCount();
bigramProb = alpha * unigramContextCount ;
}
return bigramProb ;
}
public double getBigramProbability1(String previousWord, String word)
{
double bigramProbability = probabilities.getCount(previousWord + " " + word);
if (Double.isNaN(bigramProbability) || Double.isInfinite(bigramProbability) || bigramProbability < 0)
System.err.println("stop");
if (bigramProbability != 0) return bigramProbability;
double unigramProbability = probabilities.getCount(word);
if (unigramProbability == 0) {
// System.out.println("UNKNOWN Word: " + word);
unigramProbability = probabilities.getCount(UNKNOWN);
}
if (Double.isNaN(unigramProbability) || Double.isInfinite(unigramProbability) || unigramProbability < 0)
System.err.println("stop");
double backoff = backoffs.getCount(previousWord); //backoffs == alpha
if (backoff == 0) {
if (probabilities.getCount(previousWord) == 0) backoff = 1.0;
}
return unigramProbability * backoff;
}
public double getSentenceProbability(List<String> sentence) {
List<String> stoppedSentence = new ArrayList<String>(sentence);
stoppedSentence.add(0, START);
stoppedSentence.add(STOP);
double probability = 1.0;
String previousWord = stoppedSentence.get(0);
for (int i = 1; i < stoppedSentence.size(); i++) {
String word = stoppedSentence.get(i);
probability *= getBigramProbability(previousWord, word);
previousWord = word;
}
return probability;
}
String generateWord() {
double sample = Math.random();
double sum = 0.0;
for (String word : wordCounter.keySet()) {
sum += wordCounter.getCount(word);
if (sum > sample) {
return word;
}
}
return UNKNOWN;
}
public List<String> generateSentence() {
List<String> sentence = new ArrayList<String>();
String word = generateWord();
while (!word.equals(STOP)) {
sentence.add(word);
word = generateWord();
}
return sentence;
}
public KNBigramLanguageModel(Collection<List<String>> sentenceCollection) {
for (List<String> sentence : sentenceCollection) {
List<String> stoppedSentence = new ArrayList<String>(sentence);
stoppedSentence.add(0, START);
stoppedSentence.add(STOP);
String previousWord = stoppedSentence.get(0);
// wordCounter.incrementCount(previousWord, 1.0);
for (int i = 1; i < stoppedSentence.size(); i++)
{
String word = stoppedSentence.get(i);
wordCounter.incrementCount(word, 1.0);
discountedWordCounter.incrementCount(word, 1.0);
discountedBigramCounter.incrementCount(previousWord + " " + word, 1.0);
bigramCounter.incrementCount(previousWord, word, 1.0);
previousWord = word;
}
}
wordCounter.incrementCount(UNKNOWN, 1.0);
normalizeDistributions();
}
private void normalizeDistributions()
{
double[] unigramBuckets = new double[cutOff + 2];
for (String word : wordCounter.keySet())
{
double count = wordCounter.getCount(word);
if (count <= cutOff + 1)
{
unigramBuckets[(int)count]++;
}
}
double[] bigramBuckets = new double[cutOff + 2];
for (String previousWord : bigramCounter.keySet())
{
Counter<String> currentCounter = bigramCounter.getCounter(previousWord);
for (String word : currentCounter.keySet())
{
double count = currentCounter.getCount(word);
if (count <= cutOff + 1)
{
bigramBuckets[(int)count]++;
}
}
}
double normalizer = 1.0 / wordCounter.totalCount();
double A = (cutOff + 1) * unigramBuckets[cutOff + 1] / unigramBuckets[1];
for (String word : wordCounter.keySet())
{
double count = wordCounter.getCount(word);
if (count > cutOff) probabilities.setCount(word, count * normalizer);
else
{
double discountedCount = (count + 1)* unigramBuckets[(int)count + 1] / unigramBuckets[(int)count];
double probability = count * normalizer * (discountedCount / count - A) / (1 - A);
probabilities.setCount(word, probability);
if (Double.isNaN(probability) || Double.isInfinite(probability) || probability < 0)
System.err.println("stop");
}
}
probabilities.setCount(UNKNOWN, unigramBuckets[1] * normalizer / wordCounter.size());
A = (cutOff + 1) * bigramBuckets[cutOff + 1] / bigramBuckets[1];
Counter<String> forwardProbability = new Counter<String>();
Counter<String> backwardProbability = new Counter<String>();
for (String previousWord : bigramCounter.keySet()) {
Counter<String> currentCounter = bigramCounter.getCounter(previousWord);
normalizer = 1.0 / currentCounter.totalCount();
double probability = 0;
double probabilitySoFar = 0;
for (String word : currentCounter.keySet()) {
double count = currentCounter.getCount(word);
if (count > cutOff) {
probability = count * normalizer;
// probability *= 0.99;
}
else {
double discountedCount = (count + 1) * bigramBuckets[(int)count + 1] / bigramBuckets[(int)count];
probability = count * normalizer * (discountedCount / count - A) / (1 - A);
}
if (Double.isNaN(probability) || Double.isInfinite(probability) || probability < 0)
System.err.println("stop");
probabilities.setCount(previousWord + " " + word, probability);
backwardProbability.incrementCount(previousWord, probabilities.getCount(word));
probabilitySoFar += probability;
}
forwardProbability.setCount(previousWord, probabilitySoFar);
}
for (String word : wordCounter.keySet()) {
double backoff = (1.0 - forwardProbability.getCount(word)) / (1.0 - backwardProbability.getCount(word));
if (Double.isNaN(backoff) || Double.isInfinite(backoff) || backoff == 0)
System.err.println("stop");
backoffs.setCount(word, backoff);
}
}
}