I'm using the language API to do sentiment analysis but the polarity that I'm receiving back is always 0.0
I expect polarity to be:
the sentiment ranges between
-1.0(negative) and1.0(positive) and corresponds to the overall emotional leaning of the text [[1](https://cloud.google.com/natural-language/docs/basics#sentiment_analysis_response_fields)]
Here is an example of calling documents.analyzeSentiment using the autogenerated REST client [2]. It returns polarity as expected.
require "google/apis/language_v1beta1"
def generated_print_sentiment text
language_service = Google::Apis::LanguageV1beta1::CloudNaturalLanguageAPIService.new
language_service.authorization = Google::Auth.get_application_default(
%[ https://www.googleapis.com/auth/cloud-platform ]
)
document = Google::Apis::LanguageV1beta1::Document.new
document.content = text
document.type = "PLAIN_TEXT"
document.language = "EN"
analyze_sentiment_request_object = Google::Apis::LanguageV1beta1::AnalyzeSentimentRequest.new
analyze_sentiment_request_object.document = document
response = language_service.analyze_document_sentiment analyze_sentiment_request_object
sentiment = response.document_sentiment
puts "GENERATED"
puts text.inspect
puts "Polarity: #{sentiment.polarity}"
puts "Magnitude: #{sentiment.magnitude}"
puts "------------------------------------------"
end
happy = "Today is a gloriously wonderful beautiful day. I love it! Stupendous!"
angry = "I hate you. I am so angry and mad about today. I hate this so much."
generated_print_sentiment happy
generated_print_sentiment angry
GENERATED
"Today is a gloriously wonderful beautiful day. I love it! Stupendous!"
Polarity: 1
Magnitude: 2.7
------------------------------------------
GENERATED
"I hate you. I am so angry and mad about today. I hate this so much."
Polarity: -1
Magnitude: 1.5
------------------------------------------
Here is an example of the same API call using google-cloud-language (gRPC). Polarity is not returned as expected.
# Using hand-written client-library:
# https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/google-cloud/v0.21.0/google/cloud/language
require "google/cloud"
def gcloud_print_sentiment text
gcloud = Google::Cloud.new
language = gcloud.language
document = language.document text
sentiment = document.sentiment
puts "GCLOUD"
puts text.inspect
puts "Polarity: #{sentiment.polarity}"
puts "Magnitude: #{sentiment.magnitude}"
puts "------------------------------------------"
end
happy = "Today is a gloriously wonderful beautiful day. I love it! Stupendous!"
angry = "I hate you. I am so angry and mad about today. I hate this so much."
gcloud_print_sentiment happy
gcloud_print_sentiment angry
GCLOUD
"Today is a gloriously wonderful beautiful day. I love it! Stupendous!"
Polarity: 0.0
Magnitude: 2.700000047683716
------------------------------------------
GCLOUD
"I hate you. I am so angry and mad about today. I hate this so much."
Polarity: 0.0
Magnitude: 1.5
------------------------------------------
Is there a chance that the polarity isn't being populated properly from the gRPC response?
I haven't yet tested this using raw gRPC to verify that endpoint definitely returns the response correctly. But the REST response is correct.
@remi Thanks for reporting this. I was about to comment that google-cloud-ruby has acceptance testing asserting a polarity of 1.0 for a given sample string, when I noticed these errors in our latest Travis CI build.
3) Failure:
Language (TEXT/Storage File)::language::annotation#test_0001_works without creating a document [/home/travis/build/GoogleCloudPlatform/google-cloud-ruby/google-cloud-language/acceptance/language/storage/text_file_test.rb:34]:
Expected: 1.0
Actual: 0.0
4) Failure:
Language (TEXT)::language::annotation#test_0001_works without creating a document [/home/travis/build/GoogleCloudPlatform/google-cloud-ruby/google-cloud-language/acceptance/language/text_test.rb:30]:
Expected: 1.0
Actual: 0.0
5) Failure:
Language (TEXT/Storage URL)::language::annotation#test_0001_works without creating a document [/home/travis/build/GoogleCloudPlatform/google-cloud-ruby/google-cloud-language/acceptance/language/storage/text_url_test.rb:35]:
Expected: 1.0
Actual: 0.0
The fixture in the unit test sentiment_test.rb looks ok to me compared to (v1beta1/Sentiment), and this test is currently passing.
The version of google-gax was recently updated to 0.6.0. I tried running the acceptance tests after reverting to 0.5.0, but this does not fix the issue.
I've looked into this using the recently released google-cloud-language 0.21.0 and the older google-cloud-language 0.20.2. Near as I can tell the GRPC API is returning 0.0 for all requests. Meaning, this is not a problem with google-cloud-language, but a problem with the upstream API. The REST/JSON API is behaving correctly though, so this seems to be a problem with the GRPC endpoints.
I filed a bug with the engineering team. Waiting on response.
@remi, polarity seems to be working again. Can you confirm?
I just got this in my console:
sentiment = gcloud.language.sentiment "hello, nice to meet you"
sentiment.polarity #=> 1.0
Confirmed. And I can confirm an API update that fixes this. Closing! 馃槃
Most helpful comment
I filed a bug with the engineering team. Waiting on response.