-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLanguageModelTester.java
More file actions
462 lines (420 loc) · 14.9 KB
/
Copy pathLanguageModelTester.java
File metadata and controls
462 lines (420 loc) · 14.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
package nlp.assignments;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.text.NumberFormat;
import java.text.DecimalFormat;
import nlp.assignments.lm.EmpiricalBigramLanguageModel;
import nlp.assignments.lm.EmpiricalBigramLanguageModelWithEM;
import nlp.assignments.lm.EmpiricalTrigramLanguageModel;
import nlp.assignments.lm.EmpiricalTrigramLanguageModelWithEM;
import nlp.assignments.lm.EmpiricalUnigramLanguageModel;
import nlp.assignments.lm.KN_BigramLanguageModel;
import nlp.assignments.lm.KN_TrigramLanguageModel;
import nlp.assignments.lm.KatzBigramLanguageModel;
import nlp.assignments.lm.KatzTrigramLanguageModel;
import nlp.assignments.lm.SriLanguageModel;
import nlp.langmodel.LanguageModel;
import nlp.util.CommandLineUtils;
/**
* This is the main harness for assignment 1. To run this harness, use
* <p/>
* java nlp.assignments.LanguageModelTester -path ASSIGNMENT_DATA_PATH -model
* MODEL_DESCRIPTOR_STRING
* <p/>
* First verify that the data can be read on your system. Second, find the point
* in the main method (near the bottom) where an EmpiricalUnigramLanguageModel
* is constructed. You will be writing new implementations of the LanguageModel
* interface and constructing them there.
*/
public class LanguageModelTester {
// HELPER CLASS FOR THE HARNESS, CAN IGNORE
static class EditDistance {
static double INSERT_COST = 1.0;
static double DELETE_COST = 1.0;
static double SUBSTITUTE_COST = 1.0;
private double[][] initialize(double[][] d) {
for (int i = 0; i < d.length; i++) {
for (int j = 0; j < d[i].length; j++) {
d[i][j] = Double.NaN;
}
}
return d;
}
public double getDistance(List<? extends Object> firstList,
List<? extends Object> secondList) {
double[][] bestDistances = initialize(new double[firstList.size() + 1][secondList
.size() + 1]);
return getDistance(firstList, secondList, 0, 0, bestDistances);
}
private double getDistance(List<? extends Object> firstList,
List<? extends Object> secondList, int firstPosition,
int secondPosition, double[][] bestDistances) {
if (firstPosition > firstList.size()
|| secondPosition > secondList.size())
return Double.POSITIVE_INFINITY;
if (firstPosition == firstList.size()
&& secondPosition == secondList.size())
return 0.0;
if (Double.isNaN(bestDistances[firstPosition][secondPosition])) {
double distance = Double.POSITIVE_INFINITY;
distance = Math.min(
distance,
INSERT_COST
+ getDistance(firstList, secondList,
firstPosition + 1, secondPosition,
bestDistances));
distance = Math.min(
distance,
DELETE_COST
+ getDistance(firstList, secondList,
firstPosition, secondPosition + 1,
bestDistances));
distance = Math.min(
distance,
SUBSTITUTE_COST
+ getDistance(firstList, secondList,
firstPosition + 1, secondPosition + 1,
bestDistances));
if (firstPosition < firstList.size()
&& secondPosition < secondList.size()) {
if (firstList.get(firstPosition).equals(
secondList.get(secondPosition))) {
distance = Math.min(
distance,
getDistance(firstList, secondList,
firstPosition + 1, secondPosition + 1,
bestDistances));
}
}
bestDistances[firstPosition][secondPosition] = distance;
}
return bestDistances[firstPosition][secondPosition];
}
}
// HELPER CLASS FOR THE HARNESS, CAN IGNORE
static class SentenceCollection extends AbstractCollection<List<String>> {
static class SentenceIterator implements Iterator<List<String>> {
BufferedReader reader;
public boolean hasNext() {
try {
return reader.ready();
} catch (IOException e) {
return false;
}
}
public List<String> next() {
try {
String line = reader.readLine();
String[] words = line.split("\\s+");
List<String> sentence = new ArrayList<String>();
for (int i = 0; i < words.length; i++) {
String word = words[i];
sentence.add(word.toLowerCase());
}
return sentence;
} catch (IOException e) {
throw new NoSuchElementException();
}
}
public void remove() {
throw new UnsupportedOperationException();
}
public SentenceIterator(BufferedReader reader) {
this.reader = reader;
}
}
String fileName;
public Iterator<List<String>> iterator() {
try {
BufferedReader reader = new BufferedReader(new FileReader(
fileName));
return new SentenceIterator(reader);
} catch (FileNotFoundException e) {
throw new RuntimeException("Problem with SentenceIterator for "
+ fileName);
}
}
public int size() {
int size = 0;
Iterator i = iterator();
while (i.hasNext()) {
size++;
i.next();
}
return size;
}
public SentenceCollection(String fileName) {
this.fileName = fileName;
}
public static class Reader {
static Collection<List<String>> readSentenceCollection(
String fileName) {
return new SentenceCollection(fileName);
}
}
}
static double calculatePerplexity(LanguageModel languageModel,
Collection<List<String>> sentenceCollection)
{
double logProbability = 0.0;
double numSymbols = 0.0;
for (List<String> sentence : sentenceCollection)
{
logProbability += Math.log(languageModel
.getSentenceProbability(sentence)) / Math.log(2.0);
numSymbols += sentence.size();
}
double avgLogProbability = logProbability / numSymbols;
double perplexity = Math.pow(0.5, avgLogProbability);
return perplexity;
}
static double calculateWordErrorRate(LanguageModel languageModel,
List<SpeechNBestList> speechNBestLists, boolean verbose) {
double totalDistance = 0.0;
double totalWords = 0.0;
EditDistance editDistance = new EditDistance();
for (SpeechNBestList speechNBestList : speechNBestLists)
{
List<String> correctSentence = speechNBestList.getCorrectSentence();
List<String> bestGuess = null;
double bestScore = Double.NEGATIVE_INFINITY;
double numWithBestScores = 0.0;
double distanceForBestScores = 0.0;
for (List<String> guess : speechNBestList.getNBestSentences())
{
double score = Math.log(languageModel
.getSentenceProbability(guess))
+ (speechNBestList.getAcousticScore(guess) / 16.0);
double distance = editDistance.getDistance(correctSentence,
guess);
if (score == bestScore)
{
numWithBestScores += 1.0;
distanceForBestScores += distance;
}
if (score > bestScore || bestGuess == null)
{
bestScore = score;
bestGuess = guess;
distanceForBestScores = distance;
numWithBestScores = 1.0;
}
}
// double distance = editDistance.getDistance(correctSentence,
// bestGuess);
totalDistance += distanceForBestScores / numWithBestScores;
totalWords += correctSentence.size();
if (verbose)
{
System.out.println();
displayHypothesis("GUESS:", bestGuess, speechNBestList,
languageModel);
displayHypothesis("GOLD:", correctSentence, speechNBestList,
languageModel);
}
}
return totalDistance / totalWords;
}
private static NumberFormat nf = new DecimalFormat("0.00E00");
private static void displayHypothesis(String prefix, List<String> guess,
SpeechNBestList speechNBestList, LanguageModel languageModel) {
double acoustic = speechNBestList.getAcousticScore(guess) / 16.0;
double language = Math.log(languageModel.getSentenceProbability(guess));
System.out.println(prefix + "\tAM: " + nf.format(acoustic) + "\tLM: "
+ nf.format(language) + "\tTotal: "
+ nf.format(acoustic + language) + "\t" + guess);
}
static double calculateWordErrorRateLowerBound(
List<SpeechNBestList> speechNBestLists) {
double totalDistance = 0.0;
double totalWords = 0.0;
EditDistance editDistance = new EditDistance();
for (SpeechNBestList speechNBestList : speechNBestLists) {
List<String> correctSentence = speechNBestList.getCorrectSentence();
double bestDistance = Double.POSITIVE_INFINITY;
for (List<String> guess : speechNBestList.getNBestSentences()) {
double distance = editDistance.getDistance(correctSentence,
guess);
if (distance < bestDistance)
bestDistance = distance;
}
totalDistance += bestDistance;
totalWords += correctSentence.size();
}
return totalDistance / totalWords;
}
static double calculateWordErrorRateUpperBound(
List<SpeechNBestList> speechNBestLists) {
double totalDistance = 0.0;
double totalWords = 0.0;
EditDistance editDistance = new EditDistance();
for (SpeechNBestList speechNBestList : speechNBestLists) {
List<String> correctSentence = speechNBestList.getCorrectSentence();
double worstDistance = Double.NEGATIVE_INFINITY;
for (List<String> guess : speechNBestList.getNBestSentences()) {
double distance = editDistance.getDistance(correctSentence,
guess);
if (distance > worstDistance)
worstDistance = distance;
}
totalDistance += worstDistance;
totalWords += correctSentence.size();
}
return totalDistance / totalWords;
}
static double calculateWordErrorRateRandomChoice(
List<SpeechNBestList> speechNBestLists) {
double totalDistance = 0.0;
double totalWords = 0.0;
EditDistance editDistance = new EditDistance();
for (SpeechNBestList speechNBestList : speechNBestLists) {
List<String> correctSentence = speechNBestList.getCorrectSentence();
double sumDistance = 0.0;
double numGuesses = 0.0;
for (List<String> guess : speechNBestList.getNBestSentences()) {
double distance = editDistance.getDistance(correctSentence,
guess);
sumDistance += distance;
numGuesses += 1.0;
}
totalDistance += sumDistance / numGuesses;
totalWords += correctSentence.size();
}
return totalDistance / totalWords;
}
static Collection<List<String>> extractCorrectSentenceList(
List<SpeechNBestList> speechNBestLists) {
Collection<List<String>> correctSentences = new ArrayList<List<String>>();
for (SpeechNBestList speechNBestList : speechNBestLists) {
correctSentences.add(speechNBestList.getCorrectSentence());
}
return correctSentences;
}
static Set extractVocabulary(Collection<List<String>> sentenceCollection) {
Set<String> vocabulary = new HashSet<String>();
for (List<String> sentence : sentenceCollection) {
for (String word : sentence) {
vocabulary.add(word);
}
}
return vocabulary;
}
public static void main(String[] args) throws IOException {
// Parse command line flags and arguments
Map<String, String> argMap = CommandLineUtils
.simpleCommandLineParser(args);
// Set up default parameters and settings
String basePath = ".";
String model = "baseline";
boolean verbose = false;
// Update defaults using command line specifications
// The path to the assignment data
if (argMap.containsKey("-path")) {
basePath = argMap.get("-path");
}
System.out.println("Using base path: " + basePath);
// A string descriptor of the model to use
if (argMap.containsKey("-model")) {
model = argMap.get("-model");
}
System.out.println("Using model: " + model);
// Whether or not to print the individual speech errors.
if (argMap.containsKey("-verbose")) {
String val = argMap.get("-verbose");
if (val.equalsIgnoreCase("false")) {
verbose = false;
} else {
verbose = true;
}
}
if (argMap.containsKey("-quiet")) {
verbose = false;
}
// Read in all the assignment data
String trainingSentencesFile = "/treebank-sentences-spoken-train.txt";
String validationSentencesFile = "/treebank-sentences-spoken-validate.txt";
String testSentencesFile = "/treebank-sentences-spoken-test.txt";
String speechNBestListsPath = "/wsj_n_bst";
Collection<List<String>> trainingSentenceCollection = SentenceCollection.Reader
.readSentenceCollection(basePath + trainingSentencesFile);
Collection<List<String>> validationSentenceCollection = SentenceCollection.Reader
.readSentenceCollection(basePath + validationSentencesFile);
Collection<List<String>> testSentenceCollection = SentenceCollection.Reader
.readSentenceCollection(basePath + testSentencesFile);
Set trainingVocabulary = extractVocabulary(trainingSentenceCollection);
List<SpeechNBestList> speechNBestLists = SpeechNBestList.Reader
.readSpeechNBestLists(basePath + speechNBestListsPath,
trainingVocabulary);
// Build the language model
LanguageModel languageModel = null;
if (model.equalsIgnoreCase("baseline")) {
languageModel = new EmpiricalUnigramLanguageModel(
trainingSentenceCollection);
} else if (model.equalsIgnoreCase("sri")) {
languageModel = new SriLanguageModel(argMap.get("-sri"));
} else if (model.equalsIgnoreCase("bigram")) {
languageModel = new EmpiricalBigramLanguageModel(
trainingSentenceCollection);
} else if (model.equalsIgnoreCase("trigram")) {
languageModel = new EmpiricalTrigramLanguageModel(
trainingSentenceCollection);
} else if (model.equalsIgnoreCase("katz-bigram")) {
languageModel = new KatzBigramLanguageModel(
trainingSentenceCollection);
}
else if (model.equalsIgnoreCase("katz-trigram"))
{
languageModel = new KatzTrigramLanguageModel(
trainingSentenceCollection);
}
else if (model.equalsIgnoreCase("kn-bigram"))
{
languageModel = new KN_BigramLanguageModel(
trainingSentenceCollection,validationSentenceCollection);
}
else if (model.equalsIgnoreCase("kn-trigram"))
{
languageModel = new KN_TrigramLanguageModel(
trainingSentenceCollection,validationSentenceCollection);
}
else if (model.equalsIgnoreCase("bigram-em"))
{
languageModel = new EmpiricalBigramLanguageModelWithEM(
trainingSentenceCollection,validationSentenceCollection);
}
else if (model.equalsIgnoreCase("trigram-em"))
{
languageModel = new EmpiricalTrigramLanguageModelWithEM(
trainingSentenceCollection,validationSentenceCollection);
}
else
{
throw new RuntimeException("Unknown model descriptor: " + model);
}
// Evaluate the language model
double wsjPerplexity = calculatePerplexity(languageModel,
testSentenceCollection);
double hubPerplexity = calculatePerplexity(languageModel,
extractCorrectSentenceList(speechNBestLists));
System.out.println("WSJ Perplexity: " + wsjPerplexity);
System.out.println("HUB Perplexity: " + hubPerplexity);
System.out.println("WER Baselines:");
System.out.println(" Best Path: "
+ calculateWordErrorRateLowerBound(speechNBestLists));
System.out.println(" Worst Path: "
+ calculateWordErrorRateUpperBound(speechNBestLists));
System.out.println(" Avg Path: "
+ calculateWordErrorRateRandomChoice(speechNBestLists));
double wordErrorRate = calculateWordErrorRate(languageModel,
speechNBestLists, verbose);
System.out.println("HUB Word Error Rate: " + wordErrorRate);
System.out.println("Generated Sentences:");
// for (int i = 0; i < 10; i++)
// {
// System.out.println(" " + languageModel.generateSentence());
// }
}
}