python手动构建自己的聊天机器人

1.需要安装PythonIDE去官网下载吧,然后安装好,我的是Python3.7

2.安装好aiml:pip install aiml

3.存好所需文件,主要是aiml的xml文件

tuling.xml:用于存放语料

<aiml version="1.0.1" encoding="UTF-8">
	<category>
		<pattern>
			你好
		</pattern>
		<template>
			(*´▽`)ノノ你好呀!			
		</template>
	</category>
	<category>
		<pattern>
			你是谁
		</pattern>
		<template>
			(*´▽`)我不告诉你哈哈哈			
		</template>
	</category>
	<category>
		<pattern>
			你叫啥名字
		</pattern>
		<template>
			我叫大可爱!!嘻嘻嘻!!			
		</template>
	</category>
</aiml>

4.写py程序  文件程序

start_chat.py:

# -*- coding:utf-8 -*- #
import aiml
import os

mybot_path = './mybot'
#语料库所在工作目录
os.chdir(mybot_path)
print(mybot_path)
mybot = aiml.Kernel()
mybot.learn("tuling.xml")
mybot.respond('load aiml c')
i=1
while i>0:
    print(mybot.respond(input("Enter your message >>")))
   

也可以将mybot.learn("tuling.xml")变为加载预料的xml文件

mybot.learn("std-startup.xml")

这样一来,你的std-startup.xml文件用于加载预料文件

<aiml version="1.0.1" encoding="UTF-8">
	<category>
		<pattern>
		load aiml c
		</pattern>
		<template>
			<learn>basic_chat.aiml</learn>
			<learn>tuling.aiml</learn>
		</template>
	</category>
</aiml>


这样的话你就去关心怎么写语料库文件basic_chat.aiml,以及tuling.aiml了。

5.运行结果

猜你喜欢

转载自blog.csdn.net/qq_32662795/article/details/83625791