-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTopicModeller.java
507 lines (392 loc) · 19.3 KB
/
TopicModeller.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
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Formatter;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;
import java.util.*;
import java.io.*;
import java.util.Scanner;
import cc.mallet.util.*;
import cc.mallet.types.*;
import cc.mallet.pipe.*;
import cc.mallet.pipe.iterator.*;
import cc.mallet.topics.*;
import javax.sound.sampled.UnsupportedAudioFileException;
import cc.mallet.pipe.CharSequence2TokenSequence;
import cc.mallet.pipe.CharSequenceLowercase;
import cc.mallet.pipe.Pipe;
import cc.mallet.pipe.SerialPipes;
import cc.mallet.pipe.TokenSequence2FeatureSequence;
import cc.mallet.pipe.TokenSequenceRemoveStopwords;
import cc.mallet.pipe.iterator.CsvIterator;
import cc.mallet.topics.ParallelTopicModel;
import cc.mallet.topics.TopicInferencer;
import cc.mallet.types.Alphabet;
import cc.mallet.types.FeatureSequence;
import cc.mallet.types.Instance;
import cc.mallet.types.InstanceList;
import cc.mallet.types.LabelSequence;
import edu.cmu.sphinx.frontend.util.AudioFileDataSource;
import edu.cmu.sphinx.linguist.dictionary.Word;
import edu.cmu.sphinx.linguist.language.ngram.large.LargeNGramModel;
import edu.cmu.sphinx.linguist.WordSequence;
import edu.cmu.sphinx.recognizer.Recognizer;
import edu.cmu.sphinx.result.Lattice;
import edu.cmu.sphinx.result.LatticeOptimizer;
import edu.cmu.sphinx.result.Result;
import edu.cmu.sphinx.util.TimerPool;
import edu.cmu.sphinx.util.props.ConfigurationManager;
public class TopicModeller {
WordSequence words;
InstanceList instances;
ParallelTopicModel model;
static String resultText;
// Containers for words extracted from topic model with rank from model, all topics + topic prob and
// highest probability topic + words associated with it
static HashMap<String, Integer> wordsrank = new HashMap<String, Integer>();
static Map<Integer, Double> testprobabilities = new HashMap<Integer, Double>();
static Map<Integer, String> topicwords = new HashMap<Integer, String>();
// Transcripts of wavs for WER
static String [] BC1 = "AUSTRALIAN RESEACHERS ARE OPTIMISTIC THEY HAVE FOUND A WAY TO TREAT TRIPLE NEGATIVE BREAST CANCER AN AGGRESSIVE DISEASE THAT MAINLY AFFECTS YOUNGER WOMEN".split(" ");
static String [] BC2 = "BREAST CANCER IS THE MOST COMMON CANCER AMONGST WOMEN IN AUSTRALIA".split(" ");
static String [] BC3 = "BREAST CANCER ELICITS SO MANY FEARS INCLUDING THOSE RELATING TO DEATH, SURGERY, LOSS OF BODY IMAGE AND LOSS OF SEXUALITY".split(" ");
static String [] MLOBAMA = "CREATING ALGORITHMS TO INTERPRET MOUNTAINS OF DATA THE MACHINE LEARNING EXPERT PLAYED A GROUND-BREAKING ROLE AS CHIEF SCIENTIST FOR THE OBAMA FOR AMERICA CAMPAIGN".split(" ");
static String [] HEALTH1 = "HAVE KNOWN FOR AT LEAST THREE YEARS THAT MILLIONS OF AMERICANS WOULD NOT BE ABLE TO KEEP THEIR CURRENT HEALTH CARE PLAN".split(" ");
static String [] HEALTH2 = "LOCAL COUNCILS PLAY A SIGNIFICANT ROLE BY MAKING SURE THE ENVIRONMENTS WE LIVE, WORK AND SOCIALIZE IN ENCOURAGE REGULAR EXERCISE".split(" ");
static String [] SPEECH1 = "RESEARCH INTO SPHINX AND LANGUAGE MODELING FOR BETTER SPEECH RECOGNITION REQUIRES MANY MORE WORDS".split(" ");
static String [] SPEECH2 = "BEFORE YOU GET STARTED USING WINDOWS SPEECH RECOGNITION, YOU'LL NEED TO CONNECT A MICROPHONE TO YOUR COMPUTER".split(" ");
static String [] SPEECH3 = "PEOPLE WITH DISABILITIES THAT PREVENT THEM FROM TYPING HAVE ALSO ADOPTED SPEECH RECOGNITION SYSTEMS".split(" ");
static String [] SPEECH4 = "PROGRESS HAS COME THANKS IN PART TO STEADY PROGRESS IN THE TECHNOLOGY NEEDED TO HELP MACHINES UNDERSTAND HUMAN SPEECH".split(" ");
public static void main(String[] args) throws Exception {
TopicModeller tm = new TopicModeller();
tm.buildTopicModel("resources/corpus.txt");
URL audioURL, url;
if (args.length > 0) {
audioURL = new File(args[0]).toURI().toURL();
} else {
audioURL = TopicModeller.class.getResource("BC1.wav");
}
if (args.length > 1) {
url = new File(args[1]).toURI().toURL();
} else {
url = TopicModeller.class.getResource("config.xml");
}
System.out.println("Loading recogniser...");
ConfigurationManager cm = new ConfigurationManager(url);
Recognizer recognizer = (Recognizer) cm.lookup("recognizer");
recognizer.allocate();
// configure the audio input for the recognizer
AudioFileDataSource dataSource = (AudioFileDataSource) cm.lookup("audioFileDataSource");
dataSource.setAudioFile(audioURL, null);
boolean done = false;
while (!done) {
/*This method will return when the end of speech
* is reached. Note that the endpointer will determine
* the end of speech.*/
Result result = recognizer.recognize();
if (result != null) {
Lattice lattice = new Lattice(result);
LatticeOptimizer optimizer = new LatticeOptimizer(lattice);
optimizer.optimize();
lattice.dumpAllPaths();
resultText = result.getBestResultNoFiller();
System.out.println("I heard: " + resultText + '\n');
} else {
done = true;
}
}
// calculate WER
WordSequenceAligner werEval = new WordSequenceAligner();
String [] hyp = resultText.split(" ");
WordSequenceAligner.Alignment a = werEval.align(BC1, hyp);
System.out.println(a);
// pass text into topic model
double[] testProbabilities = tm.classify(resultText);
for (int i = 0; i < 100; i++) {
testprobabilities.put(i, testProbabilities[i]);
}
// Find highest probability topic
Object highesttopic=0;
double maxValueInMap=(Collections.max(testprobabilities.values())); // This will return max value in the Hashmap
for (Entry<Integer, Double> entry : testprobabilities.entrySet()) { // Iterate through hashmap
if (entry.getValue()==maxValueInMap) {
highesttopic=entry.getKey();
System.out.println("Highest probability topic: "+highesttopic); // Print the key with max value
}
}
// get words associated with highest probability topic
String words= null;
for(Map.Entry entry: topicwords.entrySet()){
if(highesttopic.equals(entry.getKey())){
words = entry.getValue().toString();
System.out.println("Words given highest probability topic: "+words); // Print the key with max value
}
}
StringTokenizer st = new StringTokenizer(words, " ");
while( st.hasMoreElements() )
{
String key = st.nextToken().toUpperCase();
String value = st.nextToken();
// wordsrank.put(key, Integer.valueOf(value));
wordsrank.put(key, Integer.valueOf(value));
}
// Clear unneeded Hashmaps
topicwords.clear();
testprobabilities.clear();
// tm.adaptProbs(wordsrank);
loadLM();
}
public void adaptProbs(HashMap wordsrank) throws Exception {
URL url;
url = TopicModeller.class.getResource("config.xml");
ConfigurationManager cm = new ConfigurationManager(url);
edu.cmu.sphinx.linguist.language.ngram.large.KeywordOptimizerLargeNGramModel model = (edu.cmu.sphinx.linguist.language.ngram.large.KeywordOptimizerLargeNGramModel)cm.lookup("trigramModel");
//KeywordOptimizerLargeNGramModel model = new KeywordOptimizerLargeNGramModel();
//model.keywordProbs = this.wordsrank;
/* String word1 = "AARON";
String word2 = "LOVES";
words = new LargeNGramModel().getBigramProb(2, 4);*/
//model.getProbability(words);
}
public void buildTopicModel(String fileName) throws Exception {
// Begin by importing documents from text to feature sequences
ArrayList<Pipe> pipeList = new ArrayList<Pipe>();
// Pipes: lowercase, tokenize, remove stopwords, map to features
pipeList.add( new CharSequenceLowercase() );
pipeList.add( new CharSequence2TokenSequence(Pattern.compile("\\p{L}[\\p{L}\\p{P}]+\\p{L}")) );
pipeList.add( new TokenSequenceRemoveStopwords(new File("resources/en.txt"), "UTF-8", false, false, false) );
pipeList.add( new TokenSequence2FeatureSequence() );
instances = new InstanceList (new SerialPipes(pipeList));
Reader fileReader = new InputStreamReader(new FileInputStream(new File(fileName)), "UTF-8");
instances.addThruPipe(new CsvIterator (fileReader, Pattern.compile("^(\\S*)[\\s,]*(\\S*)[\\s,]*(.*)$"),
3, 2, 1)); // data, label, name fields
// Create a model with 100 topics, alpha_t = 0.01, beta_w = 0.01
// Note that the first parameter is passed as the sum over topics, while
// the second is
int numTopics = 100;
model = new ParallelTopicModel(numTopics, 1.0, 0.01);
model.addInstances(instances);
// Use two parallel samplers, which each look at one half the corpus and combine
// statistics after every iteration.
model.setNumThreads(2);
// Run the model for 50 iterations and stop (this is for testing only,
// for real applications, use 1000 to 2000 iterations)
model.setNumIterations(50);
model.estimate();
// Show the words and topics in the first instance
// The data alphabet maps word IDs to strings
Alphabet dataAlphabet = instances.getDataAlphabet();
FeatureSequence tokens = (FeatureSequence) model.getData().get(0).instance.getData();
LabelSequence topics = model.getData().get(0).topicSequence;
Formatter out = new Formatter(new StringBuilder(), Locale.US);
for (int position = 0; position < tokens.getLength(); position++) {
out.format("%s-%d ", dataAlphabet.lookupObject(tokens.getIndexAtPosition(position)), topics.getIndexAtPosition(position));
}
// Estimate the topic distribution of the first instance,
// given the current Gibbs state.
double[] topicDistribution = model.getTopicProbabilities(topics);
// Get an array of sorted sets of word ID/count pairs
ArrayList<TreeSet<IDSorter>> topicSortedWords = model.getSortedWords();
// Show top 5 words in topics with proportions for the first document
for (int topic = 0; topic < numTopics; topic++) {
Iterator<IDSorter> iterator = topicSortedWords.get(topic).iterator();
out = new Formatter(new StringBuilder(), Locale.US);
out.format("%d\t%.3f\t", topic, topicDistribution[topic]);
int rank = 0;
while (iterator.hasNext()) {
IDSorter idCountPair = iterator.next();
out.format("%s %.0f ", dataAlphabet.lookupObject(idCountPair.getID()), idCountPair.getWeight());
rank++;
}
// Put all topics and words into a container for querying later
String[] words = out.toString().split("\t");
topicwords.put(Integer.valueOf(words[0]), words[2]);
}
}
public double[] classify(String text) {
// Create a new instance named "test instance" with empty target and source fields.
InstanceList testing = new InstanceList(instances.getPipe());
testing.addThruPipe(new Instance(text, null, "test instance", null));
TopicInferencer inferencer = model.getInferencer();
double[] testProbabilities = inferencer.getSampledDistribution(testing.get(0), 10, 1, 5);
return testProbabilities;
}
public static void loadLM() throws Exception {
File file = new File("resources/input.lm");
Scanner scanner = new Scanner(file);
FileWriter writer = new FileWriter("resources/output.lm");
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
writer.write(line+"\n");
if (line.equals("\\1-grams:"))
{
break;
}
}
if (!scanner.hasNextLine()) {System.out.println("crap no 1grams!");}
else {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.equals(""))
{
break;
}
else {
String[] lines = line.split(" ");
// Check unigrams against word list
String inc="1.0";
for(String word: lines){
if(wordsrank.containsKey(word))
{
DecimalFormat df = new DecimalFormat( "##0.0000");
//System.out.println(word + " matches a unigram");
// Proportionate rank from topic model
int num=wordsrank.size();
double rank=1.0*(num/wordsrank.size());
num--;
// Adjusted probability
double prob=Double.parseDouble(lines[0])+(Double.parseDouble(inc)*(rank));
String logprob=df.format(prob);
lines[0]=logprob;
}
}
writer.write(lines[0] + " " + lines[1] + " " + lines[2]+"\n");
}
}
}
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.equals("\\2-grams:"))
{
writer.write("\n\\2-grams:\n");
break;
}
}
if (!scanner.hasNextLine()) {System.out.println("crap no 2grams!");}
else {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.equals(""))
{
break;
}
else {
String[] lines = line.split(" ");
// Check bigrams against word list
String inc="1.0";
for(String word: lines){
if(wordsrank.containsKey(word))
{
DecimalFormat df = new DecimalFormat( "##0.0000");
//System.out.println(word + " matches a bigram");
// Proportionate rank from topic model
int num=wordsrank.size();
double rank=1.0*(num/wordsrank.size());
num--;
// Adjusted probability
double prob=Double.parseDouble(lines[0])+(Double.parseDouble(inc)*(rank));
String logprob=df.format(prob);
lines[0]=logprob;
}
}
writer.write((lines[0]) + " " + lines[1] + " " + lines[2] + " " + lines[3] + "\n");
}
}
}
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.equals("\\3-grams:"))
{
writer.write("\n\\3-grams:\n");
break;
}
}
if (!scanner.hasNextLine()) {System.out.println("crap no 3grams!");}
else {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.equals(""))
{
break;
}
else {
String[] lines = line.split(" ");
// Check trigrams against word list
String inc="1.0";
for(String word: lines){
if(wordsrank.containsKey(word))
{
DecimalFormat df = new DecimalFormat( "##0.0000");
//System.out.println(word + " matches a trigram");
// Proportionate rank from topic model
int num=wordsrank.size();
double rank=1.0*(num/wordsrank.size());
num--;
// Adjusted probability
double prob=Double.parseDouble(lines[0])+(Double.parseDouble(inc)*(rank));
String logprob=df.format(prob);
lines[0]=logprob;
}
}
writer.write((lines[0]) + " " + lines[1] + " " + lines[2] + " " + lines[3] + "\n");
}
}
}
writer.write("\n\\end\\");
writer.close();
// testing
/*for(int i=0; i<u nigram.size(); i++)
{
System.out.println(unigram.get(i));
}
for (Map.Entry entry : testprobabilities.entrySet()) {
System.out.println(entry.getKey() + "," + entry.getValue());
}
for (Map.Entry entry : topicwords.entrySet()) {
System.out.println(entry.getKey() + "," + entry.getValue());
}*/
// delete input file and rename output as input
try{
if(file.delete()){
System.out.println(file.getName() + " is deleted!");
}else{
System.out.println("Delete operation has failed.");
}
File output =new File("resources/output.lm");
if(output.renameTo(file)){
System.out.println("Rename successful");
}else{
System.out.println("Rename failed");
}
}catch(Exception e){
e.printStackTrace();
}
// Convert .lm to .dmp
// set up the command and parameter
String ScriptPath = "resources/builddmp.sh";
String[] cmd = new String[2];
cmd[0] = "sh";
cmd[1] = ScriptPath;
// create runtime to execute external command
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(cmd);
// retrieve output from script
BufferedReader bfr = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line = "";
while((line = bfr.readLine()) != null) {
// display each output line from script
System.out.println(line);
}
}
}