首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将Google Cloud NLP API实体情感输出转换为JSON

将Google Cloud NLP API实体情感输出转换为JSON
EN

Stack Overflow用户
提问于 2018-03-07 04:10:22
回答 1查看 826关注 0票数 2

我有这个来自Google Cloud Natural Language API的输出结果(我花了相当长的时间来产生它,所以我不想使用How can I JSON serialize an object from google's natural language API? (No __dict__ attribute)的解决方案)

代码语言:javascript
运行
复制
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教程中的代码部分实现了这一点:

代码语言:javascript
运行
复制
# [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]

所以我甚至不确定如何将答案应用到另一个中,所以在这里。

EN

回答 1

Stack Overflow用户

发布于 2020-07-24 00:07:18

正在旧线程中添加答案。使用MessageToDictMessageToJson可以将来自protobuf格式的NLP的响应转换为JSON,如下所示

代码语言:javascript
运行
复制
from google.protobuf import json_format
import json
response_json = json.loads(json_format.MessageToJson(result)) 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49139188

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档