from google.cloud import language_v1
import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'path/to/json'
client = language_v1.LanguageServiceClient()
text_content = 'Grapes are good. Bananas are bad.'
# Available types: PLAIN_TEXT, HTML
type_ = language_v1.Document.Type.PLAIN_TEXT
# Optional. If not specified, the language is automatically detected.
# For list of supported languages:
# https://cloud.google.com/natural-language/docs/languages
document = language_v1.Document(content=text_content, type_=language_v1.Document.Type.PLAIN_TEXT)
# Available values: NONE, UTF8, UTF16, UTF32
encoding_type = language_v1.EncodingType.UTF8
response = client.analyze_entity_sentiment(request = {'document': document, 'encoding_type': encoding_type})
for entity in response.entities:
print('=' * 20)
print(type(entity))
print(entity)
====================
<class 'google.cloud.language_v1.types.language_service.Entity'>
name: "Grapes"
type_: OTHER
salience: 0.8335162997245789
mentions {
text {
content: "Grapes"
}
type_: COMMON
sentiment {
magnitude: 0.8999999761581421
score: 0.8999999761581421
}
}
sentiment {
magnitude: 0.8999999761581421
score: 0.8999999761581421
}
====================
<class 'google.cloud.language_v1.types.language_service.Entity'>
name: "Bananas"
type_: OTHER
salience: 0.16648370027542114
mentions {
text {
content: "Bananas"
begin_offset: 17
}
type_: COMMON
sentiment {
magnitude: 0.8999999761581421
score: -0.8999999761581421
}
}
sentiment {
magnitude: 0.8999999761581421
score: -0.8999999761581421
}
from google.protobuf.json_format import MessageToDict, MessageToJson
result_dict = MessageToDict(response)
result_json = MessageToJson(response)
我得到一个错误的说法
>>> result_dict = MessageToDict(response)
Traceback (most recent call last):
File "/Users/pmehta/Anaconda-3/anaconda3/envs/nlp_36/lib/python3.6/site-packages/proto/message.py", line 555, in __getattr__
pb_type = self._meta.fields[key].pb_type
KeyError: 'DESCRIPTOR'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/pmehta/Anaconda-3/anaconda3/envs/nlp_36/lib/python3.6/site-packages/google/protobuf/json_format.py", line 175, in MessageToDict
return printer._MessageToJsonObject(message)
File "/Users/pmehta/Anaconda-3/anaconda3/envs/nlp_36/lib/python3.6/site-packages/google/protobuf/json_format.py", line 209, in _MessageToJsonObject
message_descriptor = message.DESCRIPTOR
File "/Users/pmehta/Anaconda-3/anaconda3/envs/nlp_36/lib/python3.6/site-packages/proto/message.py", line 560, in __getattr__
raise AttributeError(str(ex))
AttributeError: 'DESCRIPTOR'
如何解析此响应以将其正确转换为json或DICT?
作为google-cloud-language
2.0.0迁移的一部分,响应消息由proto-plus
提供,它包装了原始的protobuf消息。parsedict
和messageTodict
是由protobuf
提供的方法,由于proto-plus
包装了proto消息,因此不能再直接使用这些protobuf方法。
替换
from google.protobuf.json_format import MessageToDict, MessageToJson
result_dict = MessageToDict(response)
result_json = MessageToJson(response)
与
import json
result_json = response.__class__.to_json(response)
result_dict = json.loads(result_json)
result_dict
我通过扩展改型回调生成了类。我想将响应解析为字符串。在我的例子中,每个响应都得到这种类型的JSON对象。在成功回应的情况下: 万一发生故障响应 如何将响应体转换为JsonObject,以便检查字段“success”:并在成功时导航各自方法--onResponseSuccess(Call Call,response response);在失败时导航onResponseFailure(Call Cal
我正在做关于口头证词转录的情感分析的论文,对于Google Cloud的自然语言API V1Beta2背后的编程有几个问题/澄清。 我对任何答案都持开放态度。此外,如果任何人知道任何官方的谷歌文件列出了这些信息,也将非常感谢。谢谢你。
我可以使用toEntity()方法返回ResponseEntity,如下所示: 但是我想在返回之前处理响应头。上面的代码将WebClient响应转换为响应实体并立即返回,但我想将其存储在响应实体变量中,处理它,然后返回响应实体。 我提到了这个- 当我试图将其存储在varibale中时,我遇到了这个错误-
我需要将其转换为以下格式: 类型的数量可以改变(例如,可以只有A和B)。有人能帮我吗?我使用这个组件在网站https://js.devexpress.com/demos/widgetsgallery/demo/datagrid/simplearray/angular/light/上显示数据
我一直在android studio上开发一个Java应用程序,我想提取我存储在google数据存储中的几个实体。实体存储为列表 我只需要每个属性的“值”。 有没有办法将响应转换回带有json的数据存储实体? 编辑:我的代码片段
使用此JSON,自动JSON到POJO严重失败。 请注意,不同请求的项目数量不同。在这里,我将包含两项JSON响应。 这个JSON对象的POJO会是什么样子?