-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathrun_sentiwordnet.py
34 lines (26 loc) · 1022 Bytes
/
run_sentiwordnet.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
import sys
from load_samples import load_samples
from load_samples import whole_text
from sentiwordnet import sentiwordnet_classify
def main(argv):
if len(argv) != 2:
print 'usage: python sentiwordnet <category> <quantity>'
sys.exit(2)
category = argv[0]
quantity = int(argv[1])
print "Category: '%s'" % category
print "SentiWordNet classification on %s samples\n" % (quantity * 2)
try:
(pos_reviews, neg_reviews) = load_samples(category, quantity, whole_text)
except Exception:
print("The data for this category and quantity are not found.")
sys.exit(2)
reviews = pos_reviews + neg_reviews
truth = [review[1] for review in reviews]
predictions = [sentiwordnet_classify(review[0]) for review in reviews]
accuracy = sum([1 if predictions[i] == truth[i] else 0
for i in range(len(truth))]) / float(len(truth))
print "Accuracy: %s\n" % accuracy
print "\n"
if __name__ == "__main__":
main(sys.argv[1:])