taffners

tweet_sentiment

May 28th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.38 KB | None | 0 0
  1. import sys
  2. import urllib
  3. import json
  4.  
  5.  
  6. def main():
  7.     sent_file = open(sys.argv[1])
  8.     tweet_file = open(sys.argv[2])
  9.  
  10.     #Make a dictionary of sentiment scores from AFINN-111.txt
  11.     #keys are words and values are score from -5(negative sentiment) to 5 (positive)
  12.  
  13.     scores ={}
  14.  
  15.     sent_line = sent_file.readline()
  16.     while sent_line != "":
  17.         term, score = sent_line.strip().split('\t')
  18.         term = term.decode('utf-8')
  19.         scores[term] = int(score)
  20.        
  21.         sent_line = sent_file.readline()
  22.    
  23.     #parse the input_file
  24.     for tweet_line in tweet_file:
  25.  
  26.         tweet_dict = json.loads(tweet_line)
  27.        
  28.     #get the tweet
  29.         if 'text' in tweet_dict.keys():
  30.             results = tweet_dict['text']
  31.            
  32.             exclude = '!@#$%^&*()_-+=><,.:;"?/\}]{['
  33.             no_punctuation_results = ''.join(ch for ch in results if ch not in exclude)
  34.             split_results = no_punctuation_results.strip().split()    
  35.            
  36.             sent_of_tweet = 0.0
  37.     #for each word in tweet get find sentiment number
  38.             for u_word in split_results:
  39.                
  40.                 if u_word in scores.keys():
  41.                     sent_of_tweet += scores[u_word]
  42.                    
  43.             print sent_of_tweet
  44.    
  45.     sent_file.close()
  46.     tweet_file.close()
  47.    
  48. if __name__ == '__main__':
  49.     main()
Advertisement
Add Comment
Please, Sign In to add comment