1

我正在使用具有自定义插槽 - 性别的 Flask-Ask 制作 alexa 技能。主要取值为“男”、“女”,对应的同义词有“他”、“她”、“男孩”、“女孩”等

该技能只是根据人的性别做出反应。例如。“他 24 岁”的话语应该给出“男性”,但给出“他”作为回应

我可以在技能的 Json 输出中看到正确的值,但是是否有一个更简单的内置函数来处理烧瓶中的分辨率,而不是在意图处理程序中编码或解析 json 响应?

任何帮助将不胜感激

4

1 回答 1

1

我遇到了类似的问题,我用一个小函数解析了 JSON:

def resolved_values(request):
    """
    Takes the request JSON and converts it into a dictionary of your intent
    slot names with the resolved value.

    Example usage:

    resolved_vals = resolved_values(request)
    txt = ""
    for key, val in resolved_vals.iteritems():
        txt += "\n{}:{}".format(key, val)


    :param request: request JSON
    :return: {intent_slot_name: resolved_value}
    """
    slots = request["intent"]["slots"]
    slot_names = slots.keys()

    resolved_vals = {}

    for slot_name in slot_names:
        slot = slots[slot_name]

        if "resolutions" in slot:
            slot = slot["resolutions"]["resolutionsPerAuthority"][0]
            slot_status = slot["status"]["code"]
            if slot_status == "ER_SUCCESS_MATCH":
                resolved_val = slot["values"][0]["value"]["name"]
                resolved_vals[slot_name] = resolved_val
            else:
                resolved_vals[slot_name] = None
        else:  # No value found for this slot value
            resolved_vals[slot_name] = None
    return resolved_vals
于 2018-08-03T18:34:12.590 回答