我有这个来自Google Cloud Natural Language API的输出结果(我花了相当长的时间来产生它,所以我不想使用How can I JSON serialize an object from google's natural language API? (No __dict__ attribute)的解决方案)
Mentions:
Name: "Trump"
Begin Offset : 0
Content : Trump
Magnitude : 0.0
Sentiment : 0.0
Type : 2
Salience: 0.6038374900817871
Sentiment:
Mentions:
Name: "hand"
Begin Offset : 19
Content : hand
Magnitude : 0.0
Sentiment : 0.0
Type : 2
Salience: 0.20075689256191254
Sentiment:
Mentions:
Name: "water boarding"
Begin Offset : 39
Content : water boarding
Magnitude : 0.0
Sentiment : 0.0
Type : 2
Salience: 0.13010266423225403
Sentiment:
Mentions:
Name: "some"
Begin Offset : 58
Content : some
Magnitude : 0.0
Sentiment : 0.0
Type : 2
Salience: 0.04501711577177048
Sentiment:
Mentions:
Name: "GOPDebate"
Begin Offset : 65
Content : GOPDebate
Magnitude : 0.0
Sentiment : 0.0
Type : 1
Salience: 0.020285848528146744
Sentiment:
我想要找出一组候选人名字(唐纳德·特朗普、希拉里·克林顿、伯尼·桑德斯和泰德·克鲁兹)的重要性和情感--或者一组类似的名字,比如只有trump/hillary/clinton/cruz/bernie/sanders/@realdonaldtrump).
一开始,我没有意识到输出文件不是json。事实上,我不确定格式是什么。我被告知这可能是格式错误的YAML。有没有办法把这些文件转换成json?正如我所说的,我已经处理了很多文件,在这一点上修改协议并创建json对我来说是不实际的。
Google Cloud NLP教程中的代码部分实现了这一点:
# [START def_entity_sentiment_text]
def entity_sentiment_text(text):
"""Detects entity sentiment in the provided text."""
client = language.LanguageServiceClient()
if isinstance(text, six.binary_type):
text = text.decode('utf-8')
document = types.Document(
content=text.encode('utf-8'),
type=enums.Document.Type.PLAIN_TEXT)
# Detect and send native Python encoding to receive correct word offsets.
encoding = enums.EncodingType.UTF32
if sys.maxunicode == 65535:
encoding = enums.EncodingType.UTF16
result = client.analyze_entity_sentiment(document, encoding)
for entity in result.entities:
print('Mentions: ')
print(u'Name: "{}"'.format(entity.name))
for mention in entity.mentions:
print(u' Begin Offset : {}'.format(mention.text.begin_offset))
print(u' Content : {}'.format(mention.text.content))
print(u' Magnitude : {}'.format(mention.sentiment.magnitude))
print(u' Sentiment : {}'.format(mention.sentiment.score))
print(u' Type : {}'.format(mention.type))
print(u'Salience: {}'.format(entity.salience))
print(u'Sentiment: {}\n'.format(entity.sentiment))
# [END def_entity_sentiment_text]
所以我甚至不确定如何将答案应用到另一个中,所以在这里。
发布于 2020-07-24 00:07:18
正在旧线程中添加答案。使用MessageToDict
或MessageToJson
可以将来自protobuf格式的NLP的响应转换为JSON,如下所示
from google.protobuf import json_format
import json
response_json = json.loads(json_format.MessageToJson(result))
https://stackoverflow.com/questions/49139188
复制相似问题