Find file
Fetching contributors…
Cannot retrieve contributors at this time
48 lines (34 sloc) 1.08 KB
import sys, json
def read_sentiment_scores(sent_file):
sent_file = open(sent_file)
scores = {} # initialize an empty dictionary
for line in sent_file:
term, score = line.split("\t") # The file is tab-delimited. "\t" means "tab character"
scores[term] = int(score) # Convert the score to an integer.
return scores # return every (term, score) pair in the dictionary
def read_tweets_file(tweet_file):
tweet_file = open(tweet_file)
# fetching tweet json to list
tweets = []
for line in tweet_file:
tweet = json.loads(line)
return tweets
def compute_tweet_sentiment_score(scores, tweets):
# extract tweet content
for i in range(1, len(tweets)):
tweet_score = 0
words = tweets[i]['text'].split(" ")
# calculate sentiment score
for w in words:
if w in scores:
tweet_score += scores[w]
print tweet_score
def main():
sent_file = sys.argv[1]
tweet_file = sys.argv[2]
scores = read_sentiment_scores(sent_file)
tweets = read_tweets_file(tweet_file)
compute_tweet_sentiment_score(scores, tweets)
if __name__ == '__main__':