这是我的代码的样子:
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
不幸的是,这似乎不起作用,无论我如何改变代码行,我一直得到:
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'
我可以提示我做错了什么吗?真的很感激一些提示!谢谢!