解析 python 中的 Alexa json 响应以获取值名称时出现问题
Problem parsing Alexa json response in python to get value name
我已将以下意图传递给我的处理程序:
"request": {
"type": "IntentRequest",
"requestId": "amzn1.echo-api.request.3af5c8c3-1d1f-4169-8ce8-fde1a99a7c8d",
"timestamp": "2019-04-03T04:08:06Z",
"locale": "en-US",
"intent": {
"name": "get_speeds",
"confirmationStatus": "NONE",
"slots": {
"direction": {
"name": "direction",
"value": "inbound",
"resolutions": {
"resolutionsPerAuthority": [
{
"authority": "amzn1.er-authority.echo-sdk.amzn1.ask.skill.e76bf13b-71ac-4a90-94d4-597aa597ae87.direction",
"status": {
"code": "ER_SUCCESS_MATCH"
},
"values": [
{
"value": {
"name": "inbound",
"id": "a8e6fe5b9e68f30a146cefebaa7edcc3"
}
}
]
}
]
},
"confirmationStatus": "NONE",
"source": "USER"
}
}
},
"dialogState": "COMPLETED"
}
我想提取实际值,而不是话语,例如值名称,在本例中为 "inbound"。我试过这个和各种类似的迭代(打印用于调试):
slots = handler_input.request_envelope.request.intent.slots
resolutions = slots["direction"].resolutions
print(resolutions)
print(resolutions["resolutions_per_authority"])
direction = resolutions["resolutions_per_authority"][0]["values"][0]["value"]["name"]
session_attr = handler_input.attributes_manager.session_attributes
我也对 "resolutionsPerAuthority" 进行了相同的尝试,这是传递的 JSON,但显然不是我程序的结果,因为日志有:
04:08:07
{'resolutions_per_authority': [{'authority': 'amzn1.er-authority.echo-sdk.amzn1.ask.skill.e76bf13b-71ac-4a90-94d4-597aa597ae87.direction',
04:08:07
'status': {'code': 'ER_SUCCESS_MATCH'},
04:08:07
'values': [{'value': {'id': 'a8e6fe5b9e68f30a146cefebaa7edcc3',
04:08:07
'name': 'inbound'}}]}]}
04:08:07
'Resolutions' object is not subscriptable
这是我在所有方法中不断遇到的错误:'Resolutions' 对象不可订阅。有人可以帮助我如何提取规范插槽值吗?我需要为其他几个意图做同样的事情,但我想如果我能让这个工作正常,它将成为其他人的榜样。
好的,我终于找到了一个 python 示例,它使用了相对较新的 SDK 的面向对象版本,就像我一样。该示例是亚马逊 PetMatch 示例的 python 版本。
基于此,以下方法有效:
slots = handler_input.request_envelope.request.intent.slots
direction = slots["direction"].resolutions.resolutions_per_authority[0].values[0].value.name
我仍然希望更好地了解它是如何工作的,但至少它是有效的,并且还可以帮助其他人。我发现 Alexa 示例和文档有很多,但组织得不好,而且 api 一直在变化,所以你发现的一些东西已经过时了。
正如您已经指出的,您的问题是您将解析对象视为可订阅的,并通过
访问它
resolutions.resolutions_per_authority[0].values[0].value
才是正确的获取方式
指出可能会有所帮助,如果有多个匹配项,Alexa 将return按照最可能匹配用户意图的顺序排列分辨率。
此代码片段迭代插槽和 return 一个 python 字典,其中仅包含键以了解它是否已通过验证以及匹配值的 ID:
from ask_sdk_model.slu.entityresolution import StatusCode
@staticmethod
def get_slot_values(filled_slots):
"""Return slot values with additional info."""
slot_values = {}
logger.info("Filled slots: {}".format(filled_slots).replace("\n", "\r"))
for key, slot_item in six.iteritems(filled_slots):
name = slot_item.name
try:
status_code = slot_item.resolutions.resolutions_per_authority[0].status.code
if status_code == StatusCode.ER_SUCCESS_MATCH:
slot_values[name] = {
"synonym": slot_item.value,
"resolved": slot_item.resolutions.resolutions_per_authority[0].values[0].value.__dict__, # to make it JSON serializable
"is_validated": True,
}
elif status_code == StatusCode.ER_SUCCESS_NO_MATCH:
slot_values[name] = {
"synonym": slot_item.value,
"resolved": slot_item.value,
"is_validated": False,
}
else:
pass
except (AttributeError, ValueError, KeyError, IndexError, TypeError) as e:
# for BUILT-IN intents, there are no resolutions, but the value is specified
if slot_item.value is not None and slot_item.value != 'NONE':
slot_values[name] = {
"synonym": slot_item.value,
"resolved": slot_item.value,
"is_validated": True,
}
else:
logger.info("SLOT {} UNRESOLVED".format(name))
slot_values[name] = {
"synonym": slot_item.value,
"resolved": slot_item.value,
"is_validated": False,
}
return slot_values
其中 filled_slots = handler_input.request_envelope.request.intent.slots
我已将以下意图传递给我的处理程序:
"request": {
"type": "IntentRequest",
"requestId": "amzn1.echo-api.request.3af5c8c3-1d1f-4169-8ce8-fde1a99a7c8d",
"timestamp": "2019-04-03T04:08:06Z",
"locale": "en-US",
"intent": {
"name": "get_speeds",
"confirmationStatus": "NONE",
"slots": {
"direction": {
"name": "direction",
"value": "inbound",
"resolutions": {
"resolutionsPerAuthority": [
{
"authority": "amzn1.er-authority.echo-sdk.amzn1.ask.skill.e76bf13b-71ac-4a90-94d4-597aa597ae87.direction",
"status": {
"code": "ER_SUCCESS_MATCH"
},
"values": [
{
"value": {
"name": "inbound",
"id": "a8e6fe5b9e68f30a146cefebaa7edcc3"
}
}
]
}
]
},
"confirmationStatus": "NONE",
"source": "USER"
}
}
},
"dialogState": "COMPLETED"
}
我想提取实际值,而不是话语,例如值名称,在本例中为 "inbound"。我试过这个和各种类似的迭代(打印用于调试):
slots = handler_input.request_envelope.request.intent.slots
resolutions = slots["direction"].resolutions
print(resolutions)
print(resolutions["resolutions_per_authority"])
direction = resolutions["resolutions_per_authority"][0]["values"][0]["value"]["name"]
session_attr = handler_input.attributes_manager.session_attributes
我也对 "resolutionsPerAuthority" 进行了相同的尝试,这是传递的 JSON,但显然不是我程序的结果,因为日志有:
04:08:07
{'resolutions_per_authority': [{'authority': 'amzn1.er-authority.echo-sdk.amzn1.ask.skill.e76bf13b-71ac-4a90-94d4-597aa597ae87.direction',
04:08:07
'status': {'code': 'ER_SUCCESS_MATCH'},
04:08:07
'values': [{'value': {'id': 'a8e6fe5b9e68f30a146cefebaa7edcc3',
04:08:07
'name': 'inbound'}}]}]}
04:08:07
'Resolutions' object is not subscriptable
这是我在所有方法中不断遇到的错误:'Resolutions' 对象不可订阅。有人可以帮助我如何提取规范插槽值吗?我需要为其他几个意图做同样的事情,但我想如果我能让这个工作正常,它将成为其他人的榜样。
好的,我终于找到了一个 python 示例,它使用了相对较新的 SDK 的面向对象版本,就像我一样。该示例是亚马逊 PetMatch 示例的 python 版本。
基于此,以下方法有效:
slots = handler_input.request_envelope.request.intent.slots
direction = slots["direction"].resolutions.resolutions_per_authority[0].values[0].value.name
我仍然希望更好地了解它是如何工作的,但至少它是有效的,并且还可以帮助其他人。我发现 Alexa 示例和文档有很多,但组织得不好,而且 api 一直在变化,所以你发现的一些东西已经过时了。
正如您已经指出的,您的问题是您将解析对象视为可订阅的,并通过
访问它resolutions.resolutions_per_authority[0].values[0].value
才是正确的获取方式
指出可能会有所帮助,如果有多个匹配项,Alexa 将return按照最可能匹配用户意图的顺序排列分辨率。
此代码片段迭代插槽和 return 一个 python 字典,其中仅包含键以了解它是否已通过验证以及匹配值的 ID:
from ask_sdk_model.slu.entityresolution import StatusCode
@staticmethod
def get_slot_values(filled_slots):
"""Return slot values with additional info."""
slot_values = {}
logger.info("Filled slots: {}".format(filled_slots).replace("\n", "\r"))
for key, slot_item in six.iteritems(filled_slots):
name = slot_item.name
try:
status_code = slot_item.resolutions.resolutions_per_authority[0].status.code
if status_code == StatusCode.ER_SUCCESS_MATCH:
slot_values[name] = {
"synonym": slot_item.value,
"resolved": slot_item.resolutions.resolutions_per_authority[0].values[0].value.__dict__, # to make it JSON serializable
"is_validated": True,
}
elif status_code == StatusCode.ER_SUCCESS_NO_MATCH:
slot_values[name] = {
"synonym": slot_item.value,
"resolved": slot_item.value,
"is_validated": False,
}
else:
pass
except (AttributeError, ValueError, KeyError, IndexError, TypeError) as e:
# for BUILT-IN intents, there are no resolutions, but the value is specified
if slot_item.value is not None and slot_item.value != 'NONE':
slot_values[name] = {
"synonym": slot_item.value,
"resolved": slot_item.value,
"is_validated": True,
}
else:
logger.info("SLOT {} UNRESOLVED".format(name))
slot_values[name] = {
"synonym": slot_item.value,
"resolved": slot_item.value,
"is_validated": False,
}
return slot_values
其中 filled_slots = handler_input.request_envelope.request.intent.slots