1

I am trying to use the Google NLP API in my Python code and found that when I call using analyze_sentiment or analyze_entity_sentiment, I sometimes get null in "score", which is undocumented behavior. Hence, I tried to use "gcloud ml language analyze-sentiment" and was shocked to find that they return different results! Any hints would be greatly appreciated!

from google.cloud import language
// I setup my credentials here
lsclient = language.LanguageServiceClient(credentials=credentials)

document = types.Document(content="@stevenmnuchin1 @Apple @tim_cook How much did this trip cost us? I\'m sure you took your wife and did some sightseeing.", type=enums.Document.Type.PLAIN_TEXT)
overall_sentiment = lsclient.analyze_sentiment(document=document)

VS.

gcloud ml language analyze-sentiment --content="@stevenmnuchin1 @Apple @tim_cook How much did this trip cost us? I\'m sure you took your wife and did some sightseeing."

From Python - note that it is missing "score" + even the "magnitude" is different! And the "begin_offset" is wrong too...

document_sentiment {
  magnitude: 0.6000000238418579
}
language: "en"
sentences {
  text {
    content: "@stevenmnuchin1 @Apple @tim_cook How much did this trip cost us?"
    begin_offset: -1
  }
  sentiment {
    magnitude: 0.30000001192092896
    score: -0.30000001192092896
  }
}
sentences {
  text {
    content: "I\'m sure you took your wife and did some sightseeing."
    begin_offset: -1
  }
  sentiment {
    magnitude: 0.30000001192092896
    score: 0.30000001192092896
  }
}

VS. gcloud

{
  "documentSentiment": {
    "magnitude": 0.3,
    "score": -0.1
  },
  "language": "en",
  "sentences": [
    {
      "sentiment": {
        "magnitude": 0.3,
        "score": -0.3
      },
      "text": {
        "beginOffset": 0,
        "content": "@stevenmnuchin1 @Apple @tim_cook How much did this trip cost us?"
      }
    },
    {
      "sentiment": {
        "magnitude": 0.0,
        "score": 0.0
      },
      "text": {
        "beginOffset": 65,
        "content": "I\\'m sure you took your wife and did some sightseeing."
      }
    }
  ]
}
Alvin
  • 33
  • 5
  • Hi. I work on this product at Google. This might be a bug on our side. I will investigate. Meanwhile, the source of truth is the client library, so please rely on that. – Mona Attariyan Apr 15 '19 at 23:24
  • It seems like the issue is with "I\\'m". gcloud is adding an extra \, and indeed if you put "I\\'m sure you took your wife and did some sightseeing." into the library, your output from library will match gcloud. I will check to see why gcloud adds this. – Mona Attariyan Apr 16 '19 at 00:53
  • Thanks Mona! This is very helpful. From a product feedback perspective, it would be helpful to get some error code from the client library that there are problems with parsing instead of skipping (some of) the labels – Alvin Apr 16 '19 at 13:37
  • Actually Mona. I just tried this and ran into another problem. The overall document sentiment now works, but the sentiment at a sentence level is no longer working... :( Any insight would be greatly appreciated! – Alvin Apr 16 '19 at 17:18
  • Sorry, didn't follow. What did you try? Could you please paste the content and the command? – Mona Attariyan Apr 19 '19 at 17:39

0 Answers0