-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreatingLanguageModel.java
More file actions
307 lines (251 loc) · 6.47 KB
/
Copy pathCreatingLanguageModel.java
File metadata and controls
307 lines (251 loc) · 6.47 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
package com.research.course.debate.util;
import java.io.BufferedReader;
import java.io.BufferedWriter;
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.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import com.research.course.debate.rsrelations.Pair;
import gnu.trove.list.linked.TByteLinkedList ;
public class CreatingLanguageModel {
// List<String> unigrams ;
// List<String> bigrams ;
int globalUnigPosn ;
int globalBigPosn ;
HashMap<String,Integer> unigrams ;
HashMap<String,Integer> bigrams ;
Map<Integer,Integer> bigramPairMap ;
private ArrayList<String> stopWords;
private HashMap<String, Map<Integer, Integer>> wekaBigramMap;
private Integer lineNumber;
public CreatingLanguageModel()
{
globalUnigPosn = -1 ;
globalBigPosn = -1 ;
unigrams = new HashMap<String,Integer>();
bigrams = new HashMap<String,Integer>();
wekaBigramMap = new HashMap<String,Map<Integer,Integer>>() ;
lineNumber = 1 ;
}
public String createBigramPairs ( String first, String second, String fold, String polRep )
{
String firstFeatures[] = first.split("\\s++");
String secondFeatures[] = second.split("\\s++");
int fPosn = 0 ;
int sPosn = 0 ;
bigramPairMap = new HashMap<Integer,Integer>();
//smoothing is important here!
//lets check the actual "ret" text
String retText = " " ;
List<Integer> posnList = new ArrayList<Integer>() ;
for ( int i = 0 ; i < firstFeatures.length ; i ++ )
{
String f = firstFeatures[i] ;
//need some cleaning
if(!TextUtilFunctions.checkAlphaNumeric(f))
{
continue ;
}
if(stopWords.contains(f))
{
// continue ;
}
fPosn = getUnigramPosn(f,fold);
if ( fPosn == -1 )
{
//i.e. the unigram is unsees
//return (no smoothing?)
continue ;
}
for ( int j = 0 ; j < secondFeatures.length ; j++ )
{
String s = secondFeatures[j];
if(!TextUtilFunctions.checkAlphaNumeric(s))
{
continue ;
}
if(stopWords.contains(s))
{
// continue ;
}
sPosn = getUnigramPosn(s,fold);
if ( sPosn == -1 )
{
continue ;
}
String bigram = f + "_" + s ;
int bPosn = getBigramPosn(bigram,fold);
if ( bPosn == -1 )
{
continue ;
}
Integer old = bigramPairMap.get(bPosn);
if ( old == null )
{
old = 0 ;
}
else
{
// System.out.println("here") ;
}
bigramPairMap.put(bPosn,old+1);
retText = retText + "< " + bigram + " >" + " ";
/*if (bigram.contains("the_this"))
{
System.out.println("here") ;
}*/
}
}
// System.out.println(retText);
TreeMap<Integer,Integer> indexOrder = new TreeMap<Integer,Integer>(bigramPairMap);
wekaBigramMap.put(polRep + "_" + lineNumber, indexOrder) ;
lineNumber++ ;
String ret = " " ;
for ( Integer p : indexOrder.keySet() )
{
//System.out.println(p+"\t"+indexOrder.get(p)) ;
//log value?
int val = indexOrder.get(p);
double logVal = Math.log((double)val);
ret += p +":"+val+" ";
// ret += p +":"+logVal+" ";
}
retText = retText.trim();
ret = ret.trim();
return ret ;
}
public void createWekaBigramData(String path, String fold) throws IOException
{
BufferedWriter writer = new BufferedWriter
(new OutputStreamWriter(new FileOutputStream(path + "/" + "weka-output-0208"+ fold + ".arff"),"UTF-8"));
writer.write("@relation debate-politics");
writer.newLine();
Map<String, Integer> sortedBigrams = sortByValues(bigrams);
int bigramSize = sortedBigrams.size() ;
for ( String bigram : sortedBigrams.keySet() )
{
//write the attributes
writer.write("@attribute " + "\"" + bigram + "\"" + " { t}") ;
writer.newLine();
}
//write the relations
writer.write("@attribute 'total' { D, R}");
writer.newLine();
writer.write("@data");
writer.newLine();
//write the data points
for ( String lineNum : wekaBigramMap.keySet())
{
String pol = lineNum.split("_")[0];
Map<Integer,Integer> sortedBigramPosn = wekaBigramMap.get(lineNum);
Set<Integer> sortedSetBigramPosn = sortedBigramPosn.keySet() ;
String ret = " " ;
for ( int posn = 0 ;posn < bigramSize ; posn++ )
{
if(sortedSetBigramPosn.contains(posn))
{
ret = ret + "t" + "," ;
}
else
{
ret = ret + "?" + "," ;
}
}
ret = ret + pol + " " ;
ret = ret.trim() ;
writer.write(ret);
writer.newLine();
}
writer.close();
}
public String getWekaFormatDataPoint()
{
return null ;
}
private int getUnigramPosn(String f, String fold)
{
// TODO Auto-generated method stub
if(!unigrams.containsKey(f))
{
if (fold.contains("train"))
{
globalUnigPosn++ ;
unigrams.put(f,globalUnigPosn);
}
else
{
//unigrams does not contains the word
//and we are not training
//return
return -1 ;
}
}
int fPosn = unigrams.get(f);
return fPosn;
}
private int getBigramPosn(String f, String fold)
{
// TODO Auto-generated method stub
if(!bigrams.containsKey(f))
{
if (fold.contains("train"))
{
globalBigPosn++ ;
bigrams.put(f,globalBigPosn);
}
else
{
//bigrams does not contains the word
//and we are not training
//return
return -1 ;
}
}
int fPosn = bigrams.get(f);
return fPosn;
}
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 static <K, V extends Comparable> Map sortByValues(final Map<String,Integer> map) {
Comparator valueComparator = new Comparator<K>() {
public int compare(K k1, K k2) {
int compare = ( map.get(k1)).compareTo( map.get(k2));
if (compare == 0) {
return 1;
} else {
return compare;
}
}
};
Map sortedByValues = new TreeMap<K, V>(valueComparator);
sortedByValues.putAll(map);
return sortedByValues;
}
}