Search code examples
stanford-nlpsentiment-analysispycorenlp

Why am I getting String where should get a dict when using pycorenlp.StanfordCoreNLP.annotate?


I'm running this example using pycorenlp Stanford Core NLP python wrapper, but the annotate function returns a string instead of a dict, so, when I iterate over it to get each sentence sentiment value I get the following error: "string indices must be integers".

What could I do to get over it? Anyone could help me? Thanks in advance. The code is below:

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"])

Solution

  • You should just use the official stanfordnlp package! (note: the name is going to be changed to stanza at some point)

    Here are all the details, and you can get various output formats from the server including 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)