-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsentiment_analyser.py
More file actions
61 lines (49 loc) · 1.61 KB
/
sentiment_analyser.py
File metadata and controls
61 lines (49 loc) · 1.61 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
import nltk
import random
import pickle
from nltk.classify import ClassifierI
from nltk.tokenize import word_tokenize
from statistics import mode
from nltk.collocations import BigramCollocationFinder
from nltk.metrics import BigramAssocMeasures
class VoteClassifier(ClassifierI):
def __init__(self, *classifiers):
self._classifiers = classifiers
def classify(self, features):
votes = []
for c in self._classifiers:
v = c.classify(features)
votes.append(v)
return mode(votes)
def confidence(self, features):
votes = []
for c in self._classifiers:
v = c.classify(features)
votes.append(v)
choice_votes = votes.count(mode(votes))
conf = choice_votes / len(votes)
return conf
l_d = open("pickled_algos/documents.pickle", "rb")
documents = pickle.load(l_d)
l_d.close()
w_features = open("pickled_algos/word_features.pickle", "rb")
word_features = pickle.load(w_features)
w_features.close()
def find(documents):
temp = {}
word = set(documents)
n = 2
score_fn=BigramAssocMeasures.chi_sq
bigram_finder = BigramCollocationFinder.from_words(word)
bigrams = bigram_finder.nbest(score_fn, n)
for w in word_features:
temp[w] = (w in word )
for w in bigrams:
temp[w] = True
return temp
classify_buffer = open("pickled_algos/classifier.pickle", "rb")
classifier = pickle.load(classify_buffer)
classify_buffer.close()
def sentiment(text):
feat = find(text)
return VoteClassifier(classifier).classify(feat),VoteClassifier(classifier).confidence(feat)