-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclassifier.py
60 lines (49 loc) · 1.5 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
from flask import jsonify, abort, g, flash
from textblob.classifiers import NaiveBayesClassifier
train = [
('Slow Moving traffic', 'neg'),
('On going protest', 'neg'),
('Vehicles not allowed', 'neg'),
('Slow traffic', 'neg'),
('bus breakdown', 'neg'),
('expect traffic holdup', 'neg'),
('more rush', 'neg'),
('Traffic is restored', 'pos'),
('safe', 'pos'),
('normal', 'pos'),
('traffic cleared', 'pos'),
('Peak hour traffic', 'neu'),
(' Routes to avoid', 'neu'),
('Traffic at', 'neu')
]
cl = NaiveBayesClassifier(train)
def classify(tweet):
return cl.classify(tweet)
def getTweetsWithStatus(final):
finalWithStatus = []
neg = neu = pos = total = 0
for tweet in final:
inst = {}
inst['tweet'] = tweet[0]
inst['time'] = tweet[1]
inst['date'] = tweet[2]
if classify(tweet[0]) == "neg":
inst['status'] = "Negative"
neg = neg + 1
elif classify(tweet[0]) == "pos":
inst['status'] = "Positive"
pos = pos + 1
elif classify(tweet[0]) == "neu":
inst['status'] = "Caution"
neu = neu + 1
finalWithStatus.append(inst)
new_list = []
temp = []
inst = {}
total = neu + neg + pos
inst['positive'] = ((float(pos) / total) * 100)
inst['neutral'] = ((float(neu) / total) * 100)
inst['negative'] = ((float(neg) / total) * 100)
temp.append(inst)
new_list = temp + finalWithStatus
return new_list