Python Forum

Full Version: How to make a bot that learns to talk
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
In this tutorial I go through making a bot that learns to talk. How does it work? This bot learns to talk the same way a baby does. The bot automatically remember anything you say and adds it to its vocabulary. Its vocabulary is actually a dictionary. Using a dictionary makes it realistically talk after it's "grown up". The bot starts off like a baby. As it talks with people, it slowly "grows up". back to the dictionary. The reason it's a dictionary is because with a dictionary we can store a reference and reply. The bot starts with by saying "Hello". Then the user says something. What the bot says will serve as the reference. What the user says next will server as the reply. This makes the bot just like a baby as it will adapt to its environment. If it talks to a calm person, the bot will be chill, otherwise the bot may turn out as a delinquent. Anyways, let's get started.

Step 1. Make a txt document and call it "Vocabulary.txt"

Step 2. Put a few phrases in the text document like so:
{"No" : "Yes", "Yes" : "No", 'Hello' : 'Hi', 'How are you' : 'good', 'how r u' : 'bad'}

You can put as many as you like

Step 3. Now let's first load layout our code
import random, pickle

def save(data):


def load():


def main():


if __name__ == '__main__':
    main()
Step 4. The first function to do is going to be the load function
def load():
    with open('Vocabulary.txt', 'rb') as f:
        return pickle.load(f)
When using with open('Vocabulary.txt') as f:, the file is opened as f and we do not have to close it. So we just read the data and return it

Step 5. The next easiest one is the save function, which will be very similar to the load function
def save(data):
    with open('Vocabulary.txt', 'wb') as f:
        pickle.dump(data, f)
We used the same method from load but this time we wrote data.

Step 6. The main function. The first thing we should do is make sure we can load the data properly.
def main():
vocab = load()
print(vocab)

Step 7. Great! let's start on the main function.
def main():
    vocab = load()
    computer = 'Com. - Hello'
    print(computer)
    reply = input('User - ')
This is the main functionality. For starting off the computer says "Hello", then we get the users input.
Step 8. Now we're introducing two more functions that are not yet defined
def main():
    vocab = load()
    computer = 'Com. - Hello'
    print(computer)
    reply = input('User - ')
    while True:
        computer = 'Com. - ' + makeReply(reply, vocab)
        print(computer)
        saveReply(computer, reply, vocab)
        reply = input('User - ')
We have saveReply and makeReply. Here is the thought process here. In the beginning of each loop. The past conversation will be saved in our vocabulary. Then makeReplay will use the users reply to what we previously said and make a reply for us.

Step 9. First we will do the saveReply function.
def saveReply(computer, reply, vocab):
    computer = computer.replace('Com. - ', '')
    vocab.update({reply : computer})
    save(vocab)
computer respresents what the computer said. reply is how the user replied. By saving it {computer : reply}, teh bot will reply just as a "educated" human would. The of course we save it to our "vocabulary.txt" file.

Step 10. The final step is the makeReply function
def makeReply(reply, vocab):
    for word in vocab:
        if word.lower() == reply.lower():
            return vocab[word]
    num = random.randint(0, len(vocab) - 1)
    count = 0
    for word in vocab:
        if count = num:
            return word
        count += 1
Instead of checking if word is in the dictionary, we can be more accurate by running a for loop. This enables the bot to lower both words before comparison. The lower function, belongs to the string class and makes all the letter of a string lowercase. If we find a match, we return that keys definition, or reply in our case. If we don't we just say something random.

This could definitely be a little ore sophisticated, especially the search part. Using the same algorithm as a search engine would allow the replies from the bot to be better. Anyways, the full code ends up like this -
import random, pickle

def save(data):
    with open('Vocabulary.txt', 'wb') as f:
        pickle.dump(data, f)

def load():
    with open('Vocabulary.txt', 'rb') as f:
        return pickle.load(f)

def makeReply(reply, vocab):
    for word in vocab:
        if word.lower() == reply.lower():
            return vocab[word]
    num = random.randint(0, len(vocab) - 1)
    count = 0
    for word in vocab:
        if count = num:
            return word
        count += 1

def saveReply(computer, reply, vocab):
    computer = computer.replace('Com. - ', '')
    vocab.update({reply : computer})
    save(vocab)

def main():
    vocab = load()
    computer = 'Com. - Hello'
    print(computer)
    reply = input('User - ')
    while True:
        computer = 'Com. - ' + makeReply(reply, vocab)
        print(computer)
        saveReply(computer, reply, vocab)
        reply = input('User - ')

if __name__ == '__main__':
    main()
Sorry if I made any mistakes in writing this tutorial. Thanks for reading, and have a nice day!

Forgot to mention, that you will have to save a dictionary to the vocabulary.txt file. Straight up writing it, causes it to be interpreted as string. You can just run save with a dictionary.
You can use the Internet of Things and you should learn Python's object-oriented topics thoroughly if you have questions you can ask at your service....