-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassifier.py
47 lines (34 loc) · 1.08 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
import json
from textblob import TextBlob
from textblob.classifiers import NaiveBayesClassifier
class Classifier:
def __init__(self):
fp = open("./data/train.csv")
self.cl = NaiveBayesClassifier(fp, format="csv")
fp.close()
def test(self):
return self.cl.classify("This is a test sentence")
def classify(self, text):
return self.cl.classify(text)
def n_classify(self, text):
dist = self.cl.prob_classify(text)
probs = {"sentiments": []}
for s in dist.samples():
if dist.prob(s) >= .10:
probs["sentiments"].append({s: dist.prob(s)})
return json.dumps(probs)
def accuracy(self):
fp = open('./data/train.csv')
train_accuracy = self.cl.accuracy(fp, format="csv")
fp.close()
fp = open('./data/test.csv')
test_accuracy = self.cl.accuracy(fp, format="csv")
fp.close()
return json.dumps({"train_accuracy": train_accuracy, "test_accuracy": test_accuracy})
def labels(self):
return json.dumps({"labels": self.cl.labels()})
def main():
cl = Classifier()
print(cl.test())
if __name__ == "__main__":
main()