Repository navigation
Expand file tree
/
Copy pathKN_BigramLanguageModel.java
More file actions
196 lines (168 loc) · 7.73 KB
/
Copy pathKN_BigramLanguageModel.java
File metadata and controls
196 lines (168 loc) · 7.73 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
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 Kneser-Ney smoothed language model using bigram counts
*/
public class KN_BigramLanguageModel implements LanguageModel {
static final String STOP = "</S>";
static final String START = "<S>";
// double singletonBicount = 0.0;
// double doubletonBicount = 0.0;
double discount = 0.6;
double wordCount = 0.0;
double vocabSize = 0.0;
double bigramVocabSize = 0.0;
double sentenceCount = 0.0;
CounterMap<String, String> bigramCounterMap = new CounterMap<String, String>();
CounterMap<String, String> reverseBigramCounterMap = new CounterMap<String, String>();
public double unigramCount(String word) {
if (word.equals(STOP)) { return sentenceCount; }
else {
Counter<String> nextwords = bigramCounterMap.getCounter(word);
if (nextwords.totalCount() == 0) { System.out.println("whoops: "+word); }
return nextwords.totalCount();
}
}
// public double getUnigramProbability(List<String> sentence, int index) {
// String word = sentence.get(index);
// return unigramCount(word) / wordCount;
// }
//
public double norm(String token) {
double tokencount = unigramCount(token);
return (discount / tokencount) * bigramCounterMap.getCounter(token).size();
}
public double p_cont(String word) {
return reverseBigramCounterMap.getCounter(word).size() / bigramVocabSize;
}
public double p_interp(String word1, String word2) {
double word1count = unigramCount(word1);
double bicount = bigramCounterMap.getCount(word1, word2);
return (Math.max(bicount - discount, 0) / word1count) + (norm(word1) * p_cont(word2));
}
public double getP_interp(List<String> sentence, int index) {
String word1 = sentence.get(index);
String word2 = sentence.get(index+1);
double prob = p_interp(word1, word2);
if (((Double)prob).isNaN()) { System.out.println(word1+"-"+word2+": NaN"); }
return prob;
}
public double getSentenceProbability(List<String> sentence) {
List<String> stoppedStartedSentence = new ArrayList<String>(sentence);
stoppedStartedSentence.add(STOP);
stoppedStartedSentence.add(0,START);
double probability = 1.0;
for (int index = 0; index < stoppedStartedSentence.size()-1; index++) {
probability *= getP_interp(stoppedStartedSentence, index);
}
return probability;
}
String generateNextWord(String word1) {
double sample = Math.random();
double sum = 0.0;
Counter<String> nextwords = bigramCounterMap.getCounter(word1);
for (String word2 : nextwords.keySet()) {
sum += bigramCounterMap.getCount(word1, word2) / unigramCount(word1);
if (sum > sample) { return word2; }
}
return word1+": nope";
}
public List<String> generateSentence() {
List<String> sentence = new ArrayList<String>();
String word = generateNextWord(START);
while (!word.equals(STOP)) {
sentence.add(word);
word = generateNextWord(word);
}
return sentence;
}
public KN_BigramLanguageModel(Collection<List<String>> trainingSet, Collection<List<String>> validSet)
{
for (List<String> sentence : trainingSet) {
List<String> stoppedStartedSentence = new ArrayList<String>(sentence);
stoppedStartedSentence.add(STOP);
stoppedStartedSentence.add(0, START);
for (int i=0; i < stoppedStartedSentence.size()-1; i++) {
String token1 = stoppedStartedSentence.get(i);
String token2 = stoppedStartedSentence.get(i+1);
bigramCounterMap.incrementCount(token1, token2, 1.0);
reverseBigramCounterMap.incrementCount(token2, token1, 1.0);
}
}
for (List<String> sentence : validSet) {
List<String> stoppedStartedSentence = new ArrayList<String>(sentence);
stoppedStartedSentence.add(STOP);
stoppedStartedSentence.add(0, START);
for (int i=0; i < stoppedStartedSentence.size()-1; i++) {
String token1 = stoppedStartedSentence.get(i);
String token2 = stoppedStartedSentence.get(i+1);
bigramCounterMap.incrementCount(token1, token2, 1.0);
reverseBigramCounterMap.incrementCount(token2, token1, 1.0);
}
}
wordCount = bigramCounterMap.totalCount() + sentenceCount;
vocabSize = bigramCounterMap.size();
bigramVocabSize = bigramCounterMap.totalSize();
sentenceCount = trainingSet.size() + validSet.size();
System.out.println("Wordcount: "+wordCount);
System.out.println("Vocabsize: "+vocabSize);
System.out.println("BigramVocabsize: "+bigramVocabSize);
System.out.println("Sentencecount: "+sentenceCount);
// System.out.println("the cat: " + bigramCounterMap.getCount("the", "cat"));
// System.out.println("a cat: " + bigramCounterMap.getCount("a", "cat"));
// System.out.println("grade cat: " + bigramCounterMap.getCount("grade", "cat"));
// System.out.println(reverseBigramCounterMap.getCounter("cat"));
// System.out.println( (.5 / 790463.0) * (reverseBigramCounterMap.getCounter("conscript").size() / bigramVocabSize) ) ;
// for (String word1 : bigramCounterMap.keySet()) {
// for (String word2 : bigramCounterMap.getCounter(word1).keySet()) {
// if (bigramCounterMap.getCount(word1, word2) == 1) { singletonBicount += 1; }
// else if (bigramCounterMap.getCount(word1, word2) == 2) { doubletonBicount += 1; }
// }
// }
// discount = singletonBicount / (singletonBicount + 2 * doubletonBicount);
// System.out.println("singletonTricount: "+singletonBicount);
// System.out.println("doubletonTricount: "+doubletonBicount);
// System.out.println("discount: "+discount);
}
public KN_BigramLanguageModel(Collection<List<String>> trainingSet) {
for (List<String> sentence : trainingSet) {
List<String> stoppedStartedSentence = new ArrayList<String>(sentence);
stoppedStartedSentence.add(STOP);
stoppedStartedSentence.add(0, START);
for (int i=0; i < stoppedStartedSentence.size()-1; i++) {
String token1 = stoppedStartedSentence.get(i);
String token2 = stoppedStartedSentence.get(i+1);
bigramCounterMap.incrementCount(token1, token2, 1.0);
reverseBigramCounterMap.incrementCount(token2, token1, 1.0);
}
}
wordCount = bigramCounterMap.totalCount() + sentenceCount;
vocabSize = bigramCounterMap.size();
bigramVocabSize = bigramCounterMap.totalSize();
// sentenceCount = trainingSet.size() + validSet.size();
System.out.println("Wordcount: "+wordCount);
System.out.println("Vocabsize: "+vocabSize);
System.out.println("BigramVocabsize: "+bigramVocabSize);
System.out.println("Sentencecount: "+sentenceCount);
// System.out.println("the cat: " + bigramCounterMap.getCount("the", "cat"));
// System.out.println("a cat: " + bigramCounterMap.getCount("a", "cat"));
// System.out.println("grade cat: " + bigramCounterMap.getCount("grade", "cat"));
// System.out.println(reverseBigramCounterMap.getCounter("cat"));
// System.out.println( (.5 / 790463.0) * (reverseBigramCounterMap.getCounter("conscript").size() / bigramVocabSize) ) ;
// for (String word1 : bigramCounterMap.keySet()) {
// for (String word2 : bigramCounterMap.getCounter(word1).keySet()) {
// if (bigramCounterMap.getCount(word1, word2) == 1) { singletonBicount += 1; }
// else if (bigramCounterMap.getCount(word1, word2) == 2) { doubletonBicount += 1; }
// }
// }
// discount = singletonBicount / (singletonBicount + 2 * doubletonBicount);
// System.out.println("singletonTricount: "+singletonBicount);
// System.out.println("doubletonTricount: "+doubletonBicount);
// System.out.println("discount: "+discount);
}
}