python3.8使用aiml总结

本文介绍了一个基于AIML的聊天机器人实现案例,包括主要文件结构、对话流程及关键配置。展示了如何通过不同标签如pattern、template等实现对话逻辑,并通过自定义脚本使机器人具备学习新对话的能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

model_aiml--aiml对话执行主文件

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


def get_module_dir(name):
    path = getattr(sys.modules[name], '__file__', None)
    if not path:
        raise AttributeError('module %s has not attribute __file__' % name)
    return os.path.dirname(os.path.abspath(path))


# alice_path = get_module_dir('aiml') + '/botdata/alice'
alice_path = '/Users/admin/Desktop/Study_aiml'
# 切换到.aiml所在工作目录
os.chdir(alice_path)
alice = aiml.Kernel()
# 通过std-startup.xml启动aiml
alice.learn("std-startup.xml")
# aiml文件有修改时可以通过load aiml b(在xml中pattern配置)进行修改
alice.respond('LOAD AIML B')

while True:
    # 对话接入位置
    print(alice.respond(input("Enter your message >> ")))
std-startup.xml--固定写法,可配置多个aiml
<aiml version="1.0.1" encoding="UTF-8">
  <!-- std-startup.xml -->

  <!-- Category是一个自动的AIML单元 -->
  <category>

    <!-- Pattern用来匹配用户输入 -->
    <!-- 如果用户输入 "LOAD AIML B" -->
    <pattern>LOAD AIML B</pattern>

    <!-- Template是模式的响应 -->
    <!-- 这里学习一个aiml文件 -->
    <template>
      <learn>basic_chat.aiml</learn>
      <learn>stude.aiml</learn>
      <!-- 你可以在这里添加更多的aiml文件 -->
      <!--<learn>more_aiml.aiml</learn>-->
    </template>

  </category>

</aiml>

basic_chat.aiml--机器人对话内容储存文件

<aiml version="1.0.1" encoding="UTF-8">
<!-- basic_chat.aiml -->


  <category>
    <pattern>HELLO</pattern>
    <template>
      Well, hello!
    </template>
  </category>

<!--that使用方法,在于承上启下,当前回复内容查找aiml中所有that,有则可以根据输入内容进行pattern匹配,无则进行新一轮问答-->
    <category>
        <pattern>你是男生吗?</pattern>
        <template>
          先告诉我你是么?
        </template>
    </category>
    <category>
        <pattern>是</pattern>
        <that>先告诉我你是么?</that>
        <template>
          我也是
        </template>
    </category>
    <category>
        <pattern>不是</pattern>
        <that>先告诉我你是么?</that>
        <template>
          我也不是
        </template>
    </category>

<!--srai使用方法:srai会根据整句去查找整个aiml中的pattern,匹配到后template返回-->
    <category>
        <pattern>张的性别是</pattern>
        <template>
          男
        </template>
    </category>
    <category>
        <pattern>李的性别是</pattern>
        <template>
          女
        </template>
    </category>
    <category>
        <pattern>你知道*的性别是*</pattern>
        <template>
          <srai><star index="1"/>的性别是</srai>
        </template>
    </category>

<!--random+li标签随机选取一个进行回复-->
    <category>
        <pattern>你好</pattern>
        <template>
            <random>
                <li>你好1</li>
                <li>你好2</li>
                <li>你好3</li>
                <li>你好4</li>
                <li>你好5</li>
                <li>你好6</li>
            </random>
        </template>
    </category>

<!--set,get标签get根据name获取set的值-->
    <category>
        <pattern>我是*</pattern>
        <template>
          <set name = "name1"> <star/></set>你好
        </template>
    </category>
    <category>
        <pattern>我的姓名是*</pattern>
        <template>
          你的姓名是<get name = "name1"/>
        </template>
    </category>

<!--think标签,记录变量值不返回给用户-->
    <category>
        <pattern>think我是*</pattern>
        <template>
          <think><set name = "name"> <star/></set></think>你好
        </template>
    </category>
    <category>
        <pattern>我的姓名是*</pattern>
        <template>
          你的姓名是<get name = "name"/>
        </template>
    </category>

<!--condition使用方法,根据输入信息返回不同的结果-->
    <category>
        <pattern>我*</pattern>
        <template>
            <think>
            <set name = "state"><star index = "1"/></set>
            </think>
            <condition name = "state" value = "开心">
                我也开心
             </condition>

             <condition name = "state" value = "不开心">
                我也不开心
             </condition>
        </template>
    </category>
  <category>
    <pattern>WHAT ARE YOU</pattern>
    <template>
      I'm a bot, silly!
    </template>
  </category>

</aiml>

 

stude.aiml --机器人自我学习

<aiml version="1.0.1" encoding="UTF-8">
  <!-- std-startup.xml -->

  <!-- Category是一个自动的AIML单元 -->
  <category>

    <!-- Pattern用来匹配用户输入 -->
    <!-- 如果用户输入 "LOAD AIML B" -->
    <pattern>* 答案是 *</pattern>

    <!-- Template是模式的响应 -->
    <!-- 这里学习一个aiml文件 -->
    <template>
    <!--system可以调用python命令,从而往aiml中写入对话-->
      <system>python3 learn.py '<star index="1"/>' '<star index="2"/>'</system>
      <learn>basic_chat.aiml</learn>
      好的我学会了
      <!-- 你可以在这里添加更多的aiml文件 -->
      <!--<learn>more_aiml.aiml</learn>-->
    </template>

  </category>

</aiml>

learn.py--写入aiml方法

# -*- coding: utf-8 -*-
import sys
# print(sys.argv)
template = """
    <category>
        <pattern>{}</pattern>
        <template>
          {}
        </template>
    </category>"""
file = open("basic_chat.aiml","rb+")
content = file.read()
# seek定位至aiml中最后一行之前,删除</aiml>
file.seek(-7,1)
file.truncate()
file.close()
file1 = open("basic_chat.aiml","a")
file1.write(template.format(sys.argv[1],sys.argv[2])+"\n"+"</aiml>")
file1.close()
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值