dedtech.info

Information about computer technology.

Create A Chatbot With Python

This blog post will explain how to create a simple chatbot with Python. A chatbot is a program that mimics a person. This is not one of those self learning ones. It is rule based because it uses hard coded responses. The response from the program depends on what input is entered. First, the regular expressions library has to be imported.

import re

The variable keywords holds lists that have the intent as the first element and the rest as keywords. The program will search a tokenized sentence for keywords that are in one of those keywords lists. The intent of the sentence will determine the reply from the program.

keywords = [['greeting', 'hello', 'hi', 'hey'],['age', 'age','old']]

There will be an infinite loop that will take input and process it. If a user types exit, the main loop is terminated. The variable tokens holds the input that is processed into tokens. The punctuation is stripped, converted to lowercase, and split into a list of tokens using a space as a delimiter. The variable action will hold the result of the query function, which extracts a keyword from the list of tokens. The variable answer will hold the result of the response function, which takes the value of the variable action to determine what reply is printed out.

while True:
    sentence = input("QUERY>>>")
    if sentence == "exit":
        break
    tokens = re.sub(r'[^\w\s]', '', sentence).lower().split(' ')
    action = query(tokens)
    answer = response(action)
    print(answer)

There needs to be a function that takes the tokenized sentence as input and searches for a certain keyword within it. There is a loop that traverses the keywords array, each keyword list is processed. Whenever the ismember function is used, everything in the keyword list is used besides the first element. The query function returns the first element in a keywords list if any elements in the tokenized sentence is found in a keywords list.

def query(tokens):
    for keyword in keywords:
        if ismember(tokens,keyword[1:]):
            return keyword[0]
    return "unknown"

There needs to be a function defined to search for keywords in a tokenized sentence.

def ismember(tokens,keywords):
    for token in tokens:
        if token in keywords:
            return True
    return False

There needs to be a function defined that determines the reply to a keyword that is found in a tokenized sentence.

def response(action):
    if action == "greeting":
        return "Hello, how's it going?"
    if action == "age":
        return "A few days old."
    if action == "unknown":
        return "That makes no sense to me."

This is what the whole code looks like.

import re

keywords = [['greeting', 'hello', 'hi', 'hey'],['age', 'age','old']]

def ismember(tokens,keywords):
    for token in tokens:
        if token in keywords:
            return True
    return False

def response(action):
    if action == "greeting":
        return "Hello, how's it going?"
    if action == "age":
        return "A few days old."
    if action == "unknown":
        return "That makes no sense to me."

def query(tokens):
    for keyword in keywords:
        if ismember(tokens,keyword[1:]):
            return keyword[0]
    return "unknown"

while True:
    sentence = input("QUERY>>>")
    if sentence == "exit":
        break
    tokens = re.sub(r'[^\w\s]', '', sentence).lower().split(' ')
    action = query(tokens)
    answer = response(action)
    print(answer)

Leave a Reply

Your email address will not be published. Required fields are marked *