-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
executable file
·68 lines (53 loc) · 2.5 KB
/
example.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
65
66
67
68
"""
An example Python client for the Chatterbox Analytics Sentiment Detection API
delivered through the Mashape platform. The API is designed specifically
for short social texts.
This client has some hard coded (but real) tweets which are passed to the
API.
You need to add your keys found on the Mashape Dashboard.
"""
from SentimentAnalysisFree import SentimentAnalysisFree
#add your keys below
public_key = ""
private_key = ""
#build the Free API object with your keys
chatterboxapi = SentimentAnalysisFree(public_key, private_key)
#Our hard coded sample tweets. Your application will figure out
#what it needs...
sampletexts = ["@getflockler must have a great designer cause it looks hot!! :-)",
"I've started exploring Evernote... this may change my life.. I'm not sure yet, but it may.",
"Well, Saudi Airlines have really messed up our holiday. Pretty pissed off they can get away with it.",
"How did someone make it to our site using the keywords: \"different types of birth control\"? #odd",
"Apple Mail, why do you crash so? #grrrr",
"If you ever wonder 'how bad can a coffee from burger king be, really?' the answer is bad, very bad. #badcoffeebadmorning",
"Great meeting with TK from the Queen Mary student paper - really interesting questions!"]
highestnumber = 0
highesttext = False
try:
for sampletext in sampletexts:
print "Text: " + sampletext
#Here we do the actual classification. We pass in a language
#identifier and the text we wish to be classified.
classification = chatterboxapi.classifytext("en",sampletext)
#Uncomment this line if you want to inspect the result.
#print classification.body
#Value is the predicted strength of the sentiment in the text
sentiment_value = classification.body['value']
if sentiment_value > highestnumber:
highestnumber = sentiment_value
highesttext = sampletext
#Sent is the sentiment class. 1 is positive, -1 is negative
sentiment_label = classification.body['sent']
if abs(sentiment_value) < 0.25:
#As a consumer of this API you will need to experiment with
#which cut-off value works best for your application.
print "WEAK: Hmm. Weak or netural sentiment it seems"
elif sentiment_label > 0:
print "POS: W00t! Seems like a positive message"
else:
print "NEG: Oh foo. Seems like a negative one"
print "The most positive message is", highesttext
except KeyError:
#Errors should be handled more elegantly than this.
print "error code: ", classification['code']
print "error message: ", classification['message']