【问题标题】:How to parse wit.ai response in python如何在 python 中解析 wit.ai 响应
【发布时间】:2026-01-03 07:20:06
【问题描述】:

这是我使用 python 客户端从我的 wit.ai 应用程序获得的响应。 我要做的就是提取:

  1. 意图值字段。
  2. 实体类型。
  3. 实体的值字段。

{'msg_id': '0KqBWZaeY9qKeVvdv3n', '_text': 'what is the temperature', 'entities': {'on_off': [{'confidence': 0.98730879525862, 'value': 'on'}], 'intent': [{'confidence': 0.99846661176623, 'value': 'get_temperature'}]}}

请注意,消息每次都可能不同。在字典中硬编码位置可能不是一个好主意。

{'msg_id': '0GN7pJRwYincs2p7xCo', '_text': 'turn light 1 off', 'entities': {'number': [{'confidence': 1, 'value': 1, 'type': 'value'}], 'on_off' [{'confidence': 0.96433768880251, 'value': 'off'}], 'intent': [{'confidence': 0.99552821331643, 'value': 'lights'}]}}

【问题讨论】:

    标签: python parsing wit.ai


    【解决方案1】:

    假设您将此输出存储在一个变量中,如下所示:

    dictionary = {'msg_id': '0KqBWZaeY9qKeVvdv3n', '_text': 'what is the temperature', 'entities': {'on_off': [{'confidence': 0.98730879525862, 'value': 'on'}], 'intent': [{'confidence': 0.99846661176623, 'value': 'get_temperature'}]}}
    

    意图值会在这里:

    intentValue = dictionary['entities']['intent'][0]['value']
    

    实体值会在这里:

    entityValue = dictionary['entities']['on_off'][0]['value']
    

    我不明白你所说的实体类型是什么意思。

    【讨论】:

    • 感谢您的快速回复。太棒了!通过实体类型,我的意思是“on_off”。但是,我没有意识到我收到的消息可能会有所不同,并且硬编码位置并不是最好的主意。让我更新这个问题。再次感谢。
    最近更新 更多