gpt4 book ai didi

python - 如何在单个发布请求中将多个文本字符串发送到谷歌云自然语言API

转载 作者:太空宇宙 更新时间:2023-11-03 15:07:33 25 4
gpt4 key购买 nike

这是我的Python代码

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)

我试图在单个发布请求中发送字符串列表以进行分析,但出现错误。这是我正在阅读的文本文件。上面的代码文本指的是文件名,代码示例是一个函数

{
"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"}

我阅读了文档和许多示例仍然无法弄清楚。

最佳答案

据我所知,没有办法发送要分析的字符串列表。返回的句子是一个数组,因为 GNL API 会分解每个句子并对其进行分析。

假设您发送以下请求:

{
"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``` 的数组,但只有一个元素。发生这种情况是因为我们发送来进行分析的文本只有一个句子。因此,以下是另一个示例:

请求:

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

可能的 react 是:

{
"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 数组有两个元素。发生这种情况是因为发送的文本有两个句子(用句点“.”分隔)。

希望对您有帮助。

关于python - 如何在单个发布请求中将多个文本字符串发送到谷歌云自然语言API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44520110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com