Skip to main content
 首页 » 编程设计

python之py-corenlp-TypeError : string indices must be integers

2025年02月15日10bhlsheji

我遵循了这个答案: Stanford nlp for python

from pycorenlp import StanfordCoreNLP 
from newspaper import Article 
 
url = u'newsArticle.example.html' 
nlp = StanfordCoreNLP('http://localhost:9000') 
article = Article(url) 
article.download() 
article.parse() 
 
 
LARGE_TEXT=article.text 
 
 
res = nlp.annotate(LARGE_TEXT, 
               properties={ 
                   'annotators': 'sentiment', 
                   'outputFormat': 'json', 
                   'timeout': 1000, 
               }) 
for s in res["sentences"]: 
    print ("%d: '%s': %s %s" % ( 
        s["index"], 
        " ".join([t["word"] for t in s["tokens"]]), 
        s["sentimentValue"], s["sentiment"])) 

我使用了较长的文本作为输入并遇到了以下错误:

 for s in res["sentences"]: 
TypeError: string indices must be integers 

请您参考如下方法:

问题是'timeout': 1000

我改成了'timeout': 10000