-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterm_sentiment.py
31 lines (26 loc) · 1.3 KB
/
term_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
import sys
import json
scores = {} # initialize an empty dictionary so it's available globally
def dictionary():
afinnfile = open(sys.argv[1]) # load up AFN-111 dictionary
for line in afinnfile:
term, score = line.split("\t") # The file is tab-delimited
scores[term] = int(score) # Convert the score to an integer
def getTextFromTweet():
tweet_file = open(sys.argv[2]).readlines() # get the file of tweets
tweets = json.loads(tweet_file[22]) # the first 22 lines of a twitter response are junk
for tweet in tweets['statuses']: # loop through the json looking for tweets
tweet_words = tweet['text'].split() # find the text element and split it into words
for word in tweet_words:
if word in scores: # see if a word exists in the scores dict
try: # sometimes there won't be a next word...
nextWord = tweet_words[(tweet_words.index(word) + 1)] # grab the next word
if not nextWord in scores: # only include next words that are not in AFN-111
print nextWord, scores[word] # print out the next word alomg with the score from the original word
except:
pass
def main():
dictionary()
getTextFromTweet()
if __name__ == '__main__':
main()