Skip to content
Advertisement

Need help understanding ‘no attribute’ error message in Google Natural Language API

Situation

I’m trying to run Google’s Cloud NLP sentiment analysis on a text field pulled from the Twitter API with Tweepy and then turned into a pandas Dataframe. That Dataframe has a text field called text, which is the tweet content on which I’d like to run the sentiment analysis.

This is my reference code:

https://cloud.google.com/natural-language/docs/reference/libraries

Expectation

I was expecting this process to run sentiment analysis on the text field in my Dataframe, returning values for sentiment score and sentiment magnitude. Next step would be to then send those values back into the dataframe.

What I’ve tried

Below is the code I’m running.

# Imports the Google Cloud client library
from google.cloud import language_v1


# Instantiates a client
client = language_v1.LanguageServiceClient()

# The text to analyze
text = dfTWEENGAGE.loc[:,"text"]
document = language_v1.Document(
    content=text, type_=language_v1.Document.Type.PLAIN_TEXT
)

# Detects the sentiment of the text
sentiment = client.analyze_sentiment(
    request={"document": document}
).document_sentiment

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

The only thing I’ve changed from the example is:

text = dfTWEENGAGE.loc[:,"text"]

… to find the text column in the dfTWEENGAGE Dataframe.

Results

It’s returning the following error message:

AttributeError: module 'google.cloud.language_v1' has no attribute 'Document'

This is confusing to me because I’m on the documentation and seeing the ‘Document’ attribute here:

https://cloud.google.com/natural-language/docs/reference/rpc/google.cloud.language.v1#google.cloud.language.v1.Document

The code following Document also matches the API documentation: .Type.PLAIN_TEXT

So I’d greatly appreciate any insights or explanations regarding the Document attribute and what I might be doing wrong here.

dsx

Advertisement

Answer

You just need to update your type_=language_v1.Document.Type.PLAIN_TEXT into type_=language_v1.types.Document.Type.PLAIN_TEXT and your code will run successfully. Please see this api documentation for more information.

In addition, make sure that you have the updated google-cloud-language library by running this pip install --upgrade google-cloud-language

Below is my testing using your code and using u"Hello, world!" as my sample text input.

# Imports the Google Cloud client library
from google.cloud import language_v1


# Instantiates a client
client = language_v1.LanguageServiceClient()

# The text to analyze
text = u"Hello, world!"
document = language_v1.Document(
    content=text, type_=language_v1.types.Document.Type.PLAIN_TEXT
)

# Detects the sentiment of the text
sentiment = client.analyze_sentiment(
    request={"document": document}
).document_sentiment

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

Output:

enter image description here

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement