Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import nltk
- import sys
- class Analyzer():
- """Implements sentiment analysis."""
- def __init__(self, positives, negatives):
- """Initialize Analyzer."""
- self.Pwords = list()
- self.Nwords= list()
- self.P = open(positives, "r")
- self.N = open(negatives, "r")
- self.load (self.P)
- self.load (self.N)
- def analyze(self, text):
- """Analyze text for sentiment, returning its score."""
- analysis = set()
- from nltk.tokenize import TweetTokenizer
- TokenizerFunction = TweetTokenizer(strip_handles=True, reduce_len=True)
- for a in range (len(text)):
- num = 0
- words = TokenizerFunction.tokenize (text[a])
- for i in range (len(words)):
- temp = Analyzer.check(self, words[i])
- num = num + temp
- analysis.add(num)
- return analysis
- def load(self, dictionary):
- for line in dictionary:
- if dictionary == self.P:
- self.Pwords.append(line.rstrip("\n"))
- if dictionary == self.N:
- self.Nwords.append(line.rstrip("\n"))
- dictionary.close()
- def check (self, checker):
- if checker in self.Pwords:
- return 1
- if checker in self.Nwords:
- return -1
- else:
- return 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement