Advertisement
DragonOsman

Sentiments application.py

Mar 5th, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.39 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import os
  4. import sys
  5. import nltk
  6.  
  7. from analyzer import Analyzer
  8. from termcolor import colored
  9. from helpers import get_user_timeline
  10.  
  11. def main():
  12.     if len(sys.argv[1]) != 2:
  13.         sys.exit("Usage: ./tweets twitterhandle")
  14.        
  15.     positives = os.path.join(sys.path[0], "positive-words.txt")
  16.     negatives = os.path.join(sys.path[0], "negative-words.txt")
  17.    
  18.     analyzer = Analyzer(positives, negatives)
  19.    
  20.     final_score = analyzer.analyze(sys.argv[1])
  21.    
  22.     for i in range(0, len(final_score)):
  23.         score = final_score[i]
  24.         print(score[0], end="")
  25.         if score[0] > 0.0:
  26.             print(colored(score, "green"))
  27.         elif score[0] < 0.0:
  28.             print(colored(score, "red"))
  29.         else:
  30.             print(colored(score, "yellow"))
  31.            
  32. if __name__ == "__main__":
  33.     main()
  34.    
  35.  
  36. def tweet_analyze(self, text):
  37.     tweets = get_user_timeline(text, 50)
  38.    
  39.     if tweets == None:
  40.         sys.exit("Error: problem getting tweets")
  41.        
  42.     print(tweets)
  43.     final_score = []
  44.     for tweet in tweets:
  45.         score = 0
  46.         tokenizer = nltk.tokenize.TweetTokenizer()
  47.         tokens = tokenizer.tokenize(tweet)
  48.         if tweet in self.positives:
  49.             score += 1
  50.         elif tweet in self.negatives:
  51.             score -= 1
  52.         final_score[tweet] = score
  53.        
  54.     return final_score
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement