-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsentiment.py
executable file
·64 lines (53 loc) · 1.94 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/python
#
# (originally entered at https://gist.github.com/1035399)
#
# License: GPLv3
#
# To download the AFINN word list do:
# wget http://www2.imm.dtu.dk/pubdb/views/edoc_download.php/6010/zip/imm6010.zip
# unzip imm6010.zip
#
# Note that for pedagogic reasons there is a UNICODE/UTF-8 error in the code.
import math
import re
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
# AFINN-111 is as of June 2011 the most recent version of AFINN
filenameAFINN = 'AFINN/AFINN-111.txt'
afinn = dict(map(lambda (w, s): (w, int(s)), [
ws.strip().split('\t') for ws in open(filenameAFINN) ]))
# Word splitter pattern
pattern_split = re.compile(r"\W+")
def sentiment(text):
"""
Returns a float for sentiment strength based on the input text.
Positive values are positive valence, negative value are negative valence.
"""
words = pattern_split.split(text.lower())
sentiments = map(lambda word: afinn.get(word, 0), words)
if sentiments:
# How should you weight the individual word sentiments?
# You could do N, sqrt(N) or 1 for example. Here I use sqrt(N)
sentiment = float(sum(sentiments))/math.sqrt(len(sentiments))
else:
sentiment = 0
return sentiment
if __name__ == '__main__':
# Single sentence example:
text = sys.argv[1]
#"Finn is stupid and idiotic"
print("%6.2f %s" % (sentiment(text), text))
# No negation and booster words handled in this approach
#text = "Finn is only a tiny bit stupid and not idiotic"
#print("%6.2f %s" % (sentiment(text), text))
'''
# Example with downloading from Twitter:
import simplejson
import urllib
query = "pfizer"
json = simplejson.load(urllib.urlopen("http://search.twitter.com/search.json?q=" + query))
sentiments = map(sentiment, [ tweet['text'] for tweet in json['results'] ])
print("%6.2f %s" % (sum(sentiments)/math.sqrt(len(sentiments)), query))
'''