-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokenization.java
More file actions
414 lines (309 loc) · 9.97 KB
/
Copy pathTokenization.java
File metadata and controls
414 lines (309 loc) · 9.97 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
package com.rutgers.util;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import opennlp.tools.tokenize.TokenizerME;
import opennlp.tools.tokenize.TokenizerModel;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.lang3.text.StrTokenizer;
public class Tokenization
{
/**
* @param args
* @throws IOException
*/
private static PorterStemmerWest stemmer = new PorterStemmerWest();
private static TokenizerModel model;
private static TokenizerME tokenizer;
private static final String OPENNLP_TOKENFILE = "en-token.bin" ;
private static Counter<String> unigrams = new Counter<String>();
private static CMUTokenization cmuTokenizer = new CMUTokenization();
private static final String CONFIG = "./data/config/" ;
private static final String STOP_FILE = "stopwords_small.txt" ;
private static final String EXCEPTION_FILE = "exceptionWord.lst" ;
private static HashSet<String> allUtterances ;
private static String [] hashtags = {"#sarcasm", "#sarcastic" , "#angry" , "#awful" , "#disappointed" ,
"#excited", "#fear" ,"#frustrated", "#grateful", "#happy" ,"#hate",
"#joy" , "#loved", "#love", "#lucky", "#sad", "#scared", "#stressed",
"#wonderful", "#positive", "#positivity", "#disappointed", "#irony"} ;
private static HashSet<String> uniqueTweets;
public static List<String> loadStopWords() throws IOException
{
List<String >stopList = new ArrayList<String>() ;
BufferedReader reader = new BufferedReader ( new FileReader (CONFIG + "stopwords_small.txt"));
while (true )
{
String line = reader.readLine();
if ( null == line )
{
break;
}
stopList.add(line.trim());
}
reader.close();
return stopList ;
}
public static void initStemmer() throws IOException
{
String exception = CONFIG + "/" + EXCEPTION_FILE ;
stemmer.initExceptionWords(exception) ;
String stopwords = CONFIG + "/" + STOP_FILE ;
stemmer.initStopWords(stopwords) ;
}
public static void tokenization(String input, String output,
String opFile, boolean stem) throws IOException
{
allUtterances = new HashSet<String>() ;
FileInputStream nlpStream = new FileInputStream(CONFIG + OPENNLP_TOKENFILE );
// model = new TokenizerModel(nlpStream);
// tokenizer = new TokenizerME(model);
BufferedWriter writer1 = new BufferedWriter ( new OutputStreamWriter (
new FileOutputStream ( output + "/" + opFile + ".tokens" ), "UTF8")) ;
BufferedWriter writer2 = new BufferedWriter ( new OutputStreamWriter (
new FileOutputStream ( output + "/" + opFile + ".tokens.unigrams" ), "UTF8")) ;
File file = new File ( input ) ;
File[] files = file.listFiles() ;
for ( File f : files )
{
if (f.isDirectory())
{
continue ;
}
if ( f.getName().startsWith("Store"))
{
continue ;
}
// if ( !f.getName().endsWith("filtered"))
// {
// continue ;
// }
tokenization(input,f.getName(),writer1,writer2,stem) ;
}
writer1.close() ;
System.out.println("Finished tokenization") ;
for ( String unigram : unigrams.keySet())
{
writer2.write(unigram + "\t" + unigrams.getCount(unigram));
writer2.newLine() ;
}
writer2.close();
}
public static void createUnigrams(String input, String output, String opFile) throws IOException
{
//this is for training/testing issues
//create the unigram counts for training data (sarcasm and positive)
File f = new File ( input ) ;
File[] files = f.listFiles() ;
BufferedReader reader = null ;
BufferedWriter writer2 = new BufferedWriter ( new OutputStreamWriter (
new FileOutputStream ( output + "/" + opFile ), "UTF8")) ;
uniqueTweets = new HashSet<String>() ;
for ( File file : files )
{
if ( file.getName().contains("Store"))
{
continue ;
}
//tweet.sarcasm.filtered.031012015.lemma.emoji.targets.all
if ( file.getName().endsWith("lemma.emoji.targets.all"))
{
reader = new BufferedReader ( new InputStreamReader (
new FileInputStream ( input + "/" + file.getName() ),"UTF8") );
System.out.println("file reading is " + file.getName()) ;
while ( true )
{
String line = reader.readLine();
if ( null == line )
{
break ;
}
String features[] = line.split("\t") ;
String snum = features[1] ;
String utterance = features[2] ;
if (!uniqueTweets.contains(snum))
{
uniqueTweets.add(snum);
}
else
{
continue ;
}
features = utterance.split("\\s++") ;
for ( String feature : features )
{
unigrams.incrementCount(feature, 1.0);
}
}
reader.close();
}
}
System.out.println("Finished tokenization") ;
double idfNeum = (double) uniqueTweets.size() ;
for ( String unigram : unigrams.keySet())
{
//we can calculate the idf here
//lets consider for each tweet the number of entries for the unigram is only once
//so the count of it will show the denom of idf
double idfDenom = unigrams.getCount(unigram) ;
double idf = Math.log(idfNeum/idfDenom) ;
writer2.write(unigram + "\t" + idfDenom + "\t" + idf);
writer2.newLine() ;
}
writer2.close();
}
public static void tokenization(String path, String file,
BufferedWriter writer1, BufferedWriter writer2, boolean stem) throws IOException
{
BufferedReader reader = new BufferedReader ( new InputStreamReader (
new FileInputStream ( path + "/" + file ),"UTF8") );
System.out.println("READING FILE: " + file);
List<String> stopList = loadStopWords() ;
while ( true )
{
String message = reader.readLine() ;
if ( null == message )
{
break ;
}
String line = message ;
// String snum = message.split("\t")[1] ;
// String hash = message.split("\t")[4] ;
// String line = message.split("\t")[5] ;
// line = "Thanks #TimeWarner for being on the fritz, I didn't want to see #ChicagoFire anyway... #NOT" ;
// System.out.println(line) ;
// StrTokenizer tokenizer = new StrTokenizer(line) ;
line = StringUtils.stripAccents(line) ;
line = TextUtility.removeHashes(line,hashtags);
// StringTokenizer tokenizer = new StringTokenizer(line.trim());
//probably the CMU one or the Stanford glove are the best tokenizers for tweets
List<String> tokens = cmuTokenizer.tokenizeRawTweetText(line);
StringBuffer buffer = new StringBuffer() ;
for ( String token : tokens )
{
if(stopList.contains(token.trim()))
{
continue ;
}
if (token.startsWith("@"))
{
token = "@ToUser" ;
}
boolean number = NumberUtils.isNumber(token);
if(number)
{
token = "22" ; //all numbers are 22!
}
if (token.startsWith("http") || token.startsWith("HTTP") )
{
token = "URL" ;
}
if(token.startsWith("://"))
{
continue ;
}
// if( TextUtility.CheckNonAlpha(token) )
{
//if we use CMU tokenizer - may be we dont have to do this?
// token = TextUtility.stripNonAlpha(token);
}
//if all the characters are UPPER CASE - keep that
if(!TextUtility.checkUppercase(token))
{
token = token.toLowerCase();
}
// if (token.length() < 2)
// {
// continue ;
// }
// token = "yeaaaaaaaaaaaaaaaaaaaah" ;
// token = "yeaaah" ;
// Pattern p = Pattern.compile("(((\\w)\\3+)+)+");
// Matcher m = p.matcher(token);
// if (m.find())
// {
// System.out.println("Duplicate character " + m.group(1));
// }
//now stem it
// System.out.println(token) ;
// token = "yeaaaaaaaaaaaaaaaaaaaah" ;
// token ="suuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuureeeee" ;
if(stem)
{
token = stemmer.stemWord(token);
}
buffer.append(token);
buffer.append(" ");
}
String ret = buffer.toString().trim() ;
// System.out.println(ret);
//dont write the buffer directly as there are too many repeats!!!
//we can store everything in a hashset now and write later
if(!allUtterances.contains(ret))
{
allUtterances.add(ret);
//do a simple split on //s++
//little clumsy code but will work!
String finalTokens[] = ret.split("\\s++") ;
for ( String token : finalTokens )
{
unigrams.incrementCount(token, 1.0);
}
// writer1.write(snum + "\t" + ret);
writer1.write(ret);
writer1.newLine();
}
else
{
// System.out.println("present~") ;
}
}
reader.close();
}
private static String getString(String[] outputs)
{
// TODO Auto-generated method stub
StringBuffer buffer = new StringBuffer() ;
for ( String output : outputs )
{
buffer.append(output);
buffer.append(" ") ;
}
return buffer.toString().trim();
}
public static void main(String[] args) throws IOException
{
// TODO Auto-generated method stub
// String input = "./data/input/wordnet_input/";
// String output = "./data/input/wordnet_tokens/";
// String file = "text.wem" ;
// String input = "./data/input/positive_training/" ;
// String output = "./data/input/positive_tokens/" ;
// String opFile = "tweet.positive.03032015.training.lemma.emoji.unigrams" ;
// String input = "./data/input/positive_training/" ;
String input = "./data/input/sarcasm_tokens/" ;
String output = input ;
String opFile = "tweet.sarcasm.03182015.training.lemma.emoji.unigrams" ;
// tweet.sarcasm.03032015.training.lemma.emoji.unigrams
boolean stem = false ;
if ( stem)
{
initStemmer() ;
}
// tokenization(input,output,file,stem) ;
createUnigrams(input,output,opFile);
}
}