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

主宰稳场 提交于 2021-01-28 12:30:54

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/44520110/how-to-send-multiple-text-strings-in-a-single-post-request-to-google-cloud-natur

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!