AIML based PHP chatbot

藏色散人
Release: 2023-04-08 08:08:01
forward
3249 people have browsed it

chatbot: PHP chatbot based on AIML

This article introduces a small chatbot program implemented in PHP, using knowledge related to PHP operating dom and mysql database, which can be used for learning. The code can be downloaded from https://github.com/kompasim/chatbot.

0. Reminder

This chat robot is written with reference to AIML 2.5 and Program-P. The aiml tags implemented by this chat robot and the standard aiml tags are determined by certain gap, so the aiml corpus you downloaded from the Internet may not work properly. Suitable for UTF-8 encoded languages ​​where word suffixes change according to different tenses. Interested friends can do their own research and study.

1. Introduction

This is an aiml parser written in PHP. It can currently run normally in the PHP5.4 environment.

2. Database configuration

This program uses a MySQL database. You need to pour the chatbot.sql file into your database, and then add it to chatbot/Config.php Modify the relevant database configuration variables in the file.

4. aiml corpus resources

You can directly edit the aiml/chatbot.aiml file or create a new aiml file and then The aiml/chatbot.aiml file introduces it with the include tag.

5. About aiml file

aiml must be placed in the aiml directory.

chatbot.aiml is the entry file, and the aiml tag in it Can contain multiple category tags, a default tag and multiple include tags.

The other aiml files you add must contain a topic tag in the root aiml tag. This topic tag can contain multiple category tags and a default tag (other aiml files here cannot write include tags, include Tags can only appear in the chatbot.aiml file).

6. Test the chatbot

You can open index.php to chat with the robot for testing.

7. Call the chatbot

If you want to call the chatbot in your own application, we can call its api like this api.php?requestType=talk&input=Hello

8. About matching rules

Matching rules after modification:

* ---> (\S+)
# ---> \S+
_ ---> .*
= ---> \S*
Copy after login

9. About AIML tags

The aiml tag of this chat robot is different from the standard aiml tag. I have personalized the tags according to my needs. We can find more tags supported by this chat robot in the AIML.MD file.

10. About the return data of api.php

{
    "status": "success",
    "type": "talk",
    "message": "haha ...",
    "data": {
        "arr1": {
            "name11": "value11",
            "name12": "value12"
        },
        "arr2": {
            "name21": "value21",
            "name22": "value22"
        }
    }
}
Copy after login

When you access api.php?requestType=talk&userInput=haha, you can get the above json data.

<category>
    <pattern>haha</pattern>
    <template>
        <data name="arr1">
            <attr name="name11">value11</attr>
            <attr name="name12">value12</attr>
        </data>
        <data name="arr2">
            <attr name="name21">value21</attr>
            <attr name="name22">value22</attr>
        </data>
        haha ...
    </template>
</category>
Copy after login

11. About userId

When you send a request to api.php in GET mode with the userId parameter, this parameter will be used as the user's unique identifier. If you do not give this parameter, the program will use the user's IP as the unique identifier.

$userId = isset($_REQUEST[&#39;userId&#39;]) ? $_REQUEST[&#39;userId&#39;] : $_SERVER[&#39;REMOTE_ADDR&#39;];
Copy after login

12. About multiple chatbots

If you set multiChatbot in the 'chatbot/Config.php' file to false, all users will share a default chatbot. When you set it to true, each user will have an independent chatbot. These chatbots will use userId as the unique identifier. Each user sets attributes such as name, gender, age, etc. for his or her chatbot. Multiple chatbot functionality is especially useful when you are writing a voice assistant or the like, because users can set names for their own voice assistants.

$user = $this->getUser($this->_unique);
if ($this->_config->multiChatbot){
    $bot = $this->getBot($this->_unique);
} else {
    $bot = $this->getBot("default");
}
Copy after login

13. About userInfo and botInfo

userInfo and botInfo refer to some attributes of users and chatbots, such as name, age, gender, etc. Users can change these attributes. Of course, this is achieved through some tags when we write the aiml corpus. We can learn set, get, del, user, bot and other related tags from AIML.MD.

<category>
    <pattern>my name is *</pattern>
    <template>
        ok , your name is
        <star/>
        <set type="user" name="name">
            <star/>
        </set>
    </template>
</category>
<category>
    <pattern>what is my name</pattern>
    <template>
        oh , your name is
        <get type="user" name="name"/>
        , i remembered it last time ...
    </template>
</category>
Copy after login

14. About the database

log is a table that stores logs.

property is a table that stores user and bot related attributes, which is equivalent to our The head of the chatbot helps us remember some attributes. Tags such as set, get, del, user, bot are used to operate this table. The

data table is used to store user input, robot replies, input, that, topic and other tags will operate Parser:: $_data array, each time the program starts running, the program will load data from this table into this array, and then store it in this table when the program ends. The only identifier of the table is userId.

Source code

https://github.com/kompasim/chatbot

The above is the detailed content of AIML based PHP chatbot. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template