Wit.ai 如何在一条消息中处理多个实体?

How to work with multiple entities in one message in Wit.ai?

我关注 this tutorial to write a simple script 使用 Wit.ai。

所以,有一个代码片段可以从第一条消息中检索实体:

def first_entity_value(entities, entity):
    if entity not in entities:
        return None
    val = entities[entity][0]['value']
    if not val:
        return None
    return val['value'] if isinstance(val, dict) else val

我有两个问题:

  1. 如何从其他消息中获取实体?那么,当用户键入内容时(不是第一条消息)?
  2. 我在消息中有多个实体(例如:我将在这个周末访问伦敦,例如,我怎样才能得到第二个实体(周末)? 现在我尝试写类似下面的东西但出现错误:

    def first_entity_value(entities, entity):
        if entity not in entities:
            return None
        val = entities[entity][0][1]['value'] # to get the second entity
        if not val:
            return None
        return val['value'] if isinstance(val, dict) else val
    

"London" 是位置,"weekend" 是日期时间。它们不是同一个实体。

要检索两个实体,只需调整 entity 参数:

city = first_entity_value(entities, 'location')
date = first_entity_value(entities, 'datetime')

如果您想检索同一实体的两个值(例如:我喜欢 ParisLondon),那么您应该使用您尝试的方法:

def get_entity_value(entities, entity, pos):
    if entity not in entities:
        return None
    val = entities[entity][pos]['value'] # to get the entity at "pos"
    if not val:
        return None
    return val['value'] if isinstance(val, dict) else val

我不太明白你的第一个问题。每次收到用户的消息时选择的操作(来自 Wit converse)is/are 运行。