2

I have this output result from Google Cloud Natural Language API (took me quite a while to produce it so I don't want to go with the solution in How can I JSON serialize an object from google's natural language API? (No __dict__ attribute) )

Mentions: 
Name: "Trump"
  Begin Offset : 0
  Content : Trump
  Magnitude : 0.0
  Sentiment : 0.0
  Type : 2
Salience: 0.6038374900817871
Sentiment: 

Mentions: 
Name: "hand"
  Begin Offset : 19
  Content : hand
  Magnitude : 0.0
  Sentiment : 0.0
  Type : 2
Salience: 0.20075689256191254
Sentiment: 

Mentions: 
Name: "water boarding"
  Begin Offset : 39
  Content : water boarding
  Magnitude : 0.0
  Sentiment : 0.0
  Type : 2
Salience: 0.13010266423225403
Sentiment: 

Mentions: 
Name: "some"
  Begin Offset : 58
  Content : some
  Magnitude : 0.0
  Sentiment : 0.0
  Type : 2
Salience: 0.04501711577177048
Sentiment: 

Mentions: 
Name: "GOPDebate"
  Begin Offset : 65
  Content : GOPDebate
  Magnitude : 0.0
  Sentiment : 0.0
  Type : 1
Salience: 0.020285848528146744
Sentiment: 

I want to find the Magnitude and Sentiment for a set of candidate names (Donald Trump, Hillary Clinton, Bernie Sanders and Ted Cruz--or a set of similar name like only trump/hillary/clinton/cruz/bernie/sanders/@realdonaldtrump).

At first I didn't realize that output files are not json. Indeed I am not sure what the format is. I was told it is perhaps malformatted YAML. Is there a way to convert these files to json? As I said, I have processed lots of files and it is not practical for me to modify the protobuf and create json at this point.

The part of code from Google Cloud NLP tutorial that does this is:

# [START def_entity_sentiment_text]
def entity_sentiment_text(text):
    """Detects entity sentiment in the provided text."""
    client = language.LanguageServiceClient()

    if isinstance(text, six.binary_type):
        text = text.decode('utf-8')

    document = types.Document(
        content=text.encode('utf-8'),
        type=enums.Document.Type.PLAIN_TEXT)

    # Detect and send native Python encoding to receive correct word offsets.
    encoding = enums.EncodingType.UTF32
    if sys.maxunicode == 65535:
        encoding = enums.EncodingType.UTF16

    result = client.analyze_entity_sentiment(document, encoding)

    for entity in result.entities:
        print('Mentions: ')
        print(u'Name: "{}"'.format(entity.name))
        for mention in entity.mentions:
            print(u'  Begin Offset : {}'.format(mention.text.begin_offset))
            print(u'  Content : {}'.format(mention.text.content))
            print(u'  Magnitude : {}'.format(mention.sentiment.magnitude))
            print(u'  Sentiment : {}'.format(mention.sentiment.score))
            print(u'  Type : {}'.format(mention.type))
        print(u'Salience: {}'.format(entity.salience))
        print(u'Sentiment: {}\n'.format(entity.sentiment))
# [END def_entity_sentiment_text]

So I am not even sure how to apply the answer in the other SO here.

Mona Jalal
  • 34,860
  • 64
  • 239
  • 408
  • 1
    if this is indeed in a text format parsing will require some version of `regex`, `grep`, etc... you might be better off redoing it with the correct format – eagle Mar 06 '18 at 20:25
  • I wonder if people in charge of Google Cloud NLP API might have provisioned a method for dealing with these output files? – Mona Jalal Mar 06 '18 at 20:35
  • @eagle please see the update to my question and let me know how I can proceed – Mona Jalal Mar 06 '18 at 20:38
  • 2
    feed your result into one of those two objects, `MessageToDict` or `MessageToJson` – eagle Mar 06 '18 at 20:52

1 Answers1

0

Adding answer to an old thread. Response from NLP APIs in protobuf format can be converted to JSON using MessageToDict or MessageToJson as below

from google.protobuf import json_format
import json
response_json = json.loads(json_format.MessageToJson(result)) 
raj
  • 1,163
  • 7
  • 9
  • 1
    when i ran this code, it returned an error `AttributeError: 'DESCRIPTOR'` . can u help me resolve this? – Regressor Oct 21 '20 at 19:08
  • Is it the same code as OP you are trying with? If not, I would suggest to open a new question. – raj Oct 21 '20 at 22:00
  • i have posted the question here - https://stackoverflow.com/questions/64470470/how-to-convert-google-cloud-natural-language-entity-sentiment-response-to-json-d – Regressor Oct 21 '20 at 23:00