我试图正确理解插槽如何以编程方式工作。在编写任何代码之前,我会通过查看适用于python的alexa sdk 的示例来尝试很好地理解它。
具体来说,我试图理解ColorPicker示例中插槽中的基础知识(我正确理解了hello_world,并通过自己添加一些东西进行了一些编码。工作正常)。
我很难理解如何访问这些插槽值(因为我看到它是通过两种不同的方式完成的)。
def whats_my_color_handler(handler_input):
"""Check if a favorite color has already been recorded in
session attributes. If yes, provide the color to the user.
If not, ask for favorite color.
"""
# type: (HandlerInput) -> Response
if color_slot_key in handler_input.attributes_manager.session_attributes:
fav_color = handler_input.attributes_manager.session_attributes[
color_slot_key]
speech = "Your favorite color is {}. Goodbye!!".format(fav_color)
handler_input.response_builder.set_should_end_session(True)
else:
speech = "I don't think I know your favorite color. " + help_text
handler_input.response_builder.ask(help_text)
handler_input.response_builder.speak(speech) …Run Code Online (Sandbox Code Playgroud) 我正在尝试将 Watson Assistant 的输出放入一个变量中。因此,据我搜索,我需要获取 json 的“输出”和“文本”部分(起初它是一个 dict,但后来我们将其解析为 json)。但我似乎无法理解:
我已经在这两个问题中搜索过:这是一个给 watson 这个一个用于解析 json
代码非常简单:访问我的机器人,并输入“行程”。我已经取出了 api 和工作区,但我有它们(显然)。
if __name__ == '__main__':
assistant = watson_developer_cloud.AssistantV1(
iam_apikey='{YOUR API HERE}',
version='2018-09-20',
url='https://gateway-syd.watsonplatform.net/assistant/api'
)
response = assistant.message(
workspace_id='{YOUR WORKSPACE HERE}',
input={
'text': 'trips'
}
).get_result()
fullResponse=json.dumps(response, indent=2)
print(fullResponse)
print("testing to print the output: ")
respuesta=json.dumps(response, indent=2)
#print(respuesta['output'][0]['text'])
print(respuesta['output']['text'])
Run Code Online (Sandbox Code Playgroud)
和输出:
Traceback (most recent call last):
"intents": [
File "C:/Users/.PyCharmCE2018.3/config/scratches/pruebaMain.py", line 105, in <module>
{
print(respuesta['output']['text'])
"intent": "trips",
TypeError: string indices must be integers
"confidence": 1
} …Run Code Online (Sandbox Code Playgroud)