Sign in to follow this  

Chatbot in 18 lines of code (Python) help.

This topic is 1103 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Edited by Tutorial Doctor

Share this post


Link to post
Share on other sites
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'''

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

This topic is 1103 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this