how to convert google cloud natural language entity sentiment response to JSON/dict in Python?

1.6k Views Asked by At

I am trying to use google cloud natural language API for analyzing entity sentiments.

from google.cloud import language_v1
import os 
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'path/to/json'

client = language_v1.LanguageServiceClient()
text_content = 'Grapes are good. Bananas are bad.'

# Available types: PLAIN_TEXT, HTML
type_ = language_v1.Document.Type.PLAIN_TEXT

# Optional. If not specified, the language is automatically detected.
# For list of supported languages:
# https://cloud.google.com/natural-language/docs/languages
document = language_v1.Document(content=text_content, type_=language_v1.Document.Type.PLAIN_TEXT)

# Available values: NONE, UTF8, UTF16, UTF32
encoding_type = language_v1.EncodingType.UTF8
response = client.analyze_entity_sentiment(request = {'document': document, 'encoding_type': encoding_type})

I then print out the entities and its attributes from the response.

for entity in response.entities:
    print('=' * 20)
    print(type(entity))
    print(entity)

====================
<class 'google.cloud.language_v1.types.language_service.Entity'>
name: "Grapes"
type_: OTHER
salience: 0.8335162997245789
mentions {
  text {
    content: "Grapes"
  }
  type_: COMMON
  sentiment {
    magnitude: 0.8999999761581421
    score: 0.8999999761581421
  }
}
sentiment {
  magnitude: 0.8999999761581421
  score: 0.8999999761581421
}

====================
<class 'google.cloud.language_v1.types.language_service.Entity'>
name: "Bananas"
type_: OTHER
salience: 0.16648370027542114
mentions {
  text {
    content: "Bananas"
    begin_offset: 17
  }
  type_: COMMON
  sentiment {
    magnitude: 0.8999999761581421
    score: -0.8999999761581421
  }
}
sentiment {
  magnitude: 0.8999999761581421
  score: -0.8999999761581421
}

Now I want to store the entire response in a JSON or dictionary format so that I can store it to a table in database or do my processing. I tried following converting Google Cloud NLP API entity sentiment output to JSON and How can I JSON serialize an object from google's natural language API? (No __dict__ attribute) but it did not work.

If i use

from google.protobuf.json_format import MessageToDict, MessageToJson 
result_dict = MessageToDict(response)
result_json = MessageToJson(response)

I get an error saying

>>> result_dict = MessageToDict(response)
Traceback (most recent call last):
  File "/Users/pmehta/Anaconda-3/anaconda3/envs/nlp_36/lib/python3.6/site-packages/proto/message.py", line 555, in __getattr__
    pb_type = self._meta.fields[key].pb_type
KeyError: 'DESCRIPTOR'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/pmehta/Anaconda-3/anaconda3/envs/nlp_36/lib/python3.6/site-packages/google/protobuf/json_format.py", line 175, in MessageToDict
    return printer._MessageToJsonObject(message)
  File "/Users/pmehta/Anaconda-3/anaconda3/envs/nlp_36/lib/python3.6/site-packages/google/protobuf/json_format.py", line 209, in _MessageToJsonObject
    message_descriptor = message.DESCRIPTOR
  File "/Users/pmehta/Anaconda-3/anaconda3/envs/nlp_36/lib/python3.6/site-packages/proto/message.py", line 560, in __getattr__
    raise AttributeError(str(ex))
AttributeError: 'DESCRIPTOR'

How do I parse this response to convert it correctly to a json or dict?

2

There are 2 best solutions below

1
On BEST ANSWER

As part of google-cloud-language 2.0.0 migration, response messages are provided by proto-plus, which wraps the raw protobuf messages. ParseDict and MessageToDict are methods provided by protobuf and since proto-plus wraps the proto messages those protobuf methods can no longer be used directly.

Replace

from google.protobuf.json_format import MessageToDict, MessageToJson 
result_dict = MessageToDict(response)
result_json = MessageToJson(response)

with

import json
result_json = response.__class__.to_json(response)
result_dict = json.loads(result_json)
result_dict
0
On

tl;dr The accepted solution is not an apples-to-apples replacement. In order to recover the original behavior, you'll need to do:

from google.protobuf.json_format import MessageToDict
result_dict = MessageToDict(response.__class__.pb(response))

After going through this myself, I'd like to note that to_json and MessageToDict have breaking changes. The parameters including_default_value_fields and use_integers_for_enums default to False for MessageToDict, and they now default to True for to_json.

Learn more here: https://github.com/googleapis/proto-plus-python/blob/5c14cbaf21e3864a247e0183480903e7640e5460/proto/message.py#L372

Here's a reference to the official implementation of to_json:

def to_json(cls, instance, *, use_integers_for_enums=True) -> str:
    """Given a message instance, serialize it to json

    Args:
        instance: An instance of this message type, or something
            compatible (accepted by the type's constructor).
        use_integers_for_enums (Optional(bool)): An option that determines whether enum
            values should be represented by strings (False) or integers (True).
            Default is True.

    Returns:
        str: The json string representation of the protocol buffer.
    """
    return MessageToJson(
        cls.pb(instance),
        use_integers_for_enums=use_integers_for_enums,
        including_default_value_fields=True,
    )