问题描述
这是我的python代码
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.
推荐答案
据我所知,无法发送要分析的字符串列表.返回的句子是一个数组,因为GNL API会中断每个句子并对其进行分析.
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."
}
}
响应可能是:
{
"language": "en",
"sentences": [
{
"text": {
"content": "Terrible, I did not like the last updated.",
"beginOffset": -1
},
"sentiment": {
"magnitude": 0.9,
"score": -0.9
}
}
]
}
上面的响应中有一个名为`sentence`的数组,但是只有一个元素.发生这种情况是因为我们发送给要分析的文本只有一个句子.因此,以下是另一个示例:
请求:
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"
}
}
可能的响应将是:
{
"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
}
}
]
}
在这种情况下, sentence
数组包含两个元素.发生这种情况是因为发送的文本有两个句子(以句点."分隔).
In this case, the sentence
array has two elements. It happened because the text sent has two sentences (separated by period ".").
希望它对您有帮助.
这篇关于如何在单个帖子请求中向Google Cloud Natural Language API发送多个文本字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!