我正在尝试创建一个程序,告诉我paulryan.txt文件的每一行的正负程度。我使用的是opinion_lexicon,文件是'_io.TextIOWrapper‘
有没有什么我可以用来代替.words的?
其他不太重要的问题:有没有办法让我的整个paulryan.txt文件变得小写,同时保持它的行标记化?如果我不把整个东西变成小写,它就不会给我一个准确的正或负分数,因为在opinion_lexicon中只有小写的单词。
import nltk
from nltk.corpus import opinion_lexicon
from nltk.tokenize.simple import (LineTokenizer, line_tokenize)
poswords = set(opinion_lexicon.words("positive-words.txt"))
negwords = set(opinion_lexicon.words("negative-words.txt"))
f=open("paulryan.txt", "rU")
raw = f.read()
token= nltk.line_tokenize(raw)
print(token)
def finddemons():
for x in token:
y = token.words()
percpos = len([w for w in token if w in poswords ]) / len(y)
percneg = len([w for w in token if w in negwords ]) / len(y)
print(x, "pos:", round(percpos, 3), "neg:", round(percneg, 3))
finddemons()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in finddemons
AttributeError: 'list' object has no attribute 'words'https://stackoverflow.com/questions/47585906
复制相似问题