Here's how my code looks like:
import nltk
class Analyzer():
def __init__(self, positives, negatives):
self.positives = set()
self.negatives = set()
file = open(positives, "r")
for line in file:
self.positives.add(line.strip("\n"))
if line.startswith(";"):
self.positives.remove(line)
file.close()
file1 = open(negatives, "r")
for line in file1:
self.negatives.add(line.strip("\n"))
if line.startswith(";"):
self.negatives.remove(line)
file1.close()
def analyze(self, text):
with open("text") as texts:
for lines in texts:
# Get a list of words from the lines in text.
tokens = [self.tokenizer.tokenize(lines)]
# All the words in postive-words and negative-words are lowercased.
if tokens.lower() in self.positives:
return 1
elif tokens.lower() in self.negatives:
return -1
else:
return 0
Unfortunately, this seem to not work and no matter how I shift around the lines of codes, I kept getting:
Traceback (most recent call last):
File "./smile", line 32, in <module>
main()
File "./smile", line 20, in main
analyzer = Analyzer(positives, negatives)
File "/home/ubuntu/workspace/pset6/sentiments/analyzer.py", line 13, in __init__
self.positives.remove(line)
KeyError: ';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\n'
May I get a hint of what am I doing wrong? Would really appreciate some hints! Thank you!