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

1 篇文章 0 订阅
1 篇文章 0 订阅

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.运行结果

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值