Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import urllib
- import json
- def main():
- sent_file = open(sys.argv[1])
- tweet_file = open(sys.argv[2])
- #Make a dictionary of sentiment scores from AFINN-111.txt
- #keys are words and values are score from -5(negative sentiment) to 5 (positive)
- scores ={}
- sent_line = sent_file.readline()
- while sent_line != "":
- term, score = sent_line.strip().split('\t')
- term = term.decode('utf-8')
- scores[term] = int(score)
- sent_line = sent_file.readline()
- #parse the input_file
- for tweet_line in tweet_file:
- tweet_dict = json.loads(tweet_line)
- #get the tweet
- if 'text' in tweet_dict.keys():
- results = tweet_dict['text']
- exclude = '!@#$%^&*()_-+=><,.:;"?/\}]{['
- no_punctuation_results = ''.join(ch for ch in results if ch not in exclude)
- split_results = no_punctuation_results.strip().split()
- sent_of_tweet = 0.0
- #for each word in tweet get find sentiment number
- for u_word in split_results:
- if u_word in scores.keys():
- sent_of_tweet += scores[u_word]
- print sent_of_tweet
- sent_file.close()
- tweet_file.close()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment