A Simple Chatbot Using NLTK Chat

When I was building my first Messenger chatbot I look and took ideas from NLTK chat examples. So basically you can learn from this examples before you can power your chatbot with more complex stuff like sentiment analysis, parts of speech tagging, intents, etc.

First you will need to install NLTK (Natural Language Toolkit).

$ pip install nltk

Create a python file

# hugotbot.py
from nltk.chat.util import Chat, reflections
pairs = [
[
r'hi',
['hello', 'kamusta', 'mabuhay',]
],
]
def hugot_bot():
print("Hi how can I help you today?")
chat = Chat(pairs, reflections)
chat.converse()
if __name__ == "__main__":
hugot_bot()

We imported the class Chat which will process the conversation between the user and your chatbot.

We also imported reflections, this is a dictionary that when a value in a regex group matches a key in the dict it will output the value in the response. You will learn more about this later.

[
r'(.*) (hungry|sleepy)',
[
"%1 %2"
]
]
# If above pattern is matched it will answer like this
>I'm hungry
you are hungry
>you are sleepy
I am sleepy
# If you check nltk.chat.util
reflections = {
"i am" : "you are",
"i was" : "you were",
"i" : "you",
"i'm" : "you are",
"i'd" : "you would",
"i've" : "you have",
"i'll" : "you will",
"my" : "your",
"you are" : "I am",
"you were" : "I was",
"you've" : "I have",
"you'll" : "I will",
"your" : "my",
"yours" : "mine",
"you" : "me",
"me" : "you"
}

reflections is optional and you can even pass your own dictionary for this.

tagalog_reflections = {
"ikaw": "ako",
"sa akin": "sa iyo"
}

Next we have the variable pairs which is a list (as per docs list of tuple, but you can use either). The first item in the pair is the pattern which is a string or regular expression. The second item is a list of possible responses, Chat will randomly select a response if a pattern matches the user’s input.

Let’s try it:

$ python hugotbot.py
Hi what's your name?
>My name is mark
None
>hi
kamusta
>HI
kamusta
>hI
hello

Cool so it has an answer for “hi” but when you answered the intro question it returned None. Let’s add a new pair.

pairs = [
[
r"My name is (.*)",
['hello %1',]
],
[
r'hi',
['hello', 'kamusta',]
],
]
# Try it:
$ python hugotbot.py
Hi what's your name?
>My name is Mark
hello Mark

Alright now it greeted me with hello + my name. Regex groups can be displayed back to you according to their order with %1, %2 and so on.

Now this is just a basic chatbot and you can do more just use your creativity and imagination like what if you load pairs from the database
or you try to anticipate all patterns that your chatbot and the user will have. After this maybe you can learn more about natural language processing so you can make your chatbot more conversational when trying to chat with the user.

See the simple hugotbot code in my gist.

Sorry for naming it hugotbot just trying to make it catchy and fun (for filipino readers). Maybe someone can create one with sentiments analysis. =)

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.