Advertisement
callyb

analyzer

Mar 5th, 2017
107
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import nltk
  2. import sys
  3. write = sys.stdout.write
  4.  
  5. class Analyzer():
  6. """Implements sentiment analysis."""
  7.  
  8.  
  9. def __init__(self, positives, negatives):
  10. """Initialize Analyzer."""
  11. self.positives = []
  12. self.negatives = []
  13.  
  14. with open(positives) as infile:
  15. for line in infile:
  16. if not line.startswith((';', ' ')):
  17. self.positives.append(line)
  18.  
  19. with open(negatives) as infile:
  20. for line in infile:
  21. if not line.startswith((';', ' ')):
  22. self.negatives.append(line)
  23.  
  24.  
  25.  
  26.  
  27.  
  28. def analyze(self, text):
  29. """Analyze text for sentiment, returning its score."""
  30. #scores: positive = 1, negative = -1, neutral = 0
  31. score = 0
  32.  
  33. #instantiate tokenizer
  34. tokenizer = nltk.tokenize.TweetTokenizer
  35. tokens = tokenizer.tokenize(text)
  36.  
  37. #iterate over tokenized tweets
  38. for token in tokens:
  39. token = token.lower()
  40.  
  41. #check if word is pos or neg (if text is in self.positives or self.negatives )
  42. if token in self.positives:
  43. score += 1
  44.  
  45. elif token in self.negatives:
  46. score -= 1
  47.  
  48. return score
Advertisement
RAW Paste Data Copied
Advertisement