1

I'm trying to run a sample sentiment analysis, following the guide here: https://cloud.google.com/natural-language/docs/analyzing-sentiment#language-sentiment-string-python

However, on running the example, I get this error:

AttributeError: 'Client' object has no attribute 'analyze_sentiment'

Below is the code I'm trying:

def sentiment_text(text):
"""Detects sentiment in the text."""
client = language.Client.from_service_account_json('<my_creds>')

if isinstance(text, six.binary_type):
    text = text.decode('utf-8')
    # Instantiates a plain text document.
    document = types.Document(
        content=text,
        type=enums.Document.Type.PLAIN_TEXT)
    # Detects sentiment in the document. You can also analyze HTML 
with:
    #   document.type == enums.Document.Type.HTML
    sentiment = client.analyze_sentiment(document).document_sentiment
    print('Score: {}'.format(sentiment.score))
    print('Magnitude: {}'.format(sentiment.magnitude))

I can't seem to find any docs about the error - am I missing something?

dTaylor
  • 62
  • 1
  • 6

1 Answers1

0

Did you use client like this?

This is for strings in gcp.

client = language_v1.LanguageServiceClient.from_service_account_json("key1.json")

When you call just language it is for documents.

Paulo Campez
  • 702
  • 1
  • 8
  • 24