-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentiment.py
48 lines (38 loc) · 1.3 KB
/
sentiment.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
#!/usr/bin/python
#
# Simple python script that loads a classifier from file, then uses that to generate
# the sentiment of each line of input. Designed for use with EMR as a mapper, but can be
# used on the command line as well.
#
# Revised Nov 2013: For use with the AWS Getting Started with Big Data guide
#
import cPickle as pickle
import nltk.classify.util
from nltk.classify import NaiveBayesClassifier
from nltk.tokenize import word_tokenize
import sys
sys.stderr.write("started mapper\n");
def word_feats(words):
return dict([(word, True) for word in words])
def subj(subjLine):
subjgen = subjLine.lower()
# Replace term1 with your subject term
subj1 = "term1"
if subjgen.find(subj1) != -1:
subject = subj1
return subject
else:
subject = "No match"
return subject
def main(argv):
classifier = pickle.load(open("classifier.p", "rb"))
for line in sys.stdin:
tolk_posset = word_tokenize(line.rstrip())
d = word_feats(tolk_posset)
subjectFull = subj(line)
if subjectFull == "No match":
print "LongValueSum:" + " " + subjectFull + ": " + "\t" + "1"
else:
print "LongValueSum:" + " " + subjectFull + ": " + classifier.classify(d) + "\t" + "1"
if __name__ == "__main__":
main(sys.argv)