-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsentimentEngine.py
More file actions
89 lines (67 loc) · 2.29 KB
/
Copy pathsentimentEngine.py
File metadata and controls
89 lines (67 loc) · 2.29 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
import sys
import os
import time
import nltk
import random
import pickle
import itertools
import operator
from sklearn.feature_extraction.text import TfidfVectorizer
# documents=[]
# with open("documents.pickle","rb") as fid:
# documents=pickle.load(fid)
test_sen_all = ["I am in rvce where are you going"]
open_file = open("vectorizer.pickle", "rb")
vectorizer = pickle.load(open_file)
def most_common(L):
# get an iterable of (item, iterable) pairs
SL = sorted((x, i) for i, x in enumerate(L))
# print 'SL:', SL
groups = itertools.groupby(SL, key=operator.itemgetter(0))
# auxiliary function to get "quality" for an item
def _auxfun(g):
item, iterable = g
count = 0
min_index = len(L)
for _, where in iterable:
count += 1
min_index = min(min_index, where)
# print 'item %r, count %r, minind %r' % (item, count, min_index)
return count, -min_index
# pick the highest-count/earliest item
return max(groups, key=_auxfun)[0]
def test_vectorizer(test_docs):
test_corpus = [d for d in test_docs]
X = vectorizer.transform(test_corpus)
return X
def sentiment(message):
open_file = open("svm_linearSVM_pos_neg.pickle", "rb")
svm_lk_pos_neg = pickle.load(open_file)
open_file.close()
open_file = open("svm_linearSVM_pos_neut.pickle", "rb")
svm_lk_pos_neut = pickle.load(open_file)
open_file.close()
open_file = open("svm_linearSVM_neg_neut.pickle", "rb")
svm_lk_neg_neut = pickle.load(open_file)
open_file.close()
X_test = test_vectorizer(message)
pred1 = svm_lk_pos_neg.predict(X_test)
pred2 = svm_lk_pos_neut.predict(X_test)
pred3 = svm_lk_neg_neut.predict(X_test)
res=[]
res_predict=[]
for p1,p2,p3 in zip(pred1,pred2,pred3):
if p1==p2:
res.append(p1)
res_predict.append(p1)
elif p1==p3:
res.append(p1)
res_predict.append(p1)
elif p2==p3:
res.append(p2)
res_predict.append(p2)
else:
temp=[p1,p2,p3]
res.append(random.choice(temp))
res_predict.append("meh")
return (most_common(res_predict))