Skip to main content
GameDev.net gamedev.net
🔒 Locked

Chatbot in 18 lines of code (Python) help.

Started by Tutorial Doctor Dec 2, 2014 at 5:14 PM 9 replies 108k views
Original Post
Tutorial Doctor
Tutorial Doctor
Found a little code that is supposed to be a simple chatbot AI. It is random, except that the only time it randomly chooses from the list is when the program is run again (not while True). The random choice I chose when I first ran the program is the one it will keep choosing until I run the program again. What would be the fix?

import random

greetings = ['hola', 'hello', 'hi', 'Hi', 'hey!','hey']
random_greeting = random.choice(greetings)

question = ['How are you?','How are you doing?']
responses = ['Okay',"I'm fine"]
random_response = random.choice(responses)


while True:
	userInput = raw_input(">>> ")
	if userInput in greetings:
		print(random_greeting)
	elif userInput in question:
		print(random_response)
	else:
		print("I did not understand what you said")
They call me the Tutorial Doctor.
alvaro
alvaro

I tried it and it seems to work fine:


> python hello.py
>>> hola
hi
>>> How are you?
I'm fine
>>> Great!
I did not understand what you said
ferrous
ferrous

I'm not a python expert, but wouldn't you need to have the random.choice bits of code in the while bit?

alvaro
alvaro

I'm not a python expert, but wouldn't you need to have the random.choice bits of code in the while bit?

Oh, right! I now understand what the OP was talking about.


import random

greetings = ['hola', 'hello', 'hi', 'Hi', 'hey!','hey']

question = ['How are you?','How are you doing?']
responses = ['Okay',"I'm fine"]

while True:
        userInput = raw_input(">>> ")
        if userInput in greetings:
                random_greeting = random.choice(greetings)
                print(random_greeting)
        elif userInput in question:
                random_response = random.choice(responses)
                print(random_response)
        else:
                print("I did not understand what you said")
Tutorial Doctor
Tutorial Doctor
Ahh! Okay, thanks!

I am trying to see how smart I can make it. Hehe.

The code comments said that in order to extend it, you just add more elif statements (for anyone interested).
They call me the Tutorial Doctor.
Tutorial Doctor
Tutorial Doctor

On a related note: http://xkcd.com/221/

random_number.png


Wow. I had watched a video on YouTube that was a tutorial on how to download and parse all comics from this site. Haha. Never hear about it before then.

I think I am going to be able to append lists to other lists to increase vocabulary.

They call me the Tutorial Doctor.
Tutorial Doctor
Tutorial Doctor
Just posting an idea about how I would add to memory (not working right now, I think I can figure this one out though.)


import random
from speech import *

CONVERSING = True

memory = []
greetings = ['hola', 'hello', 'hi','hey!','Hello','Hi']
questions = ['How are you?','How are you doing?']
responses = ['Okay','I am fine']
validations = ['yes','yeah','yea','no','No','Nah','nah']
verifications = ['Are you sure?','You sure?','you sure?','sure?',"Sure?"]

while CONVERSING:
	lang = 'en-US'
	speed = .3
	
	userInput = raw_input(">>>Me: ")
	if userInput in greetings:
		random_greeting = random.choice(greetings)
		say(random_greeting,lang,speed)
		print random_greeting
		memory.append((userInput,random_greeting))
	elif userInput in questions:
		random_response = random.choice(responses)
		say(random_response,lang,speed)
		memory.append((userInput,random_response))
		print random_response
	elif userInput in verifications:
		random_response = random.choice(validations)
		say(random_response,lang,speed)
		memory.append((userInput,random_response))
		print random_response
	elif 'sure' in userInput:
		response = "Sure about what?"
		say(response,lang,speed)
		memory.append(('sure',response))
		print response
	else:
		say("I did not understand what you said. Goodbye",lang,speed)
		CONVERSING = False
		
for conversations in memory:
	print conversations

	
'''This can be extended with elif statements and eventually you will eventually have your very own slightly clever AI. Just make sure that the else statement is always at the bottom of your code'''
They call me the Tutorial Doctor.
Angus Hollands
Angus Hollands

And a condensed version ;)


from __future__ import print_statement
import random
from speech import *

CONVERSING = True

memory = []
greetings = ['hola', 'hello', 'hi','hey!','Hello','Hi']
questions = ['How are you?','How are you doing?']
responses = ['Okay','I am fine']
validations = ['yes','yeah','yea','no','No','Nah','nah']
verifications = ['Are you sure?','You sure?','you sure?','sure?',"Sure?"]

engagement_pairs = (greetings, greetings), (questions, responses), (verifications, validations)

while CONVERSING:
    lang = 'en-US'
    speed = .3
    
    userInput = raw_input(">>>Me: ")
    for triggers, outputs in engagement_pairs:
        if not user_input in triggers:
            continue
            
		random_output = random.choice(outputs)
            
        say(random_output, lang, speed)
        print(random_output)
        memory.append((userInput, random_output))
        break
        
    else:
        if 'sure' in userInput:
            response = "Sure about what?"
            say(response,lang,speed)
            memory.append(('sure',response))
            print response
        else:
            say("I did not understand what you said. Goodbye",lang,speed)
            CONVERSING = False
ApochPiQ
ApochPiQ
I had a pretty sophisticated IRC bot infrastructure in JavaScript written as a Chrome plugin at one time... I'm tempted to dig it up and write an article on designing chat bots.
Tutorial Doctor
Tutorial Doctor
Nice angus. I'm getting better at Python, and it's always good to see more techniques. I actually was wondering how I could better pair the "engagements."
They call me the Tutorial Doctor.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.