Skip to content

majidkh/MajBot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Introduction

MajBot is a Java-Based chat bot which responses based on the messages that it receives. It uses an external data file (XML file) to make it easier for users to extend its features and its domain. MajBot uses dynamic features to retrieve weather information from Yahoo Weather API. It allows multiple messages per state to make it possible to display random message for similar questions to make the bot more interactive. Each state can expect multiple keywords or no keywords which returns user to the default state. It also supports regular expressions for better user input processing. MajBot can learn new keywords by asking questions from user and provide their answer for them in the future questions.

Chat Bot

License: GPL

At a glance

It is easy to customize the MajBot by configuring data.xml file, this file contains different states and keywords inside each state, once a keyword is matched, user will be transferred to the target state, MajBot stats from State “0” by default, here is a simple configuration file:
<?xml version="1.0" encoding="UTF-8"?>
<Config>
    <State id="0">
        <message>Hello, How are you?</message>
        <keywords>                    <keyword target="1">happy</keyword>
        </keywords>
    </State>

    <State id="1">       <message>wow, you are happy!</message>       <message>glad to hear that!</message>    </State>    <InvalidMessages>         <message>Huh, I didn't understand?</message>         <message>What do you mean?</message>     </InvalidMessages> </Config>

In the above example, If user enters happy keyword, bot moves to State "1" and display one of  state "1" messages randomly, If user enters any other strings, one of the invalid messages will appear.

Keyword tag contains multiple attributes to make it possible to customize the bot according to the requirements.

Navigation

To navigate user between different states, keyword tag is being used with target attribute, the matching keyword will transfer user to its target, it is possible to use multiple keywords and multiple messages for each state, in this case everytime a random message appears. It is easy to use multiple keywords by separating the keywords by comma, if any of these keywords matches, user will be transferred to the target state:
<State id="16">
<message>Hello, how are you today?</message>
<message>How are you feeling?</message>
<message>How are you doing?</message>
<keyword target="11">happy, very happy</keyword>
<keyword target="13">not happy,not good,not fine,sad,upset</keyword>
<keyword target="23">fine, good, thanks</keyword>
<keyword target="14">yourself</keyword>
</State>
 

Using the weather feature

To use weather feature, keyword tag should contain two attributes, className and arg, className should set to Weather and arg should set to "today", "tomorrow" or "dayaftertomorrow" keywords. Here is an example to get today's weather:

<keyword className="Weather" arg="today">weather</keyword>

Result:

You: how is the weather
Bot: I think today is Fog

Catch all

To transfer user to a custom state when user enters an unexpected keyword, "*" symbol can be used:
<State id="11">
<message>That's great, why you are happy?</message>
<keywords>
<keyword target="21">lottery</keyword>
<keyword target="22">*</keyword>
</keywords>
</State>

Regular Expressions

Using Regular Expression makes it possible to process user's input and extract data from their inputs; MajBot stores these data into a dictionary and it is possible to use these dynamic data in different states, following example demonstrates how to ask a user's name and response with a greeting message with user's name in the greeting:
<?xml version="1.0" encoding="UTF-8"?>
<Config>
<State id="0">
<message>Hello, My name is MajBot, what is your name?</message>
<keywords>
<keyword target="2" variable="name">([a-zA-Z ]+)</keyword>
</keywords>
</State>
<State id="16">
<message>Hello [name], nice to see you!</message>
</State>
</Config>
Result:
Bot: Hello, My name is MajBot, what is your name?
You: Majid 
Bot: Hello majid, nice to see you!
In the above example "([a-zA-Z )+)" regex accepts any characters betwen a to z  (case insensitive) including spaces and store the matching word to name variable, then by using [name] in the message, user's name will be replaced with this tag. (For more information about Regex syntax refer to Java Regex Documentation).

Adding priorities to keywords

By default the longest matching keyword will be the best match, however sometimes we need to give extra points to some of keywords so in case both of the keywords matches, the keywords with higher points selected for the best match, in this case we can use points attribute:
<Config>
<State id="0">
<message>Hello, My name is MajBot, what is your name?</message>
<keywords>
<keyword target="16" variable="name" points="2">.*my name is ([a-zA-z]+).*</keyword>
<keyword target="16" variable="name">([a-zA-Z ]+)</keyword>
</keywords>
</State>
<State id="16">
<message>Hello [name], nice to see you!</message>
</State>
</Config>
Result:
Bot: Hello, My name is MajBot, what is your name?
You: my name is Majid
Bot: Hello majid, nice to see you!
In the above example we are adding 2 points for the first keyword so if user enters "my name is Majid", it only gets the name instead of returning "My name is Majid" as name.

Learning new words

It is possible to ask the user about unknown keywords and add dynamic keywords and states for the new keyword, if in the future user enters same keyword, his answer will be replied back to the user. For this purpose we can use variable tag to get user's keyword first and then asking a question about that subject and storing the subject result by using learn attribute:
 <State id="1">
<message>What do you want to talk about?</message>
<keyword variable="subject" target="27">([a-zA-z]+)</keyword>
</State>
<State id="27">
<message>What is [subject]?</message>
<keyword target="43" variable="result" learn="subject">(.*)</keyword>
</State>
Result:
Bot: What do you want to talk about?
You: football 
Bot: What is football?
You: it is a good game 
Bot: I see...
Bot: What do you want to talk about?
You: football 
Bot: it is a good game
As it is shown in the above example, bot learns about football keyword and then send the response back to the user when user sends same keyword again.

How to run MajBot

MajBot includes 7 classes and one data.xml file. It also uses Apache HttpdClient libraries to connect to Yahoo Weather API. All of the classes are compiled into MajBot.jar file and you can run the jar file using provided run.sh file which contains:
java -cp MajBot.jar bot.Main
To run MajBot you can simply type:
sh run.sh
If you are using windows, you can run run.bat file instead.

After starting the bot, user can enter a message and start chating with MajBot.

Enjoy!

http://www.majidkhosravi.com/chat-bot/

Releases

No releases published

Packages

No packages published

Languages