我正在使用使用 AIML 的 Python 启动一个项目,当我运行脚本时它给了我一个“未找到匹配”错误。这是 Python 代码:
import aiml
kernel = aiml.Kernel()
kernel.learn("bot.aiml")
while True:
print kernel.respond(raw_input("\n>>"))
只是一个简单的 AIML 内核。它有什么问题吗?
如果你有兴趣,我有一个更好的 python 脚本
import aiml
import sys <br>
brainLoaded = False
forceReload = False
while not brainLoaded:
if forceReload or (len(sys.argv) >= 2 and sys.argv[1] == "reload"):
kern.bootstrap(learnFiles="Database.xml", commands="load aiml b")
brainLoaded = True
kern.saveBrain("Cache.brn")
else:
# Attempt to load the brain file. If it fails, fall back on the Reload
try:
# It is our cache file.
kern.bootstrap(brainFile = "Cache.brn")
brainLoaded = True
except:
forceReload = True
# Enter the main input/output loop.
print "Enter your message for the chatbot"
while(True):
print kern.respond(raw_input("> "))
注意:您需要创建一个文件夹 Database 来放置您的 AIML 文件和一个文件 Database.xml
您必须将.aiml中的句子以大写字母写到<pattern>
标签中。但是你可以用小写字母和大写字母输入。否则你会得到这样的错误。例如:
<category>
<pattern>WHAT IS YOUR NAME ?</pattern>
<template>My name is robot.</template>
</category
尝试删除代码中的“打印”语句
import aiml
kernel = aiml.Kernel()
kernel.learn("bot.aiml")
while True:
kernel.respond(raw_input("\n>>"))
好吧,如果有人在 Python 3 中需要它,应该做的只是改变print函数的语法。在 python2 中,它接受不带括号的函数,但在 python3 中,必须使用它。
另一个观察结果是raw_input()函数在 python3中已重命名为input() 。
所以解决这个语法错误的方法很简单:
while True:
print(kernel.respond(input("\n>>")))
我们在print函数中添加了括号,包括要显示的内容,并开始使用input()函数,因为在 python3中没有更多的raw_input()了。
出现“未找到输入匹配”警告是因为“bot.aiml”没有与您的输入匹配的输出。尝试包含如下默认响应:
<category>
<pattern>*</pattern>
<template>
Sorry. I didn't quite get that.
</template>
</category>