如何解析python的响应



这是我使用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'}]}}

假设您将此输出存储在变量中,例如:

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']

我不明白您对实体类型的含义。

最新更新