为亚马逊的Alexa创造话语。
utter-more的Python项目详细描述
更多
要定制亚马逊的Alexa,你需要一种所谓的技能。在技巧上做点什么,你就有了意图。为了达到目的,你必须说出一句话。当这句话被说出时,意图就跑了因为语言是复杂的,可能有很多不同的方式说同一件事,你可能想让亚历克斯学习所有这些方式此外,您可能有许多语句变量(称为意图槽)。冗长到足以涵盖每一个案例可能会很乏味,所以这就解决了这个问题。
安装软件包
做经典:
pip install utter-more
或者如果你喜欢冒险并使用conda:
conda install -c crumpstrr33 utter-more
创造话语
下面是一些显示其功能的示例。
格式化
目前有两种选择:
- 或语句
(a|b|c|...)
-如果要允许多个可互换的单词,则使用。例如,如果photo
、picture
和painting
在你的话语中是可以互换的,那么在它应该在的地方写(photo|picture|painting)
。单词的数量是任意的,可以使用单个卷曲的关键字,如{intent_slot}
。 - 条件或语句
(a*tag1|b) (c^tag1|d)
-如果希望或语句中短语的外观依赖于另一个短语,则使用这里,a
是主语,c
是从句;只有当它也包含a
时,才出现带有c
的语句。它的另一个功能如下如果有(It*s|They*p) (is^s|are^p) (close^s|far^p)
,is
和close
只在It
也显示时显示,反之,are
和far
只在They
显示时显示这就是你如何用这个函数做条件和
如果你的话语中有一个意图槽是可选的,则使用可选的意图槽。例如,如果你有一个可选副词,你可以写
I {adverb} love it
,或者只写I love it
相反,您可以编写I {{adverb}} love it
来捕获两者。运行代码
现在,随着格式的降低,让我们为话语创建一些模板。例如:
"What is that {{descriptor}} (photo|picture) (of|from)"
以及
"Download the (photo|picture) {{to_file_path}}"
为此,我们运行以下命令:
frompprintimportpprintfromutter_moreimportUtterMoreum=UtterMore("What (is*s|are*p) (that^s|those^p) {{descriptor}} (photo|picture)(^s|s^p) (of|from)","Download the (photo|picture) {{to_file_path}}")um.iter_build_utterances()pprint(um.utterances)
这将显示:
[['What is that {descriptor} photo of','What is that {descriptor} photo from','What is that {descriptor} picture of','What is that {descriptor} picture from','What is that photo of','What is that photo from','What is that picture of','What is that picture from','What are those {descriptor} photos of','What are those {descriptor} photos from','What are those {descriptor} pictures of','What are those {descriptor} pictures from','What are those photos of','What are those photos from','What are those pictures of','What are those pictures from'],['Download the photo {to_file_path}','Download the photo','Download the picture {to_file_path}','Download the picture']]
在这里我们可以很容易地遵循复数的语法规则如果我们想保存话语,以便将它们上传到我们的Alexa技能中,我们只需要:
um.save_for_alexa(PATH_TO_DIRECTORY,FILE_NAME)
在这里,我们将找到CSV文件的正确格式上传
上传话语
- 在进入指定意图的选项卡后,单击页面右上角的“批量编辑”。
- 浏览或拖放以前生成的csv,它将填充文本字段。
- 按“提交”按钮,将填充“话语”字段。
就这样,不需要手动输入成百上千个令人烦恼的相似短语
其他特点
- 您可以在将类设置为这样之后添加话语模板:
fromutter_moreimportUtterMoreum=UtterMore()um.add_utterance_template("What (is*s|are*p) (that^s|those^p) {{descriptor}} (photo|picture)(^s|s^p) (of|from)")um.add_utterance_template("Download the (photo|picture) {{to_file_path}}")um.iter_build_utterances()um.save_for_alexa(PATH_TO_DIRECTORY,FILE_NAME)
这将产生与上面相同的csv
- 继续上面的代码,然后您可以将这些语句通常保存为常规csv或类似这样的文本文件:
# Saves as utterances.txt with new line separatorsum.save_utterances(PATH_TO_DIRECTORY,'utterances','txt')# Saves as utterances.csv as actual comma-separated valuesum.save_utterances(PATH_TO_DIRECTORY,'utterances','csv')
- 话语可以从模板创建,而无需将其添加到uttermore对象中,因此
frompprintimportpprintfromutter_moreimportUtterMoreum=UtterMore()utterances=um.build_utterance("What (is*s|are*p) (that^s|those^p) {{descriptor}} (photo|picture)(^s|s^p) (of|from)")pprint(utterances)
将输出:
[['What is that {descriptor} photo of','What is that {descriptor} photo from','What is that {descriptor} picture of','What is that {descriptor} picture from','What is that photo of','What is that photo from','What is that picture of','What is that picture from','What are those {descriptor} photos of','What are those {descriptor} photos from','What are those {descriptor} pictures of','What are those {descriptor} pictures from','What are those photos of','What are those photos from','What are those pictures of','What are those pictures from']]