[我正在使用pycorenlp Stanford Core NLP python包装器运行此example,但是注释函数返回的是字符串而不是字典,因此,当我对其进行迭代以获取每个句子的情感值时,出现以下错误:“字符串索引必须是整数”。
我该怎么做才能克服它?有人可以帮助我吗?提前致谢。代码如下:
from pycorenlp import StanfordCoreNLP
nlp_wrapper = StanfordCoreNLP('http://localhost:9000')
doc = "I like this chocolate. This chocolate is not good. The chocolate is delicious. Its a very
tasty chocolate. This is so bad"
annot_doc = nlp_wrapper.annotate(doc,
properties={
'annotators': 'sentiment',
'outputFormat': 'json',
'timeout': 100000,
})
for sentence in annot_doc["sentences"]:
print(" ".join([word["word"] for word in sentence["tokens"]]) + " => "\
+ str(sentence["sentimentValue"]) + " = "+ sentence["sentiment"])
您应该只使用官方的stanfordnlp软件包! (注意:名称将在某些时候更改为节)
这里有所有详细信息,您可以从服务器获取包括JSON在内的各种输出格式。
https://stanfordnlp.github.io/stanfordnlp/corenlp_client.html
from stanfordnlp.server import CoreNLPClient
with CoreNLPClient(annotators=['tokenize','ssplit','pos','lemma','ner', 'parse', 'depparse','coref'], timeout=30000, memory='16G') as client:
# submit the request to the server
ann = client.annotate(text)