forked from as409/Pipelined-Sentiment-Classifier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreProcess.py
More file actions
238 lines (208 loc) · 7.45 KB
/
PreProcess.py
File metadata and controls
238 lines (208 loc) · 7.45 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
from __future__ import unicode_literals
from __future__ import division
from __future__ import absolute_import
from __future__ import print_function
from __future__ import with_statement
from subprocess import Popen, PIPE
try:
# python 2.x
from string import punctuation, letters
except:
# python 3.x
from string import punctuation, ascii_letters
import re
import tokenizer
import nltk
# Some elements from http://en.wikipedia.org/wiki/List_of_emoticons
emoticons = { ':-)' : 'happy_abc',
':)' : 'happy_abc',
':o)' : 'happy_abc',
':]' : 'happy_abc',
':3' : 'happy_abc',
':c)' : 'happy_abc',
':>' : 'happy_abc',
'=]' : 'happy_abc',
'8)' : 'happy_abc',
'=)' : 'happy_abc',
':}' : 'happy_abc',
':^)' : 'happy_abc',
':-))' : 'happy_abc',
'|;-)' : 'happy_abc',
":'-)" : 'happy_abc',
":')" : 'happy_abc',
'\o/' : 'happy_abc',
'*\\0/*': 'happy_abc',
':-l' : 'laugh_abc',
':l' : 'laugh_abc',
'8-l' : 'laugh_abc',
'8D' : 'laugh_abc',
':D' : 'laugh_abc',
'x-l' : 'laugh_abc',
'xD' : 'laugh_abc',
'X-l' : 'laugh_abc',
'XD' : 'laugh_abc',
'=-l' : 'laugh_abc',
'=l' : 'laugh_abc',
'=-3' : 'laugh_abc',
'=3' : 'laugh_abc',
'B^l' : 'laugh_abc',
'>:[' : 'sad_abc',
':-(' : 'sad_abc',
':(' : 'sad_abc',
':-c' : 'sad_abc',
':c' : 'sad_abc',
':-<' : 'sad_abc',
':<' : 'sad_abc',
':-[' : 'sad_abc',
':[' : 'sad_abc',
':{' : 'sad_abc',
':-||' : 'sad_abc',
':@' : 'sad_abc',
":'-(" : 'sad_abc',
":'(" : 'sad_abc',
'l:<' : 'sad_abc',
'l:' : 'sad_abc',
'D8' : 'sad_abc',
'l;' : 'sad_abc',
'l=' : 'sad_abc',
'DX' : 'sad_abc',
'v.v' : 'sad_abc',
"l-':" : 'sad_abc',
'(>_<)' : 'sad_abc',
':|' : 'sad_abc',
'>:O' : 'surprise_abc',
':-O' : 'surprise_abc',
':-o' : 'surprise_abc',
':O' : 'surprise_abc',
':O' : 'surprise_abc',
'o_O' : 'surprise_abc',
'o_0' : 'surprise_abc',
'o.O' : 'surprise_abc',
'8-0' : 'surprise_abc',
'|-O' : 'surprise_abc',
';-)' : 'wink_abc',
';)' : 'wink_abc',
'*-)' : 'wink_abc',
'*)' : 'wink_abc',
';-]' : 'wink_abc',
';]' : 'wink_abc',
';l' : 'wink_abc',
';^)' : 'wink_abc',
':-,' : 'wink_abc',
'>:P' : 'tong_abc',
':-P' : 'tong_abc',
':P' : 'tong_abc',
'X-P' : 'tong_abc',
'x-p' : 'tong_abc',
'xp' : 'tong_abc',
'XP' : 'tong_abc',
':-p' : 'tong_abc',
':p' : 'tong_abc',
'=p' : 'tong_abc',
':-b' : 'tong_abc',
':b' : 'tong_abc',
':-&' : 'tong_abc',
':&' : 'tong_abc',
'>:\\' : 'annoyed_abc',
'>:/' : 'annoyed_abc',
':-/' : 'annoyed_abc',
':-.' : 'annoyed_abc',
':/' : 'annoyed_abc',
':\\' : 'annoyed_abc',
'=/' : 'annoyed_abc',
'=\\' : 'annoyed_abc',
':L' : 'annoyed_abc',
'=L' : 'annoyed_abc',
':S' : 'annoyed_abc',
'>.<' : 'annoyed_abc',
':-|' : 'annoyed_abc',
'<:-|' : 'annoyed_abc',
':-X' : 'seallips_abc',
':X' : 'seallips_abc',
':-#' : 'seallips_abc',
':#' : 'seallips_abc',
'O:-)' : 'angel_abc',
'0:-3' : 'angel_abc',
'0:3' : 'angel_abc',
'0:-)' : 'angel_abc',
'0:)' : 'angel_abc',
'0;^)' : 'angel_abc',
'>:)' : 'devil_abc',
'>;)' : 'devil_abc',
'>:-)' : 'devil_abc',
'}:-)' : 'devil_abc',
'}:)' : 'devil_abc',
'3:-)' : 'devil_abc',
'3:)' : 'devil_abc',
'o/\o' : 'highfive_abc',
'^5' : 'highfive_abc',
'>_>^' : 'highfive_abc',
'^<_<' : 'highfive_abc',
'<3' : 'heart_abc'
}
def pre_process(tweet_messages):
count = 0
for temp in tweet_messages:
for i in temp:
if (i==' '):
count+=1
count+=1
ark_tweet_cmd = ['/home/akhil/Desktop/TwitterHybridClassifier/Tools/ark-tweet-nlp/runTagger.sh', '--input-format', 'text', '--output-format', 'conll', '--no-confidence', '--quiet']
tweets = '\n'.join(tweet_messages)
#print (len(tweets))
tweets = tweets.encode("ascii","ignore")
# Run the tagger and get the output
p = Popen(ark_tweet_cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE,shell=True)
(stdout, stderr) = p.communicate(input=tweets)
stdout = stdout.decode('utf8','ignore')
ark_tweet_output = stdout.strip() + '\n'
#print (len(ark_tweet_output))
# Check the return code.
if p.returncode != 0:
print ('Tools/ark-tweet-nlp/runTagger.sh command failed! Details: %s\n%s' % (stderr,ark_tweet_output))
return None
tweet_tokens_list = list()
tweet_tokens = list()
lines = ark_tweet_output.split('\n')
for line in lines:
values = re.split(r'[ \t]',line)
values = [t for t in values if len(t) != 0]
if len(values) == 0:
tweet_tokens_list.append(tweet_tokens)
tweet_tokens = list()
continue
try:
for i in range(len(values)):
tweet_tokens.append(values[i])
except:
print ('Error reading art tweet tagger output line: ' + line)
#print (tweet_tokens_list)
for tweet_tokens in tweet_tokens_list:
l = len(tweet_tokens)-count
l = (int(l/3))
#print (l)
for i in range(l):
token = tweet_tokens[i]
tag = tweet_tokens[i+l]
# substitute mentions
if tag == '@':
tweet_tokens[i] = ('&mention')
# substitute urls
if tag == 'U':
tweet_tokens[i] = ('&url')
# substitute emoticions
if tag == 'E':
tweet_tokens[i] = (emoticons.get(token,'_'))
# return the tweet in the format [(word,tag),...]
yup=[]
for twe in tweet_tokens_list:
for i in range(l):
k = twe[i]
# print (k)
k.encode('ascii', 'ignore')
yup.append(k)
yup.append(' ')
str1=''.join(str(i) for i in yup)
c=tokenizer.tokenize(str1)
tweet_tokens=nltk.pos_tag(c)
return tweet_tokens