I've been on this for a while and can't figure out the mistakes. Please correct me if i am mistaken. On running the code, the returned value is always 0. (2) When I use the 'startwith' function i'm getting an error AttributeError: 'str' object has no attribute 'startwith'
import nltk
class Analyzer():
"""Implements sentiment analysis."""
"""Initialize Analyzer."""
def __init__(self, positives, negatives):
self.PositiveDict = open(positives, "r")
self.NegativeDict = open(negatives, "r")
#Make two lists/set/dicts.
self.Pos = list()
self.Neg = list()
self.load(self.PositiveDict)
self.load(self.NegativeDict)
def load(self, WordFile):
for Word in WordFile:
if Word.startswith(';'):
continue
else:
#print(Word)
if WordFile == self.PositiveDict:
self.Pos.append(Word.rstrip("\n"))
elif WordFile == self.NegativeDict:
self.Neg.append(Word.rstrip("\n"))
"""Analyze text for sentiment, returning its score."""
def analyze(self, text):
sum=0
tokenizer = nltk.tokenize.TweetTokenizer()
tokens = tokenizer.tokenize(text)
#print(tokens)
x = len(self.Pos)
y = len(self.Neg)
print(y)
print(x)
if(True):
for A in range(x):
if tokens==self.Pos[A]:
sum=1
for B in range(y):
if tokens==self.Neg[B]:
sum=-1.0
else:
sum=0.0
print(sum)
return sum