Chatbot in 18 lines of code (Python) help.

Started by
9 comments, last by Tutorial Doctor 9 years, 4 months ago
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.

Advertisement

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

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

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")

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

random_number.png

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.

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.

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.

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
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.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

This topic is closed to new replies.

Advertisement