AIML 是一种基于 XML 标准的声明式语言,它规定了可以在机器人中使用的编程构想和数据结构。我们基于人工智能标记语言(AIML)来定义聊天机器人的模式和回复,构建模式匹配聊天机器人。
展示如何在聊天机器人中创建和加载 AIML 文件并使用它生成回复:
1.greeting_step1.aiml:
<?xml version="1.0" encoding="UTF-8"?>
<aiml version="1.0.1">
<!--在 AIML 1.0 中,所有模式必须全大写-->
<category>
<pattern>HELLO ROSA </pattern>
<template>Hi there!</template>
</category>
<category>
<pattern>HELLO TROLL </pattern>
<template>Good one, human.</template>
</category>
</aiml>
2.greeting_step2.aiml:
<?xml version="1.0" encoding="UTF-8"?>
<aiml version="1.0.1">
<!--使用模板中的<srai>标签和星号(*),将多个模式链接到同一个响应模板,以解-->
<!--决大部分匹配遗漏的问题。我们将这些模式视为“Hello”的同义词,即使它们可能是拼写错误或完全不同的词-->
<!--<star>标签用于匹配<pattern>标签中的通配符*字符-->
<category>
<pattern>HELO * </pattern>
<template><srai>HELLO <star/></srai></template>
</category>
<category>
<pattern>HI * </pattern>
<template><srai>HELLO <star/></srai></template>
</category>
<category>
<pattern>HIYA * </pattern>
<template><srai>HELLO <star/></srai></template>
</category>
<category>
<pattern>HYA * </pattern>
<template><srai>HELLO <star/></srai></template>
</category>
<category>
<pattern>HY * </pattern>
<template><srai>HELLO <star/></srai></template>
</category>
<category>
<pattern>HEY * </pattern>
<template><srai>HELLO <star/> </srai></template>
</category>
<category>
<pattern>WHATS UP * </pattern>
<template><srai>HELLO <star/></srai></template>
</category>
<category>
<pattern>WHAT IS UP * </pattern>
<template><srai>HELLO <star/></srai></template>
</category>
</aiml>
3.greeting_step3.aiml:
<?xml version="1.0" encoding="UTF-8"?>
<aiml version="1.0.1">
<!--
在 AIML 2.0 中,可以使用方括号列表来指定选择随机响应模板。而在 AIML 1.0 中,使用<li>
标签来执行此操作。<li>标签仅在<condition>或<random>标签内部使用。可以使用
<random>标签来帮助机器人在回复问候时显得更有创意一点儿,看起来不那么机械
-->
<category>
<pattern>HELLO ROSA </pattern>
<template>
<random>
<li>Hi Human!</li>
<li>Hello friend</li>
<li>Hi pal</li>
<li>Hi!</li>
<li>Hello!</li>
<li>Hello to you too!</li>
<li>Greetings Earthling ;)</li>
<li>Hey you :)</li>
<li>Hey you!</li>
</random>
</template>
</category>
<category>
<pattern>HELLO TROLL </pattern>
<template>
<random>
<li>Good one, Human.</li>
<li>Good one.</li>
<li>Nice one, Human.</li>
<li>Nice one.</li>
<li>Clever.</li>
<li>:)</li>
</random>
</template>
</category>
</aiml>
4.Python_AIML_解释器.py:
import aiml_bot
bot = aiml_bot.Bot(learn='xxx\\greeting_step1.aiml')
# AIML 规范巧妙地忽略了标点符号和大小写
# 但是 AIML 1.0 规范只规范化模式的词结尾的标点符号,而不是词之间和词内部;
# 词之间的空白符,而不是词内部
print("Hello Rosa,:", bot.respond("Hello Rosa,"))
print("hello troll!!!:", bot.respond("hello troll!!!"))
print("hello ! troll!!!:", bot.respond("hello ! troll!!!"))
print("hello !!!troll!!!:", bot.respond("hello !!!troll!!!"))
print("hello **troll:", bot.respond("hello **troll"))
# 它无法处理同义词、拼写错误、带连字符的词或复合词
print("Helo Rosa:", bot.respond("Helo Rosa"))
print("Hello Ro-sa:", bot.respond("Hello Ro-sa"))
# 加载一个附加的 AIML,机器人就可以识别出“Hello”的几种不同的说法和错误拼写形式
bot.learn('xxx\\greeting_step2.aiml')
print("Hey Rosa:", bot.respond("Hey Rosa"))
print("Hi Rosa:", bot.respond("Hi Rosa"))
print("Helo Rosa:", bot.respond("Helo Rosa"))
print("HY troll!:", bot.respond("HY troll!"))
print("WHAT IS UP troll!:", bot.respond("WHAT IS UP troll!"))
print("hello **troll** !!!:", bot.respond("hello **troll** !!!"))
print("hello !!! troll!!!:", bot.respond("hello !!!troll!!!"))
# 使用<random>标签来帮助机器人在回复问候时显得更有创意一点儿
# 每次匹配模式时,它都会从列表中随机选择一个回复,无法在 aiml_bot 中设置随机种子
bot.learn('xxx\\greeting_step3.aiml')
print("Hey Rosa:", bot.respond("Hey Rosa"))
print("Hey Rosa:", bot.respond("Hey Rosa"))
print("Hey Rosa:", bot.respond("Hey Rosa"))