-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextUtility.java
More file actions
347 lines (278 loc) · 7.17 KB
/
Copy pathTextUtility.java
File metadata and controls
347 lines (278 loc) · 7.17 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
package com.deft.sarcasm.util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class TextUtility
{
private static ArrayList<String> hashTagList;
public static int countChars(String line, char c)
{
// TODO Auto-generated method stub
int count = 0 ;
char[] chars = line.toCharArray() ;
for ( char x : chars )
{
if(x == c)
{
count++ ;
}
}
return count;
}
public static String recreate(String[] features, int start, int length)
{
// TODO Auto-generated method stub
StringBuffer buffer = new StringBuffer() ;
for ( int i = start ; i < length ; i++ )
{
buffer.append(features[i]);
buffer.append(" ") ;
}
return buffer.toString().trim();
}
public static boolean isAlphaNumeric ( String token )
{
char[] chars = token.toCharArray() ;
for ( char c : chars )
{
if ( ( 'a' <= c && c <= 'z') || ( 'A' <= c && c <= 'Z') || ( '0' <= c && c <= '9') )
{
return true ;
}
}
return false ;
}
/**
* Strips all punctuation (non-alphanumeric characters) from a word and returns the word.
* @param word The word to be modified
* @return The word, stripped of all punctuation
*/
public static String stripPunctuation(String word)
{
word = word.replaceAll("[^a-zA-Z0-9]", "");
return word;
}
public static boolean checkUppercase ( String token )
{
//if all characters are upper case - return true
char[] chars = token.toCharArray() ;
for ( char c : chars )
{
if ( ( 'A' <= c && c <= 'Z') )
{
//do nothing
}
else
{
return false ;
}
}
return true ;
}
public static boolean CheckNumeric ( String token )
{
StringBuffer buffer = new StringBuffer() ;
char[] chars = token.toCharArray() ;
for ( char c : chars )
{
if (( '0' <= c && c <= '9') )
{
}
else
{
return false ;
}
}
return true ;
}
public static String stripNonAlphaNumericAndSpecial ( String token )
{
StringBuffer buffer = new StringBuffer() ;
char[] chars = token.toCharArray() ;
for ( char c : chars )
{
if ( ( 'a' <= c && c <= 'z') || ( 'A' <= c && c <= 'Z') || ( '0' <= c && c <= '9') || (c == '@') ||
(c == '#') )
{
buffer.append(c);
}
}
return buffer.toString() ;
}
public static void loadCrimeHashtags()
{
String [] crimeHashtags = {"#crimingwhilewhite" , "#alivewhileblack" } ;
hashTagList = new ArrayList<String>(Arrays.asList(crimeHashtags)) ;
}
public static void loadHashtags()
{
String [] englishHashtags = {"#sarcasm", "#sarcastic" , "#angry" , "#awful" , "#disappointed" ,
"#excited", "#fear" ,"#frustrated", "#grateful", "#happy" ,"#hate",
"#joy" , "#loved", "#love", "#lucky", "#sad", "#scared", "#stressed",
"#wonderful", "#positive", "#positivity", "#disappointed", "#irony"} ;
//spanish
String [] spanishHashtags = {"#sarcasmo", "#sarcasm" , "#feliz" , "#alegre" , "#entusiasmado" ,
"#contento", "#gratitud" ,"#diversion", "#amor", "#enamorado" ,"#optimismo",
"#triste" , "#enojado", "#irritado", "#aterrado", "#asustado", "#asustado", "#confundido"} ;
hashTagList = new ArrayList<String>(Arrays.asList(spanishHashtags)) ;
hashTagList.addAll(Arrays.asList(englishHashtags));
}
public static boolean checkAllHashtags ( String line )
{
if (line.contains(hashTagList.get(0) ) && line.contains(hashTagList.get(0) ) )
{
return true ;
}
else
{
return false ;
}
}
public static String removeHashtags ( String line )
{
for ( String hash : hashTagList )
{
line = line.replaceAll("(?i)" +hash, "");
}
return line ;
}
public static boolean checkSarcasm(String[] tokens)
{
// TODO Auto-generated method stub
for ( String token : tokens )
{
if ( token.equalsIgnoreCase("sarcasm") || token.equalsIgnoreCase("sarcastic") )
{
return true ;
}
}
return false;
}
public static Map<String,Double> extractSentiFeaturesAsMap(Map<String, Double> sentiMap)
{
if(sentiMap.get("totalArg1Positive") > 0 && ( sentiMap.get("totalArg1Negative") > 0 ||
sentiMap.get("totalArg1NegatePositive") > 0) )
{
sentiMap.put("arg1Contrast", 1.0);
}
else
{
sentiMap.put("arg1Contrast", 0.0);
}
return sentiMap ;
}
public static String getValuesOfMap(Map<String, Double> sentiMap)
{
// TODO Auto-generated method stub
StringBuffer buffer = new StringBuffer() ;
int index = 1 ;
for ( String key : sentiMap.keySet())
{
buffer.append(index + ":" +sentiMap.get(key));
buffer.append(" ");
index++ ;
}
if(sentiMap.get("totalArg1Positive") > 0 && ( sentiMap.get("totalArg1Negative") > 0 ||
sentiMap.get("totalArg1NegatePositive") > 0) )
{
buffer.append(index+":"+"1.0");
buffer.append("\t");
index++ ;
}
else
{
buffer.append(index+":"+"0.0");
buffer.append("\t");
index++ ;
}
return buffer.toString().trim();
}
public static String getValuesOfMap(Map<String, Double> sentiMap1, Map<String, Double> sentiMap2)
{
// TODO Auto-generated method stub
StringBuffer buffer = new StringBuffer() ;
int index = 1 ;
for ( String key : sentiMap1.keySet())
{
buffer.append(index+":"+sentiMap1.get(key));
buffer.append(" ");
index++ ;
}
for ( String key : sentiMap2.keySet())
{
buffer.append(index+":"+sentiMap2.get(key));
buffer.append(" ");
index++ ;
}
if(sentiMap1.get("totalArg1Positive") > 0 && ( sentiMap2.get("totalArg1Negative") > 0 ||
sentiMap2.get("totalArg1NegatePositive") > 0) )
{
buffer.append(index+":"+"1:0");
buffer.append(" ");
index++ ;
}
else
{
buffer.append(index+":"+"0:0");
buffer.append(" ");
index++ ;
}
if(sentiMap1.get("totalArg1NegatePositive") > 0 && ( sentiMap1.get("totalArg1Negative") > 0 ||
sentiMap2.get("totalArg1Positive") > 0) )
{
buffer.append(index+":"+"1:0");
buffer.append("\t");
index++ ;
}
else
{
buffer.append(index+":"+"0:0");
buffer.append("\t");
index++ ;
}
return buffer.toString().trim();
}
public static Map<String, Double> extractSentiFeaturesAsMap(Map<String, Double> sentiMap,int context)
{
// TODO Auto-generated method stub
for ( String key : sentiMap.keySet())
{
sentiMap.put(key, sentiMap.get(key));
}
if ( context == 0 )
{
if(sentiMap.get("totalArg1Positive") > 0 && ( sentiMap.get("totalArg1Negative") > 0 ||
sentiMap.get("totalArg1NegatePositive") > 0) )
{
sentiMap.put("totalArg1Arg1Contrast", 1.0);
}
else
{
sentiMap.put("totalArg1Arg1Contrast", 0.0);
}
}
else if ( context == 1 )
{
if(sentiMap.get("totalArg1Positive") > 0 && ( sentiMap.get("totalArg2Negative") > 0 ||
sentiMap.get("totalArg2NegatePositive") > 0) )
{
sentiMap.put("totalArg1Arg2Contrast", 1.0);
}
else
{
sentiMap.put("totalArg1Arg2Contrast", 0.0);
}
if(sentiMap.get("totalArg1NegatePositive") > 0 && ( sentiMap.get("totalArg1Negative") > 0 ||
sentiMap.get("totalArg2Positive") > 0) )
{
sentiMap.put("totalArg2Arg1Contrast", 1.0);
}
else
{
sentiMap.put("totalArg2Arg1Contrast", 0.0);
}
}
return sentiMap ;
}
}