forked from yenhao/EmotionDetection_API
-
Notifications
You must be signed in to change notification settings - Fork 1
/
classifier.py
376 lines (316 loc) · 11.2 KB
/
classifier.py
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
# -*- coding:utf-8 -*-
import codecs
import unicodedata
import re
from os import listdir
from os.path import isfile, join, basename
import numpy as np
import multiprocessing as mp
import sys
from multiprocessing import Process
from Regemotest import pattern_match
from time import gmtime, strftime
reload(sys) # Reload does the trick!
sys.setdefaultencoding('UTF8')
def remove_control_characters(s):
return u"".join(ch for ch in s if unicodedata.category(ch)[0]!="C")
def loadModels(path):
print ("Loading Models")
models = {}
onlyFiles = [ f for f in listdir(path) if isfile(join(path,f)) ]
for f in onlyFiles:
if "DS_Store" in f:
continue
print ("Processing "+f)
emotion = f.split(".")[0]
f = join(path,f)
f = codecs.open(f,"r",encoding='utf-8')
patts = {}
pos = 0
for line in f:
#print line
tokens = line.split("\t")
pattern = remove_control_characters(tokens[0].strip())
pos = pos + 1
patts[pattern] = pos
models[emotion] = patts
return models;
def loadMatrix(path):
#print "Loading matrix "+path
f = codecs.open(path,"r",encoding='utf-8')
emotions = {}
patterns = {}
matrix = []
inEmotions = 0
inPatterns = 0
inMatrix = 0
for line in f:
line = line.strip()
if "Emotions" in line:
inEmotions = 1
continue
if "Patterns" in line:
inPatterns = 1
inEmotions = 0
continue
if "Matrix" in line:
inMatrix = 1
inPatterns = 0
inEmotions = 0
continue
if inEmotions and len(line) > 0:
#print "Emotion "+line
tokens = line.split("\t");
emotions[int(tokens[0])] = remove_control_characters(tokens[1].strip())
if inPatterns and len(line) > 0:
tokens = line.strip().split("\t")
patt = remove_control_characters(tokens[1].strip())
patterns[int(tokens[0])] = patt
'''if len(patt) < 2:
print "Small pattern "+patt'''
if inMatrix and len(line) > 0:
row = [];
tokens = line.split(" ");
for val in tokens:
row.append(float(val))
matrix.append(row)
return emotions, patterns, np.array(matrix)
def evalWithMultiple(post, emotion,emotionModel):
post = remove_control_characters(post)
finalScore = 0.0
for patt in emotionModel:
#prog = re.compile(patt)
#result = prog.finditer(post)
#result = re.finditer(pattern, line, re.UNICODE)
#for match in result:
#print patt+" "+match.group(0)+" "+str(emotionModel[patt] )
# finalScore = finalScore + emotionModel[patt];
sep_pattern = patt.split("<pw>")
i = 0
combine_word = u""
while i < len(word_list):
word = word_list[i]
if word == " " or word == " ":
word = u"_blank_"
if sep_pattern[0] != "":
if i == 0:
if word in sep_pattern[0]:
combine_word = word
if combine_word == sep_pattern[0]:
#i += (len(sep_pattern) - 1)
combine_word = u""
if (i + (len(sep_pattern) - 1)) < len(word_list):
count += 1
else:
combine_word = u""
else:
if word in sep_pattern[0]:
combine_word = combine_word + word
if combine_word == sep_pattern[0]:
#i += (len(sep_pattern) - 1)
combine_word = u""
#print pattern + "\t" + str(i) + "\t" + str(len(word_list))
if (i + (len(sep_pattern) - 1)) < len(word_list):
count += 1
#print pattern
else:
combine_word = u""
else:
pos = len(sep_pattern)-1
if i == 0:
if word in sep_pattern[pos]:
combine_word = word
if combine_word == sep_pattern[pos]:
if ((i - len(combine_word) + 1) - pos) >= 0:
count += 1
combine_word = u""
else:
combine_word = u""
else:
if word in sep_pattern[pos]:
combine_word = combine_word + word
if combine_word == sep_pattern[pos]:
if ((i - len(combine_word) + 1) - pos) >= 0:
count += 1
#print pattern + "\t" + str(i) + "\t" + str(pos)
combine_word = u""
else:
combine_word = u""
i += 1
print (emotion+"\t"+str(finalScore))
return finalScore
def evalWithMatrix(post,emotions,patterns,matrix):
print('[Classifier] {} - Processing : {}'.format(strftime("%Y-%m-%d %H:%M:%S", gmtime()) ,post))
'''
Start to handle emotion classifier
'''
post = remove_control_characters(post)
word_list = pattern_match(post)
vector = []
for index in patterns:
#print index
patt = patterns[index]
count = 0;
sep_pattern = patt.split("<pw>")
i = 0
combine_word = u""
while i < len(word_list):
word = word_list[i]
if sep_pattern[0] != "":
if i == 0:
if word in sep_pattern[0]:
combine_word = word
if combine_word == sep_pattern[0]:
combine_word = u""
if (i + (len(sep_pattern) - 1)) < len(word_list):
count += 1
else:
combine_word = u""
else:
if word in sep_pattern[0]:
combine_word = combine_word + word
if combine_word == sep_pattern[0]:
combine_word = u""
if (i + (len(sep_pattern) - 1)) < len(word_list):
count += 1
else:
combine_word = u""
else:
pos = len(sep_pattern)-1
if i == 0:
if word in sep_pattern[pos]:
combine_word = word
if combine_word == sep_pattern[pos]:
if ((i - len(combine_word) + 1) - pos) >= 0:
count += 1
combine_word = u""
else:
combine_word = u""
else:
if word in sep_pattern[pos]:
combine_word = combine_word + word
if combine_word == sep_pattern[pos]:
if ((i - len(combine_word) + 1) - pos) >= 0:
count += 1
combine_word = u""
else:
combine_word = u""
i += 1
vector.append(count)
vector = np.transpose(np.array(vector))
#print vector
result = np.dot(matrix,vector)
order = np.argsort(result)
result_dict = {}
for i in order:
emotion = emotions[i]
score = result[i]
result_dict[emotion] = score
#print emotion + "\t" +str(score)
emotion = emotions[order[0]]
emotion2 = emotions[order[1]]
#news:88880.0 joy:85079.0 anticipation:70876.0 fear:51043.0 anger:71064.0 surprise:78956.0 trust:76015.0 sadness:63041.0 disgust:61514.0
if result_dict[emotion] == 0 and result_dict[emotion2] == 0:
ambiguous = "True"
else:
ambiguous = "False"
post_emotion = {
"message": post,
"emotion1": emotion,
"emotion2": emotion2,
"ambiguous": ambiguous
}
# print(post_emotion)
return post_emotion
minFreq = 20
matrixPath = 'matrix_' + str(minFreq)
print('[Classifier] {} - Loading Matrix : {}'.format(strftime("%Y-%m-%d %H:%M:%S", gmtime()) ,matrixPath))
'''
Multi processing needed
'''
Matrix = loadMatrix(matrixPath)
print('[Classifier] {} - Using {} Core to process emotion classifier'.format(strftime("%Y-%m-%d %H:%M:%S", gmtime()) ,mp.cpu_count()-1))
def classifyUsingMatrixMulti(data, Matrix = Matrix):
'''
Input:
posts [JSON]
e.g.
{ data : [
{
"message": "抽顯卡做研究 DeepLearning + 黑色沙漠",
...
},
{
"message": "送趙奕誠出國報conference",
...
}...
]
}
Output:
emotion_result [JSON]
e.g.
{ data : [
{
"message": "抽顯卡做研究 DeepLearning + 黑色沙漠",
"emotion1": "anger",
"emotion2": "haha",
"ambiguous" "True"
},
{
"message": "送趙奕誠出國報conference",
"emotion1": "Sad",
"emotion2": "anger",
"ambiguous" "False"
}...
]
}
'''
pool = mp.Pool(processes=mp.cpu_count()-1)
[emotions,patterns,matrix] = Matrix
emotion_result = {"data":[]}
multi_res = [pool.apply_async(evalWithMatrix, (post.get('message'),emotions,patterns,matrix,)) for post in data.get('data') if post.get('message')] # for multi processing
emotion_result['data'] = [res.get(timeout=1) for res in multi_res]
return emotion_result
def classifyUsingMatrix(data, Matrix = Matrix):
'''
Input:
posts [JSON]
e.g.
{ data : [
{
"message": "抽顯卡做研究 DeepLearning + 黑色沙漠",
...
},
{
"message": "送趙奕誠出國報conference",
...
}...
]
}
Output:
emotion_result [JSON]
e.g.
{ data : [
{
"message": "抽顯卡做研究 DeepLearning + 黑色沙漠",
"emotion1": "anger",
"emotion2": "haha",
"ambiguous" "True"
},
{
"message": "送趙奕誠出國報conference",
"emotion1": "Sad",
"emotion2": "anger",
"ambiguous" "False"
}...
]
}
'''
[emotions,patterns,matrix] = Matrix
emotion_result = {"data":[]}
post = data.get('data')[0].get('message')
if post:
emotion_result['data'] = [evalWithMatrix(post,emotions,patterns,matrix)]
else:
emotion_result['data'] = ['Empty Message']
return emotion_result