-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject_interface.py
367 lines (253 loc) · 13.5 KB
/
project_interface.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
from itertools import cycle
from shutil import get_terminal_size
from threading import Thread
from time import sleep
from collections import Counter
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import pyterrier as pt
import os, datetime, string, re
from joblib import dump, load
from sklearn.model_selection import train_test_split
import socket
socket.getaddrinfo('localhost', 8080)
# os.environ["JAVA_HOME"] ='C:/Program Files/Java/jdk-18.0.1.1'
if not pt.started():
pt.init()
SEED=42
RANDOM_STATE = 0
from textblob import TextBlob
import nltk
nltk.download("wordnet")
nltk.download("stopwords")
from nltk.corpus import wordnet, stopwords
from nltk.stem.porter import PorterStemmer
from nltk.tokenize import word_tokenize
# from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier
import fastrank
# import lightgbm as lgb
stop_words = set(stopwords.words('english'))
stemmer = PorterStemmer()
stop_words = stop_words.union({'http','https','www','href','src','img'})
stop_words = stop_words.union({'ll','re','ve'})
t_contractions = {'cant', 'wonnot', 'shouldnt', 'arent', 'daresnt', 'shallnt', 'wont', 'shant', 'darent', 'maynt', 'wat', \
'oughtnt', 'couldnt', 'hadnt', 'didnt', 'yallrent', 'yaint', 'wasnt', 'dont', 'isnt', 'werent', 'doesnt', 'aint', 'havent', \
'wouldnt', 'hasnt', 'neednt', 'mustnt', 'amnt', 'dasnt', 'mightnt', 'idnt', 'willnt'}
negations = {'no','not','cannot'}.union(t_contractions)
emotion_map = {'sadness':0, 'joy':1, 'love':2, 'anger':3,'fear':4,'surprise':5, 0: 'sadness', 1: 'joy', 2: 'love', 3: 'anger', 4: 'fear', 5: 'surprise'}
emotions = ['sadness', 'joy', 'love', 'anger','fear','surprise']
def create_ngrams(text, nrange=(1, 1)):
text_features = []
text = [stemmer.stem(w) for w in text.split() if w not in stop_words and len(w) > 1]
text = [a[:-1] if a in t_contractions else a for a in text]
for n in range(nrange[0], nrange[1]+1):
text_features += nltk.ngrams(text, n)
return dict(Counter(text_features)) # Tf
def sentiment_textblob(row):
classifier = TextBlob(row)
polarity = classifier.sentiment.polarity
subjectivity = classifier.sentiment.subjectivity
return polarity,subjectivity
# credit for Loader class: https://stackoverflow.com/a/66558182
class Loader:
def __init__(self, desc="Loading...", end="Done!", timeout=0.1):
"""
A loader-like context manager
Args:
desc (str, optional): The loader's description. Defaults to "Loading...".
end (str, optional): Final print. Defaults to "Done!".
timeout (float, optional): Sleep time between prints. Defaults to 0.1.
"""
self.desc = desc
self.end = end
self.timeout = timeout
self._thread = Thread(target=self._animate, daemon=True)
self.steps = ["⢿", "⣻", "⣽", "⣾", "⣷", "⣯", "⣟", "⡿"]
self.done = False
def start(self):
self._thread.start()
return self
def _animate(self):
for c in cycle(self.steps):
if self.done:
break
print(f"\r{self.desc} {c}", flush=True, end="")
sleep(self.timeout)
def __enter__(self):
self.start()
def stop(self):
self.done = True
cols = get_terminal_size((80, 20)).columns
print("\r" + " " * cols, end="", flush=True)
print(f"\r{self.end}", flush=True)
def __exit__(self, exc_type, exc_value, tb):
# handle exceptions with those variables ^
self.stop()
def main():
print('\nEmotion Classifier and Search Engine')
loader = Loader("Loading...", "Loading...done", 0).start()
vectorizer = load('project_classify_models/vectorizer_0.25.joblib')
clf = load( "project_classify_models/project_classify_LinearSVC.joblib")
docs_df = pd.read_pickle('emotion/datasets/Emotion Dataset for Emotion Recognition Tasks/docs_df.pkl')
# may require downloading nltk packages
emotion_synonyms, emotion_antonyms = dict(), dict()
for i in pd.unique(docs_df['emotion']):
emotion_synonyms[i], emotion_antonyms[i] = set(), set()
for syn in wordnet.synsets(i):
for l in syn.lemmas():
if '_' not in l.name():
emotion_synonyms[i].add(l.name())
if l.antonyms() and '_' not in l.antonyms()[0].name():
emotion_antonyms[i].add(l.antonyms()[0].name())
emotion_terms_all = {stemmer.stem(i) for a in emotion_synonyms for i in emotion_synonyms[a] }.union({stemmer.stem(i) for a in emotion_antonyms for i in emotion_antonyms[a] })
pos_pref = ['NN', 'PR', 'VB', 'RB', 'MD', 'JJ', 'IN']
pos_list = list(docs_df.pos.explode().unique())
negations = {'no','not','arent', 'isnt', 'wasnt', 'werent', 'cant', 'couldnt', 'mustnt', 'shouldnt', 'wont', 'wouldnt', 'didnt', 'doesnt', 'dont', 'hasnt', 'havent', 'hadnt'}
pos_pref = ['NN', 'PR', 'VB', 'RB', 'MD', 'JJ', 'IN']
numeric_columns = ["polarity", "subjectivity", "er", "neg"] + pos_pref
index_dir = os.path.join(os.getcwd(),'indices','carer7') #####
index_ref = pt.IndexRef.of(os.path.join(index_dir, "data.properties"))
index = pt.IndexFactory.of(index_ref)
metadata = ['docno', 'emotion', 'polarity', 'subjectivity', 'er', 'neg', 'NN', 'PR', 'VB', 'RB', 'MD', 'JJ', 'IN']
termpipelines = "PorterStemmer"
# tf = pt.BatchRetrieve(index, metadata=metadata, wmodel="Tf", properties={"termpipelines" : termpipelines})
# tfidf = pt.BatchRetrieve(index, metadata=metadata, wmodel="TF_IDF", properties={"termpipelines" : termpipelines})
bm25 = pt.BatchRetrieve(index, metadata=metadata, wmodel="BM25", properties={"termpipelines" : termpipelines})
ltr_feats1 = bm25 >> (
pt.transformer.IdentityTransformer()
**
pt.FeaturesBatchRetrieve(index, wmodel="TF_IDF", features=["WMODEL:DirichletLM"], properties={"termpipelines" : ""})
**
pt.apply.doc_features(lambda row: np.array([float(row[a]) for a in numeric_columns]))
)
topics = docs_df[['emotion']].drop_duplicates(ignore_index=True).rename(columns={'emotion':'query'})
topics['qid'] = [str(a) for a in range(topics.shape[0])]
# topics
qrels = docs_df.merge(topics, left_on='emotion', right_on='query', how='inner')[['qid','docno','label']]
qrels.columns = ['qid','docno','label']
# qrels
num_queries_per_emotion = 100
test_qrels = pd.DataFrame()
for label in pd.unique(qrels.label):
test_qrels = pd.concat([test_qrels, qrels.loc[qrels.label == label,:].sample(num_queries_per_emotion, random_state=0)], axis=0)
tr_va_qrels = qrels.drop(test_qrels.index)
train_qrels, valid_qrels = train_test_split(tr_va_qrels, test_size=2/10, random_state=SEED)
# importing only the best ML model
# note that BM25 and TF-IDF both outperform it
train_request = fastrank.TrainRequest.coordinate_ascent()
params = train_request.params
params.init_random = True
params.normalize = True
params.seed = 1234567
ca_pipe = ltr_feats1 >> pt.ltr.apply_learned_model(train_request, form='fastrank')
ca_pipe.fit(topics, train_qrels)
loader.stop()
def create_features_single_query(text, nrange=(1, 1)):
text = text.translate(str.maketrans('', '', string.punctuation))
res = pd.DataFrame([text], columns=['text'])
# copied from steps above, which were applied to docs_df
res['tok'] = [word_tokenize(a) for a in res.text]
res['stem'] = [[stemmer.stem(b) for b in a] for a in res.tok]
res['pos_tup'] = [nltk.tag.pos_tag(a) for a in res.tok]
res['pos'] = [[b[1] for b in a] for a in res.pos_tup]
res[['polarity', 'subjectivity']] = pd.DataFrame([sentiment_textblob(a) for a in res.text], columns=['polarity', 'subjectivity'])
for pref in pos_pref:
tags = {a for a in pos_list if pref in a}
res[pref] = [sum(1 for tag in row if tag in tags) for row in res.pos]
res['pos_sum'] = res[pos_pref].sum(axis=1)
res[pos_pref] = res[pos_pref].div(res['pos_sum'], axis=0)
res['er'] = [sum(1 for tag in row if tag in emotion_terms_all) for row in res.stem] / res['pos_sum']
res['neg'] = [sum(1 for tag in row if tag in negations) for row in res.tok] / res['pos_sum']
ng = create_ngrams(res['text'].item(), nrange)
res.columns = [tuple([a.upper()]) for a in res.columns]
ret = [{**res[[tuple([a.upper()]) for a in numeric_columns]].to_dict(orient='index')[0], **ng}]
return ret
def predict_single_query(text, fitted_clf, vectorizer):
feats = create_features_single_query(text, (1,4))
print(feats)
feats_vec = vectorizer.transform(feats)
print(feats_vec)
ret = fitted_clf.predict(feats_vec)
return ret
inp = ''
while inp != 'exit':
loader = Loader("", "", 0).start()
sleep(0.5)
loader.stop()
print('\nWhat would you like to do? Choices:')
print(" 1 -> Classify emotion and display similar messages from the corpus")
print(" 2 -> Classify emotions of queries in a text file")
print(" 3 -> Get the emotion distribution about a topic from the corpus")
print("exit -> exit program")
inp = input("\nEnter choice -> ")
if inp == '1':
inp = input("Enter query -> ")
inp = re.sub(r'[0-9]', '', inp).translate(str.maketrans('', '', string.punctuation))
num_messages = int(input("Enter number of messages to retrieve (minimum is 20) -> "))
num_messages = max(20, num_messages)
predicted_emotion = predict_single_query(inp, clf, vectorizer)[0]
messages = ca_pipe.search(inp)
if messages.empty:
# PyTerrier bug: https://github.com/terrier-org/pyterrier/issues/352
print('Sorry! No results found. Try something else.')
continue
messages = pd.DataFrame(messages.head(num_messages)[['docno','emotion']].merge(docs_df[['docno','text']], on='docno'))
print('Predicted emotion: ',predicted_emotion)
print('Similar messages: ')
print(''.rjust(10, ' ')+' Emotion'.ljust(12, ' ') + '\tText')
# print(messages)
for msg in messages.itertuples():
print((str(msg.Index+1) + ' ').rjust(10, ' ')+' ' + str(msg.emotion).ljust(12, ' ') + '\t' + msg.text)
elif inp == '2':
print('Please ensure that each line in the file contains a single query.')
inp = input("Enter relative path to file -> ")
if not os.path.exists(inp):
print('File not found! Try something else.')
continue
# test_input/test1.txt
with open(inp, 'r') as file:
lines = file.readlines()
lines = [re.sub(r'[0-9]', '', a).translate(str.maketrans('', '', string.punctuation)).replace('\n','') for a in lines]
results = [predict_single_query(a, clf, vectorizer)[0] for a in lines]
messages = pd.DataFrame(lines,columns=['text'])
messages['emotion'] = results
for msg in messages.itertuples():
print((str(msg.Index+1) + ' ').rjust(10, ' ')+' ' + str(msg.emotion).ljust(12, ' ') + '\t' + msg.text)
res = pd.DataFrame(100 * messages[['emotion','text']].groupby('emotion').count() / messages.shape[0]).reset_index()
res.columns = ['emotion','percent']
print(res)
sns.set(rc={"figure.figsize":(5,5)})
sns.histplot(messages, x='emotion' , stat="percent")
print('See popup for graph')
plt.show()
# for line in lines:
# print(line)
elif inp == '3':
inp = input("Enter query -> ")
inp = re.sub(r'[0-9]', '', inp).translate(str.maketrans('', '', string.punctuation))
num_messages = int(input("Enter number of messages to retrieve (minimum is 20) -> "))
num_messages = max(20, num_messages)
# predicted_emotion = predict_single_query(inp, clf, vectorizer)[0]
messages = ca_pipe.search(inp)
if messages.empty:
# PyTerrier bug: https://github.com/terrier-org/pyterrier/issues/352
print('Sorry! No results found. Try something else.')
continue
messages = pd.DataFrame(messages.head(num_messages)[['docno','emotion']].merge(docs_df[['docno','text']], on='docno'))
res = pd.DataFrame(100 * messages[['emotion','text']].groupby('emotion').count() / messages.shape[0]).reset_index()
res.columns = ['emotion','percent']
print(res)
sns.set(rc={"figure.figsize":(5,5)})
sns.histplot(messages, x='emotion' , stat="percent")
print('See popup for graph')
plt.show()
elif inp.lower() == 'exit':
print(inp)
break
else:
print('Invalid input. Try again')
continue
if __name__ == "__main__":
main()