Repository navigation
Expand file tree
/
Copy pathCalculateMIPerTopic.java
More file actions
527 lines (415 loc) · 12 KB
/
Copy pathCalculateMIPerTopic.java
File metadata and controls
527 lines (415 loc) · 12 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
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
package com.research.course.debate.MI;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import jtextpro.JTextPro;
import jtextpro.JTextProcessor;
public class CalculateMIPerTopic {
/**
* @param args
*/
private JTextProcessor jtProcObj ;
private final String JTPModelPath = "./data/models/";
private Map<String,Integer> corpusMap ;
private Map<String,Map<String,Integer>> debateTopicMap ;
private Map<String,Integer> topicSpeechMap ;
class EachMIFeature implements Comparable
{
public String getFeature() {
return feature;
}
public void setFeature(String feature) {
this.feature = feature;
}
public double getMI() {
return MI;
}
public void setMI(double mI) {
MI = mI;
}
public String getTopic() {
return topic;
}
public void setTopic(String topic) {
this.topic = topic;
}
private String feature ;
private double MI ;
private String topic ;
private int global ;
private int local ;
@Override
public int compareTo(Object arg0)
{
// TODO Auto-generated method stub
double miScore1 = this.getMI() ;
double miScore2 = ((EachMIFeature) arg0).getMI() ;
if(miScore1 >= miScore2)
{
return 0;
}
else
{
return 1 ;
}
}
public void setGlobalCount(Integer globalCount)
{
// TODO Auto-generated method stub
this.global = globalCount ;
}
public int getGlobalCount()
{
return global ;
}
public void setLocallCount(Integer localCount) {
// TODO Auto-generated method stub
this.local = localCount ;
}
public int getLocal ()
{
return local ;
}
public String toString()
{
return feature +"\t" + global +"\t" + local +"\t" + MI ;
}
}
Map<String,List<EachMIFeature>> debateTopicFeatMap ;
private ArrayList<String> stopWords;
private TreeMap<String, Integer> periodCountMap;
public CalculateMIPerTopic()
{
jtProcObj = new JTextProcessor();
jtProcObj.setModelPath("./data/models/");
jtProcObj.initJTextProObject();
corpusMap = new HashMap<String,Integer>();
debateTopicMap = new HashMap<String,Map<String,Integer>>() ;
topicSpeechMap = new HashMap<String,Integer>();
debateTopicFeatMap = new HashMap<String,List<EachMIFeature>>() ;
}
public void loadStopWords ( String path ) throws IOException
{
stopWords = new ArrayList<String>();
BufferedReader reader = null ;
reader = new BufferedReader(new InputStreamReader (
new FileInputStream( path + "stop_words.txt") , "UTF8") );
//header
String line = reader.readLine();
while (true )
{
line = reader.readLine();
if ( null == line )
{
break ;
}
stopWords.add(line.trim());
}
reader.close();
}
public void countPeriod ( String path ) throws IOException
{
File file = new File ( path );
String files[] = file.list() ;
BufferedReader reader = null ;
Map<String,Integer> eachDebateMap = null ;
periodCountMap = new TreeMap<String,Integer>() ;
for ( String f : files )
{
File fl = new File (path + f );
if (fl.isDirectory())
{
continue ;
}
if ( f.contains("Store") || f.contains("zip"))
{
continue ;
}
String debate = f.substring(0,f.indexOf(".")) ;
if ( debate.contains("16"))
{
System.out.println("here1");
}
eachDebateMap = new HashMap<String,Integer>();
reader = new BufferedReader(new InputStreamReader (
new FileInputStream( path + f) , "UTF8") );
//header
String line = reader.readLine();
int index = 0 ;
while (true)
{
line = reader.readLine();
if (line == null)
{
break;
}
String features[] = line.split("\t");
String speech = features[7];
String words[] = speech.split("\\s++");
for ( String word : words )
{
word = word.trim();
if ( word.contains(".") || word.contains("?") || word.contains("!"))
{
Integer old = periodCountMap.get(word);
if ( null == old )
{
old = 0 ;
}
periodCountMap.put(word, old+1);
}
}
}
reader.close();
}
int index = 0 ;
for ( String word : periodCountMap.keySet() )
{
System.out.println(word +"\t"+ periodCountMap.get(word ));
index++;
if ( index == 100 )
{
break;
}
}
}
public void loadEachDebateTopics( String path ) throws IOException
{
File file = new File ( path );
String files[] = file.list() ;
BufferedReader reader = null ;
Map<String,Integer> eachDebateMap = null ;
for ( String f : files )
{
File fl = new File (path + f );
if (fl.isDirectory())
{
continue ;
}
if ( f.contains("Store"))
{
continue ;
}
String debate = f.substring(0,f.indexOf(".")) ;
if ( debate.contains("16"))
{
System.out.println("here1");
}
eachDebateMap = new HashMap<String,Integer>();
reader = new BufferedReader(new InputStreamReader (
new FileInputStream( path + f) , "UTF8") );
//header
String line = reader.readLine();
int index = 0 ;
while (true)
{
line = reader.readLine();
if (line == null)
{
break;
}
String features[] = line.split("\t");
String speech = features[7];
@SuppressWarnings("unchecked")
List<String>sentences = jtProcObj.getSentences(speech) ;
for ( String sentence : sentences )
{
List<String> tokens = jtProcObj.getTokens(sentence);
// List<String> tokens = jtProcObj.getPhrases(sentence);
for ( String token : tokens )
{
//global map
if(stopWords.contains(token))
{
continue ;
}
if(!checkAlphaNumeric(token))
{
continue ;
}
Integer old = corpusMap.get(token);
if ( null == old )
{
old = 0 ;
}
corpusMap.put(token, old+1);
old = eachDebateMap.get(token);
if ( null == old )
{
old = 0 ;
}
eachDebateMap.put(token, old+1);
}
}
index++;
}
reader.close();
debateTopicMap.put(debate, eachDebateMap);
topicSpeechMap.put(debate,index);
}
}
public void calculateMIForEachToken (String path) throws FileNotFoundException, IOException
{
int totalFileList = getTotalSpeechList(topicSpeechMap);
List<EachMIFeature> eachTopicMIList = null ;
for( String debate : debateTopicMap.keySet() )
{
if ( debate.contains("16"))
{
System.out.println("here1");
}
else
{
// continue ;
}
int fileCount = topicSpeechMap.get(debate) ;
Map<String,Integer > eachCatMap = debateTopicMap.get(debate);
eachTopicMIList = new ArrayList<EachMIFeature>();
EachMIFeature eachFeat = null ;
for(String token : eachCatMap.keySet() )
{
if (token.contains("milit"))
{
// System.out.println("here2");
}
Integer localCount = eachCatMap.get(token);
Integer globalCount = corpusMap.get(token);
if(null == globalCount)
{
//should not reach here...
System.out.println("check the term: "+token);
continue ;
}
double MI = calcMutInfoVal(token , globalCount ,localCount , fileCount , totalFileList );
// double tfidf = calcTFIDFIntoVal(token, localCount)
eachFeat = new EachMIFeature();
eachFeat.setTopic(debate);
eachFeat.setFeature(token);
eachFeat.setGlobalCount(globalCount);
eachFeat.setLocallCount(localCount);
eachFeat.setMI(MI);
eachTopicMIList.add(eachFeat);
}
sortingMI(path, debate, eachTopicMIList);
}
//sort the MI value
// sortingMI(catName) ;
}
private boolean checkAlphaNumeric(String token)
{
// TODO Auto-generated method stub
char[] chars = token.toCharArray();
for ( char c : chars )
{
if ( 'a' <= c && c<= 'z')
{
return true ;
}
}
return false;
}
private void sortingMI(String path, String debate, List<EachMIFeature> eachTopicMIList) throws IOException, FileNotFoundException
{
// TODO Auto-generated method stub
java.util.Collections.sort(eachTopicMIList);
Writer writer = new BufferedWriter( new OutputStreamWriter ( new FileOutputStream ( path + "/debate_MI_phrases/"+debate +".MI.txt"), "UTF8") );
writer.write("topic"+"\t"+"feature"+"\t"+"global_count"+"\t"+"local_count"+"\t"+"MI");
writer.write("\n");
int i = 0 ;
for ( EachMIFeature feature : eachTopicMIList)
{
writer.write(debate + "\t" + feature.toString());
writer.write("\n");
i++ ;
if ( i == 301)
{
// break ;
}
}
writer.close() ;
}
private int getTotalSpeechList(
Map<String, Integer> debateCountMap)
{
// TODO Auto-generated method stub
int totalSpeechCount = 0 ;
for ( String topic : debateCountMap.keySet() )
{
totalSpeechCount += debateCountMap.get(topic);
}
return totalSpeechCount ;
}
public double calcMutInfoVal(String featBuffer , int crpsCount , int catCount , int catfile_size , int featfile_size)
{
double cat_feature_11 = (double)(catCount)/(double)(featfile_size) ;
double cat_11 = (double)(catfile_size) / (double)(featfile_size) ; //hardcoded to check acq catgry only...
double feature_11 = (double)(crpsCount)/(double)(featfile_size);
double log_cat_feature_11 = Math.log10 (cat_feature_11) ;
double log_cat_11 = Math.log10(cat_11) ;
double log_feature_11 = Math.log10(feature_11) ;
double MI_11 = (cat_feature_11)*((log_cat_feature_11) - (log_cat_11 + log_feature_11))/(Math.log10(2d)) ;
double cat_feature_10 = (double)(catfile_size - catCount)/(double)(featfile_size) ;
double cat_10 = cat_11 ;
double feature_10 = (double)(featfile_size - crpsCount)/(double)(featfile_size) ;
double log_cat_feature_10 ;
if(cat_feature_10 <=0d)
log_cat_feature_10 = 0.0 ;
else
log_cat_feature_10 = Math.log10(cat_feature_10) ;
double log_cat_10 = Math.log10(cat_10) ;
double log_feature_10 = Math.log10(feature_10) ;
double MI_10 = (cat_feature_10)* ((log_cat_feature_10) - (log_cat_10 + log_feature_10))/(Math.log10(2d)) ;
double cat_feature_01 = (double)(crpsCount - catCount)/(double)(featfile_size) ;
double cat_01 = (double)(featfile_size-catfile_size)/(double)(featfile_size) ;
double feature_01 = feature_11 ;
double log_cat_feature_01 ;
if(cat_feature_01 <= 0d)
log_cat_feature_01 = 0.0;
else
log_cat_feature_01 = Math.log10(cat_feature_01) ;
double log_cat_01 = Math.log10(cat_01) ;
double log_feature_01 = Math.log10(feature_01) ;
double MI_01 = (cat_feature_01) * ((log_cat_feature_01) - ( log_cat_01 + log_feature_01))/(Math.log10(2d)) ;
double cat_feature_00 = (double)(featfile_size - (crpsCount + catfile_size) + catCount )/(double)(featfile_size) ;
double cat_00 = cat_01 ;
double feature_00 = (double)(featfile_size - crpsCount)/(double)(featfile_size) ;
double log_cat_feature_00 = Math.log10(cat_feature_00) ;
double log_cat_00 = Math.log10(cat_00) ;
double log_feature_00 = Math.log10(feature_00) ;
double MI_00 = (cat_feature_00) * (( log_cat_feature_00) - (log_cat_00 + log_feature_00 )) /(Math.log10(2d)) ;
double MI_all = MI_11 + MI_10 + MI_01 + MI_00 ;
/*
feat = new CEachFeature() ;
feat->featName = std::string(featBuffer) ;
feat->count = crpsCount ;
feat->dummy = MI_all ;
feat->checked = false ;
FeatureVector.push_back(feat);
*/
return MI_all ;
}
public static void main(String[] args) throws IOException
{
// TODO Auto-generated method stub
String path = "data/convote_v1.1/data_stage_one/training_set/topics_together/";
String config = "data/config/";
CalculateMIPerTopic calcMIObj = new CalculateMIPerTopic() ;
// calcMIObj.countPeriod(path);
calcMIObj.loadStopWords(config);
calcMIObj.loadEachDebateTopics(path);
calcMIObj.calculateMIForEachToken(path);
}
}