python模拟机器人对话,全匹配

该博客介绍了如何使用python的python-aiml库搭建一个全匹配的机器人对话系统。首先,通过pip安装python-aiml,接着将必要的 alice 文件复制到工作目录,然后提供源码包括 testb.py, std-startup.xml 和 basic_chat.aiml。运行这些文件后,即可得到对话结果。" 82720836,5792330,C++面试:解析指针数组与数组指针的差异,"['C++', '数据类型', '指针', '数组', '面试']

1、python3安装pip install pip install python-aiml

2、将alice文件复制到工作区

在这里插入图片描述
在这里插入图片描述

3、源码如下:

testb.py


    import aiml

# Create the kernel and learn AIML files
kernel = aiml.Kernel()
kernel.learn("std-startup.xml")
kernel.respond("load aiml b")

# Press CTRL-C to break this loop
while True:
    print(kernel.respond(input("Enter your message >> ")))

std-startup.xml


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

    <!-- Category is an atomic AIML unit -->
    <category>

        <!-- Pattern to match in user input -->
        <!-- If user enters "LOAD AIML B" -->
        <pattern>LOAD AIML B</pattern>

        <!-- Template is the response to the pattern -->
        <!-- This learn an aiml file -->
        <template>
            <learn>basic_chat.aiml</learn>
            <!-- You can add more aiml files here -->
            <!--<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>
    
    <category>
        <pattern>WHAT ARE YOU</pattern>
        <template>
            I'm a bot, silly!
        </template>
    </category>

    <category>
        <pattern>你好</pattern>
        <template>
            I'm a bot, silly!你好
        </template>
    </category>

    <category>
        <pattern>2</pattern>
        <template>
            111111I'm a bot, silly!你好
        </template>
    </category>
    
</aiml>


运行结果:
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值