module 'google.cloud.language_v1' has no attribute 'EncodingType'

1.2k Views Asked by At

I am learning google natural language API by following a sample python code, and running the same in google colab, however, I am getting below error..

# Create the document
document = { 
    "type_": "PLAIN_TEXT",
    "language": "en",
    "content": "Hello World. I love you! I hate you!"
}

results = client.analyze_sentiment(
    document = document,
    encoding_type = language_v1.EncodingType.UTF8)

And I am getting below error

AttributeError                            Traceback (most recent call last)
<ipython-input-26-17157fa1dfc4> in <module>()
      8 results = client.analyze_sentiment(
      9     document = document,
---> 10     encoding_type = language_v1.EncodingType.UTF8)

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

This seems a straight forward error but after trying different options, like removing encoding_type, it still does not work

1

There are 1 best solutions below

0
On

I have found root cause. There are 2 issues in the sample code:

  1. "type_" is not valid. It should be changed to "type"
  2. encoding_type should just be set to "UTF8" directly. Such as encoding_type = "UTF8"