So last night, Benji and I really stepped the Luongo app game up to another level by adding sentiment analysis in deciding what people were thinking. Before, our main algorithm was really elementary and only detected people who tweeted the words “luongo” and “fault” and then counted all the tweets that had the word “not”. We would then compare the number of tweets that had the word “not” (indicating that people didn’t think it was his fault) with the number of words without the word “not” (indicating that people did think it was his fault).
Here’s a snippet of the code so you see exactly what it’s doing:
results = tweepy.Cursor(api.search,
q="luongo & fault",
for r in results:
#print r.text, r.id
# r now contains one tweepy result object
if r.text.find(u'not') > -1:
n += 1
nn += 1
count += 1
if n > nn:
print "That was luongo's fault"
print "That was not luongo's fault"
Now, what we’ve done is we actually take the data that we get from the Twitter API and send it to another API called Sentiment 140 to do sentiment analysis on all of the tweets featuring the word Luongo. Sentiment 140 will analyse each individual tweet that says if it’s a positive, negative or neutral tweet and then we count up that value like before. The reason that this is better is because Sentiment 140 will actually look all words within the tweet which contain positive or negative values instead of just looking for only the word “fault” as a subject and “not” as the counter indicator. For example, the following is a snippet from the JSON response from Sentiment 140. It’s ranked as a negative tweet (negative tweet has polarity = 0):
Sorry we broke your goalie, mister. #Luongo #Canucks Go #Bruins”,”polarity”:0
Here’s a tweet that’s ranked as positive (postive tweets has polarity = 4:
@loveforthree Pretty good for a team that needs Luongo…”,”polarity”:4
So asides from showing that Bruins fans are terrible, Sentiment 140 does an excellent job at figuring out whether a tweet is positive or negative, especially compared to our previous algorithm. There’s a couple of challenges that we’re facing, which is whether we’re only reporting on tweets with the word “fault” in them, or just assume that any tweets that are positive and negative during a game will generally have some bearing on the number of goals scored.
Our next step is going to be importing this script to a server so you can all start accessing it during games! It’s gonna be cool.