-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtweet_sentiment.py
28 lines (24 loc) · 1.03 KB
/
tweet_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
import sys
import json
scores = {} # initialize an empty dictionary so it's available globally
def dictionary():
afinnfile = open(sys.argv[1])
for line in afinnfile:
term, score = line.split("\t") # The file is tab-delimited. "\t" means "tab character"
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
print tweet['text']
print 'keyword:', word
print 'score', scores[word] # if it does, print it out alomg with it's score value
print
def main():
dictionary()
getTextFromTweet()
if __name__ == '__main__':
main()