google-cloud-nlmagnitude

Document Sentiment Magnitude != sum(Sentence Magnitude)


I am currently utilizing the google cloud NL api for some tests where I analyze news articles. I was initially curious about how document magnitude was calculated, and searches here yielded

Google Cloud Natural Language API - How is document magnitude calculated?

where it was mentioned to be the sum of constituent sentence magnitudes.

In my own tests, I have found that this was not the case. Is there any thing I might be doing wrong?


For clarity, I am using the running Python 3.7.3 in a conda environment with google-cloud-language obtained from conda-forge.

document =types.Document(content = str, type = enums.Document.Type.PLAIN_TEXT)
sentiment = client.analyze_sentiment(document=document)

sentence_sents = sentiment.sentences
test_mag = 0
for sent_obj in sentence_sents:
     test_mag += sent_obj.sentiment.magnitude

print(sentiment.document_sentiment.magnitude)
print(test_mag)

Solution

  • From another thread it can be sometimes just the absolute sum but not always.

    Google Natural Language Sentiment Analysis Aggregate Scores

    "The way the aggregation works is breaking down the input text into smaller components, often ngrams, which is likely why the documentation talks about aggregation, however, the aggregation isn't a simple addition, one can't sum individual sentiment values of each entity to get a total score."

    I assume this is the case for score and magnitude calculations.