1

I'm using google natural language api to analyse the sentiment in a sentence. But, every time I run the script. It goes to an infinite loop of sleep.

My code.

comments = "This product is really good"
credentials = GoogleAuth.get_instance()
client = language.LanguageServiceClient(credentials=credentials)
document = types.Document(
    content=comments,
    type=enums.Document.Type.PLAIN_TEXT
)
sentiment = client.analyze_sentiment(document=document)

Stack trace.

Traceback (most recent call last):
  File "/Users/my_mac/app/sentiment_analysis_runner.py", line 14, in <module>
    main()
  File "/Users/my_mac/app/sentiment_analysis_runner.py", line 8, in main
    sentiment = sentiment_analyser.analyse(url)
  File "/Users/my_mac/app/sentiment_analysis/product_sentiment_analysis.py", line 20, in analyse
    return self.analyse_comments(comments)
  File "/Users/my_mac/app/sentiment_analysis/product_sentiment_analysis.py", line 32, in analyse_comments
    sentiment = client.analyze_sentiment(document=document)
  File "/Users/my_mac/PycharmProjects/HelloWorld/venv/lib/python3.6/site-packages/google/cloud/language_v1/gapic/language_service_client.py", line 230, in analyze_sentiment
    request, retry=retry, timeout=timeout, metadata=metadata
  File "/Users/my_mac/PycharmProjects/HelloWorld/venv/lib/python3.6/site-packages/google/api_core/gapic_v1/method.py", line 143, in __call__
    return wrapped_func(*args, **kwargs)
  File "/Users/my_mac/PycharmProjects/HelloWorld/venv/lib/python3.6/site-packages/google/api_core/retry.py", line 270, in retry_wrapped_func
    on_error=on_error,
  File "/Users/my_mac/PycharmProjects/HelloWorld/venv/lib/python3.6/site-packages/google/api_core/retry.py", line 205, in retry_target
    time.sleep(sleep)
KeyboardInterrupt

Process finished with exit code 1
Aakash
  • 1,751
  • 1
  • 14
  • 21

1 Answers1

0

I just tried the following code, based on the client library documentation, and it worked without issues.

from google.cloud import language
from google.cloud.language import enums
from google.cloud.language import types
from google.oauth2 import service_account

#Setting the Service Account key
credentials = service_account.Credentials.from_service_account_file('../key.json')
client = language.LanguageServiceClient(credentials=credentials)
text = u'This product is really good'
document = types.Document(
    content=text,
    type=enums.Document.Type.PLAIN_TEXT)
sentiment = client.analyze_sentiment(document=document).document_sentiment

print('Text: {}'.format(text))
print('Sentiment: {}, {}'.format(sentiment.score, sentiment.magnitude))

And got the output:

Text: This product is really good
Sentiment: 0.800000011921, 0.800000011921

Hope it helps.

F10
  • 2,843
  • 2
  • 12
  • 18
  • There was an issue with the credentials object I was passing into LanguageServiceClient. Not sure why it was not throwing an error. But, thanks for your response. – Aakash Mar 20 '19 at 10:02
  • HI @Aakash , good to know you solved your issue. Could you please post your solution as it may help the community if they had this same issue? – F10 Mar 26 '19 at 17:48
  • It just was a silly mistake, really. I had to access the auth variable to get the credentials like this ... credentials = GoogleAuth.get_instance().auth. But, not sure why I wasn't getting an error when I passed an object to the LanguageServiceClient. I'm pretty new to python and still trying to figure out. – Aakash Apr 01 '19 at 06:58
  • **@Aakash** could you specify the documentation you are using to determine it was required to add the `auth` part to the `GoogleAuth.get_instance()` method? I have been making a research and I haven't been able to find information about this method. Additionally, it would be great if you can add the libraries you are implementing. – Armin_SC Apr 02 '19 at 22:15