我正在尝试逐行分析文本文件中每个给定句子的情绪。每当我使用链接的第一个问题中的硬编码句子时,该代码就可以工作。当我使用文本文件输入时,我得到TypeError
.
第一个有效,第二个与文本文件("I love you. I hate him. You are nice. He is dumb")
无效。这是代码:
from pycorenlp import StanfordCoreNLP
nlp = StanfordCoreNLP('http://localhost:9000')
results = []
with open("c:/nlp/test.txt","r") as f:
for line in f.read().split('\n'):
print("Line:" + line)
res = nlp.annotate(line,
properties={
'annotators': 'sentiment',
'outputFormat': 'json',
'timeout': 1000,
})
results.append(res)
for res in results:
s = res["sentences"]
print("%d: '%s': %s %s" % (
s["index"],
" ".join([t["word"] for t in s["tokens"]]),
s["sentimentValue"], s["sentiment"]))
我收到此错误:
第 21 行,在
s["索引"],
TypeError:列表索引必须是整数或切片,而不是 str