Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. import nltk
  2. import sys
  3.  
  4. class Analyzer():
  5. """Implements sentiment analysis."""
  6.  
  7.  
  8.  
  9. def __init__(self, positives, negatives):
  10. """Initialize Analyzer."""
  11.  
  12. self.Pwords = list()
  13. self.Nwords= list()
  14. self.P = open(positives, "r")
  15. self.N = open(negatives, "r")
  16. self.load (self.P)
  17. self.load (self.N)
  18.  
  19.  
  20.  
  21.  
  22.  
  23. def analyze(self, text):
  24. """Analyze text for sentiment, returning its score."""
  25.  
  26. analysis = set()
  27. from nltk.tokenize import TweetTokenizer
  28. TokenizerFunction = TweetTokenizer(strip_handles=True, reduce_len=True)
  29. for a in range (len(text)):
  30. num = 0
  31. words = TokenizerFunction.tokenize (text[a])
  32. for i in range (len(words)):
  33. temp = Analyzer.check(self, words[i])
  34. num = num + temp
  35. analysis.add(num)
  36. return analysis
  37.  
  38.  
  39. def load(self, dictionary):
  40. for line in dictionary:
  41. if dictionary == self.P:
  42. self.Pwords.append(line.rstrip("\n"))
  43. if dictionary == self.N:
  44. self.Nwords.append(line.rstrip("\n"))
  45.  
  46. dictionary.close()
  47.  
  48.  
  49. def check (self, checker):
  50. if checker in self.Pwords:
  51. return 1
  52. if checker in self.Nwords:
  53. return -1
  54. else:
  55. return 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement