how to send multiple text strings in a single post request to google cloud natural language api

here is my python code

def sentiment_local_file(text):

  """Detects sentiment in the local document"""
  language_client = language.Client()

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

  with open("abhi.txt",'r') as fr:
      data = json.loads(fr.read())

  print ([data['document']['content']])
  document = language_client.document_from_text(data['document']['content'])
  result = document.annotate_text(include_sentiment=True,
                                         include_syntax=False,
                                         include_entities=False)

I am trying to send list of strings in a single post request for analysis but it is giving an error . This is the text file i am reading. In above code text refer to file name and the code sample is a function

{
 "document":{
 "type":"PLAIN_TEXT",
 "language": "EN",
 "content":[

    "pretending to be very busy"
  ,

    "being totally unconcerned"
  ,

    "a very superior attitude"
  ,

    "calm, dignified and affectionate disposition" 
]},"encodingType":"UTF8"}

i read documentation and many examples still unable to figure it out.

Upvotes: 1

Views: 726

Answers (1)

Bruno Henrique
Bruno Henrique

Reputation: 13

As I know, there is no way to send a list of strings to be analysed. The sentences returned is an array because GNL API, breaks every sentence and analyses it.

Lets say that you send the following request:

{
 "document": {
  "type": "PLAIN_TEXT",
  "content": "Terrible, I did not like the last updated."
 }
}
The response probably will be:
{
    "language": "en",
    "sentences": [
        {
            "text": {
                "content": "Terrible, I did not like the last updated.",
                "beginOffset": -1
            },
            "sentiment": {
                "magnitude": 0.9,
                "score": -0.9
            }
        }
    ]
}
The response above, there is an array called ```sentence``` but with only one element. It happens because the text we sent to be analysed there is only one sentence. So, following is another sample:

Request:

{
 "document": {
  "type": "PLAIN_TEXT",
  "content": "Terrible, I did not like the last updated. Also, I would like to have access to old version"
 }
}

The probably response will be:

{
    "language": "en",
    "sentences": [
        {
            "text": {
                "content": "Terrible, I did not like the last updated.",
                "beginOffset": -1
            },
            "sentiment": {
                "magnitude": 0.9,
                "score": -0.9
            }
        },
        {
            "text": {
                "content": "Also, I would like to have access to old version",
                "beginOffset": -1
            },
            "sentiment": {
                "magnitude": 0,
                "score": 0
            }
        }
    ]
}

In this case, the sentence array has two elements. It happened because the text sent has two sentences (separated by period ".").

Hope it helps you.

Upvotes: 1

Related Questions