Difference between revisions of "Turing Test and Eliza"
(→Version 3: React to Negative User Answers) |
(→When Two Bots Talk to Each Other) |
||
(16 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
=Introduction to the Turing Test= | =Introduction to the Turing Test= | ||
<br /> | <br /> | ||
+ | ==The Imitation game== | ||
+ | <br /> | ||
+ | <center><videoflash>xlsFwBhiY-M</videoflash></center> | ||
+ | <br /> | ||
+ | ==Video Introduction== | ||
+ | <br /> | ||
+ | {| | ||
+ | | | ||
<center><videoflash>qrTqmWbYD3Q</videoflash></center> | <center><videoflash>qrTqmWbYD3Q</videoflash></center> | ||
+ | | | ||
+ | <center><videoflash>S23yie-779k?t=23m2s</videoflash></center> | ||
+ | |} | ||
+ | <br /> | ||
+ | <br /> | ||
+ | Introduction given by Prof. Suilin Lavelle, in Week 3 of the University of Edinburgh's "Introduction to Philosophy" (INTROPHIL) open online course. | ||
+ | <br /> | ||
+ | Below are two short scenes from the movie Bladerunner, mentioned in Prof. Lavelle's video. | ||
+ | <br /> | ||
+ | <center><videoflash>Qc_204tXHZY</videoflash></center> | ||
<br /> | <br /> | ||
+ | <center><videoflash>ChJVaTqU2So</videoflash></center> | ||
+ | <br /> | ||
+ | |||
=Eliza Program= | =Eliza Program= | ||
<br /> | <br /> | ||
Line 239: | Line 260: | ||
=Version 4: Detect Answer Mentioning Family= | =Version 4: Detect Answer Mentioning Family= | ||
<br /> | <br /> | ||
− | <source lang="python"> | + | <source lang="python" highlight="36-47,91,92"> |
+ | # Eliza4.py | ||
+ | # D. Thiebaut | ||
+ | # A very short beginning program for Eliza | ||
+ | # with random prompts from the program. | ||
+ | import random | ||
+ | |||
+ | |||
+ | # -------------------------------------------------------------------------- | ||
+ | # just print the string to the console | ||
+ | # will be transformed to something better later... | ||
+ | def myprint( string ): | ||
+ | print( string ) | ||
+ | |||
+ | # sayGoodBye | ||
+ | # say goodbye to the user. | ||
+ | def sayGoodBye( name ): | ||
+ | myprint( "Good bye " + name ) | ||
+ | |||
+ | # isGoodBye | ||
+ | # checks to see if what the user said is one of the keywords for | ||
+ | # ending the conversation. | ||
+ | def isGoodBye( userAnswer ): | ||
+ | if userAnswer.lower().strip() in [ "bye", "goodbye", "ciao" ]: | ||
+ | return True | ||
+ | else: | ||
+ | return False | ||
+ | |||
+ | # isNegativeAnswer | ||
+ | # returns True if user says a single word such as "no", or "never" | ||
+ | def isNegative( userAnswer ): | ||
+ | if userAnswer.lower().strip() in [ "no", "never" ]: | ||
+ | return True | ||
+ | else: | ||
+ | return False | ||
+ | |||
+ | # isFamily | ||
+ | # returns True if the user says something about her family | ||
+ | # returns False otherwise. | ||
+ | def isFamily( userAnswer ): | ||
+ | foundIt = False | ||
+ | for word in ["mother", "father", "mom", "dad", "sister", | ||
+ | "brother" ]: | ||
+ | |||
+ | if userAnswer.lower().find( word ) != -1: | ||
+ | foundIt = True | ||
+ | |||
+ | return foundIt | ||
+ | |||
+ | def greetings(): | ||
+ | myprint( "Hello there!" ) | ||
+ | myprint( "What is your name?" ) | ||
+ | name = input( "> " ).capitalize() | ||
+ | myprint( "Welcome " + name ) | ||
+ | return name | ||
+ | |||
+ | # -------------------------------------------------------------------------- | ||
+ | # main function | ||
+ | # -------------------------------------------------------------------------- | ||
+ | def main(): | ||
+ | prompts = ["Please go on...", "Please tell me more...", | ||
+ | "Interesting... Go on, please!", "Yes? Really? Go on", | ||
+ | "Weird... I'm not sure what to think of that..." ] | ||
+ | |||
+ | promptsNegative = [ "Such strong words!", "Really?", "Why?", | ||
+ | "My, we are so negative today!", | ||
+ | "You really think so?", "Why not?" ] | ||
+ | |||
+ | familyPrompts = [ "Tell me more about your family", | ||
+ | "Let's talk more about your parents", | ||
+ | "Do you have brothers or sisters?", | ||
+ | "Interesting how our family comes up in the conversation", | ||
+ | "What about your mother?" ] | ||
+ | |||
+ | # greet user and get her name | ||
+ | userName = greetings() | ||
+ | |||
+ | # conversation: get user input, and respond | ||
+ | for i in range( 1000 ): | ||
+ | |||
+ | # get user's statement | ||
+ | userAnswer = input( "> " ) | ||
+ | |||
+ | # if it is a goodbye statement, exit the loop | ||
+ | if isGoodBye( userAnswer ) == True: | ||
+ | break | ||
+ | |||
+ | # tell the user to continue speaking | ||
+ | if isNegative( userAnswer ) == True: | ||
+ | myprint( random.choice( promptsNegative ) ) | ||
+ | |||
+ | elif isFamily( userAnswer ) == True: | ||
+ | myprint( random.choice( familyPrompts ) ) | ||
+ | |||
+ | else: | ||
+ | myprint( random.choice( prompts ) ) | ||
+ | |||
+ | |||
+ | # if we're here, it's because the loop stopped. | ||
+ | # say goodbye to the user | ||
+ | sayGoodBye( userName ) | ||
+ | |||
+ | |||
+ | main() | ||
+ | |||
</source> | </source> | ||
<br /> | <br /> | ||
+ | |||
=Version 5: Do Reflection on answers of the form "I xxx you"= | =Version 5: Do Reflection on answers of the form "I xxx you"= | ||
<br /> | <br /> | ||
<source lang="python" highlight="84, 85, 95-99"> | <source lang="python" highlight="84, 85, 95-99"> | ||
− | # | + | # Eliza5.py |
+ | # D. Thiebaut | ||
+ | # A very short beginning program for Eliza | ||
+ | # with random prompts from the program. | ||
+ | import random | ||
+ | |||
+ | |||
+ | # -------------------------------------------------------------------------- | ||
+ | # just print the string to the console | ||
+ | # will be transformed to something better later... | ||
+ | def myprint( string ): | ||
+ | print( string ) | ||
+ | |||
+ | # sayGoodBye | ||
+ | # say goodbye to the user. | ||
+ | def sayGoodBye( name ): | ||
+ | myprint( "Good bye " + name ) | ||
+ | |||
+ | # isGoodBye | ||
+ | # checks to see if what the user said is one of the keywords for | ||
+ | # ending the conversation. | ||
+ | def isGoodBye( userAnswer ): | ||
+ | if userAnswer.lower().strip() in [ "bye", "goodbye", "ciao" ]: | ||
+ | return True | ||
+ | else: | ||
+ | return False | ||
+ | |||
+ | # isNegativeAnswer | ||
+ | # returns True if user says a single word such as "no", or "never" | ||
+ | def isNegative( userAnswer ): | ||
+ | if userAnswer.lower().strip() in [ "no", "never" ]: | ||
+ | return True | ||
+ | else: | ||
+ | return False | ||
+ | |||
+ | # isFamily | ||
+ | # returns True if the user says something about her family | ||
+ | # returns False otherwise. | ||
+ | def isFamily( userAnswer ): | ||
+ | foundIt = False | ||
+ | for word in ["mother", "father", "mom", "dad", "sister", | ||
+ | "brother" ]: | ||
+ | |||
+ | if userAnswer.lower().find( word ) != -1: | ||
+ | foundIt = True | ||
+ | |||
+ | return foundIt | ||
+ | |||
+ | |||
+ | def greetings(): | ||
+ | myprint( "Hello there!" ) | ||
+ | myprint( "What is your name?" ) | ||
+ | name = input( "> " ).capitalize() | ||
+ | myprint( "Welcome " + name ) | ||
+ | return name | ||
+ | |||
+ | # -------------------------------------------------------------------------- | ||
+ | # main function | ||
+ | # -------------------------------------------------------------------------- | ||
+ | def main(): | ||
+ | prompts = ["Please go on...", "Please tell me more...", | ||
+ | "Interesting... Go on, please!", "Yes? Really? Go on", | ||
+ | "Weird... I'm not sure what to think of that..." ] | ||
+ | |||
+ | promptsNegative = [ "Such strong words!", "Really?", "Why?", | ||
+ | "My, we are so negative today!", | ||
+ | "You really think so?", "Why not?" ] | ||
+ | |||
+ | familyPrompts = [ "Tell me more about your family", | ||
+ | "Let's talk more about your parents", | ||
+ | "Do you have brothers or sisters?", | ||
+ | "Interesting how our family comes up in the conversation", | ||
+ | "What about your mother?" ] | ||
+ | |||
+ | # greet user and get her name | ||
+ | userName = greetings() | ||
+ | |||
+ | # conversation: get user input, and respond | ||
+ | for i in range( 1000 ): | ||
+ | |||
+ | # get user's statement | ||
+ | userAnswer = input( "> " ) | ||
+ | |||
+ | # split answer into words | ||
+ | words = userAnswer.split( ) | ||
+ | |||
+ | # if it is a goodbye statement, exit the loop | ||
+ | if isGoodBye( userAnswer ) == True: | ||
+ | break | ||
+ | |||
+ | # tell the user to continue speaking | ||
+ | if isNegative( userAnswer ) == True: | ||
+ | myprint( random.choice( promptsNegative ) ) | ||
+ | |||
+ | elif words[0]=="I" and words[2]=="you": | ||
+ | words[0] = "you" | ||
+ | words[2] = "me" | ||
+ | userAnswer = " ".join( words ) | ||
+ | myprint( userAnswer+ "?" ) | ||
+ | |||
+ | elif isFamily( userAnswer ) == True: | ||
+ | myprint( random.choice( familyPrompts ) ) | ||
+ | |||
+ | else: | ||
+ | myprint( random.choice( prompts ) ) | ||
+ | |||
+ | |||
+ | # if we're here, it's because the loop stopped. | ||
+ | # say goodbye to the user | ||
+ | sayGoodBye( userName ) | ||
+ | |||
+ | |||
+ | main() | ||
+ | |||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | =Version 6: Adding ''Memory'' to Eliza= | ||
+ | <br /> | ||
+ | * To add memory to Eliza, we use the reflection, where the program recognizes answers of the form "I xxxx you" (where ''xxxx'' is some verb), and responds with "you xxxx me?", and takes this last sentence and adds it to the '''prompts''' list. This way, every so often, out of the blue, when the program does not recognize a family matter, or a reflection, or a negative comment, it will respond with "You xxxx me?" | ||
+ | <source lang="python" highlight="100-103"> | ||
+ | # Eliza6.py | ||
# D. Thiebaut | # D. Thiebaut | ||
# A very short beginning program for Eliza | # A very short beginning program for Eliza | ||
Line 345: | Line 592: | ||
userAnswer = " ".join( words ) | userAnswer = " ".join( words ) | ||
myprint( userAnswer+ "?" ) | myprint( userAnswer+ "?" ) | ||
+ | # add the question to the random prompts that | ||
+ | # are issued when nothing interesting has been | ||
+ | # found in the user input | ||
+ | prompts.append( "Is that why " + userAnswer+"?" ) | ||
+ | |||
+ | elif isFamily( userAnswer ) == True: | ||
+ | myprint( random.choice( familyPrompts ) ) | ||
+ | |||
+ | else: | ||
+ | myprint( random.choice( prompts ) ) | ||
+ | |||
+ | |||
+ | # if we're here, it's because the loop stopped. | ||
+ | # say goodbye to the user | ||
+ | sayGoodBye( userName ) | ||
+ | |||
+ | |||
+ | main() | ||
+ | |||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | |||
+ | =Version 7: Make Eliza Talk!= | ||
+ | <br /> | ||
+ | ==Mac Version== | ||
+ | <br /> | ||
+ | On a Mac, you can open a Terminal window and type commands of the form: ''say Hello there!'', and the Mac will "say" the sentence. In Python, on a Mac, you can run these commands using a simple statement, as illustrated in the code below: | ||
+ | <br /> | ||
+ | <source lang="python"> | ||
+ | import os | ||
+ | import time | ||
+ | |||
+ | os.system( "say hello there!" ) | ||
+ | time.sleep( 0.5 ) | ||
+ | |||
+ | os.system( "say how are you today?" ) | ||
+ | time.sleep( 0.5 ) | ||
+ | |||
+ | print( "done" ) | ||
+ | |||
+ | </source> | ||
+ | |||
+ | <br /> | ||
+ | Below is the version of Eliza where we modified the contents of the '''myprint()''' function so that it says the answers back. | ||
+ | <br /> | ||
+ | <source lang="python" highlight="6,7,12-18"> | ||
+ | # Eliza7.py | ||
+ | # D. Thiebaut | ||
+ | # A very short beginning program for Eliza | ||
+ | # with random prompts from the program. | ||
+ | import random | ||
+ | import time | ||
+ | import os | ||
+ | |||
+ | # -------------------------------------------------------------------------- | ||
+ | # just print the string to the console | ||
+ | # will be transformed to something better later... | ||
+ | def myprint( sentence ): | ||
+ | print( sentence ) | ||
+ | os.system('say ' + sentence ) | ||
+ | |||
+ | # wait 0.5 seconds to give the program time to say the | ||
+ | # sentence | ||
+ | time.sleep( 0.5 ) | ||
+ | |||
+ | # sayGoodBye | ||
+ | # say goodbye to the user. | ||
+ | def sayGoodBye( name ): | ||
+ | myprint( "Good bye " + name ) | ||
+ | |||
+ | # isGoodBye | ||
+ | # checks to see if what the user said is one of the keywords for | ||
+ | # ending the conversation. | ||
+ | def isGoodBye( userAnswer ): | ||
+ | if userAnswer.lower().strip() in [ "bye", "goodbye", "ciao" ]: | ||
+ | return True | ||
+ | else: | ||
+ | return False | ||
+ | |||
+ | # isNegativeAnswer | ||
+ | # returns True if user says a single word such as "no", or "never" | ||
+ | def isNegative( userAnswer ): | ||
+ | if userAnswer.lower().strip() in [ "no", "never" ]: | ||
+ | return True | ||
+ | else: | ||
+ | return False | ||
+ | |||
+ | # isFamily | ||
+ | # returns True if the user says something about her family | ||
+ | # returns False otherwise. | ||
+ | def isFamily( userAnswer ): | ||
+ | foundIt = False | ||
+ | for word in ["mother", "father", "mom", "dad", "sister", | ||
+ | "brother" ]: | ||
+ | |||
+ | if userAnswer.lower().find( word ) != -1: | ||
+ | foundIt = True | ||
+ | |||
+ | return foundIt | ||
+ | |||
+ | |||
+ | def greetings(): | ||
+ | myprint( "Hello there!" ) | ||
+ | myprint( "What is your name?" ) | ||
+ | name = input( "> " ).capitalize() | ||
+ | myprint( "Welcome " + name ) | ||
+ | return name | ||
+ | |||
+ | # -------------------------------------------------------------------------- | ||
+ | # main function | ||
+ | # -------------------------------------------------------------------------- | ||
+ | def main(): | ||
+ | prompts = ["Please go on...", "Please tell me more...", | ||
+ | "Interesting... Go on, please!", "Yes? Really? Go on", | ||
+ | "Weird... I'm not sure what to think of that..." ] | ||
+ | |||
+ | promptsNegative = [ "Such strong words!", "Really?", "Why?", | ||
+ | "My, we are so negative today!", | ||
+ | "You really think so?", "Why not?" ] | ||
+ | |||
+ | familyPrompts = [ "Tell me more about your family", | ||
+ | "Let's talk more about your parents", | ||
+ | "Do you have brothers or sisters?", | ||
+ | "Interesting how our family comes up in the conversation", | ||
+ | "What about your mother?" ] | ||
+ | |||
+ | # greet user and get her name | ||
+ | userName = greetings() | ||
+ | |||
+ | # conversation: get user input, and respond | ||
+ | for i in range( 1000 ): | ||
+ | |||
+ | # get user's statement | ||
+ | userAnswer = input( "> " ) | ||
+ | |||
+ | # split answer into words | ||
+ | words = userAnswer.split( ) | ||
+ | |||
+ | # if it is a goodbye statement, exit the loop | ||
+ | if isGoodBye( userAnswer ) == True: | ||
+ | break | ||
+ | |||
+ | # tell the user to continue speaking | ||
+ | if isNegative( userAnswer ) == True: | ||
+ | myprint( random.choice( promptsNegative ) ) | ||
+ | |||
+ | elif words[0]=="I" and words[2]=="you": | ||
+ | words[0] = "you" | ||
+ | words[2] = "me" | ||
+ | userAnswer = " ".join( words ) | ||
+ | myprint( userAnswer+ "?" ) | ||
+ | # add the question to the random prompts that | ||
+ | # are issued when nothing interesting has been | ||
+ | # found in the user input | ||
+ | prompts.append( userAnswer+"?" ) | ||
+ | |||
elif isFamily( userAnswer ) == True: | elif isFamily( userAnswer ) == True: | ||
myprint( random.choice( familyPrompts ) ) | myprint( random.choice( familyPrompts ) ) | ||
Line 362: | Line 766: | ||
</source> | </source> | ||
<br /> | <br /> | ||
+ | <br /> | ||
+ | =When Two Bots Talk to Each Other= | ||
+ | <br /> | ||
+ | <center><videoflash>WnzlbyTZsQY</videoflash></center> | ||
+ | <br /> | ||
+ | <!--center><videoflash>zWuw7tWbrlw</videoflash></center--> | ||
+ | <br /> | ||
+ | |||
+ | =Recent Movies Dealing with the Turing Test= | ||
+ | <br /> | ||
+ | ==Imitation Game== | ||
+ | <center><videoflash>lTMEUA6VnJE</videoflash></center> | ||
+ | <br /> | ||
+ | ==Ex Machina== | ||
+ | <br /> | ||
+ | <center><videoflash>Tu1ajhotzj0</videoflash></center> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | [[Category:CSC111]][[Category:Python]] |
Latest revision as of 12:47, 25 March 2018
--D. Thiebaut (talk) 21:23, 4 March 2015 (EST)
Contents
- 1 Introduction to the Turing Test
- 2 Eliza Program
- 3 Version 3: React to Negative User Answers
- 4 Version 4: Detect Answer Mentioning Family
- 5 Version 5: Do Reflection on answers of the form "I xxx you"
- 6 Version 6: Adding Memory to Eliza
- 7 Version 7: Make Eliza Talk!
- 8 When Two Bots Talk to Each Other
- 9 Recent Movies Dealing with the Turing Test
Introduction to the Turing Test
The Imitation game
Video Introduction
|
|
Introduction given by Prof. Suilin Lavelle, in Week 3 of the University of Edinburgh's "Introduction to Philosophy" (INTROPHIL) open online course.
Below are two short scenes from the movie Bladerunner, mentioned in Prof. Lavelle's video.
Eliza Program
Version 1: Let's start with something very basic
# Eliza1.py
# D. Thiebaut
# A very short beginning program for Eliza
# --------------------------------------------------------------------------
# just print the string to the console
# will be transformed to something better later...
def myprint( string ):
print( string )
# sayGoodBye
# say goodbye to the user.
def sayGoodBye( name ):
myprint( "Good bye " + name )
# isGoodBye
# checks to see if what the user said is one of the keywords for
# ending the conversation.
def isGoodBye( userAnswer ):
if userAnswer.lower().strip() in [ "bye", "goodbye", "ciao" ]:
return True
else:
return False
def greetings():
myprint( "Hello there!" )
myprint( "What is your name?" )
name = input( "> " )
myprint( "Welcome " + name )
return name
# --------------------------------------------------------------------------
# main function
# --------------------------------------------------------------------------
def main():
# greet user and get her name
userName = greetings()
# conversation: get user input, and respond
for i in range( 1000 ):
# get user's statement
userAnswer = input( "> " )
# if it is a goodbye statement, exit the loop
if isGoodBye( userAnswer ) == True:
break
# tell the user to continue speaking
myprint( "Please tell me more..." )
# if we're here, it's because the loop stopped.
# say goodbye to the user
sayGoodBye( userName )
main()
Version 2: Let's randomize the program's prompts
The new code is highlighted.
# Eliza2.py
# D. Thiebaut
# A very short beginning program for Eliza
# with random prompts from the program.
import random
# --------------------------------------------------------------------------
# just print the string to the console
# will be transformed to something better later...
def myprint( string ):
print( string )
# sayGoodBye
# say goodbye to the user.
def sayGoodBye( name ):
myprint( "Good bye " + name )
# isGoodBye
# checks to see if what the user said is one of the keywords for
# ending the conversation.
def isGoodBye( userAnswer ):
if userAnswer.lower().strip() in [ "bye", "goodbye", "ciao" ]:
return True
else:
return False
def greetings():
myprint( "Hello there!" )
myprint( "What is your name?" )
name = input( "> " )
myprint( "Welcome " + name )
return name
# --------------------------------------------------------------------------
# main function
# --------------------------------------------------------------------------
def main():
prompts = ["Please go on...", "Please tell me more...",
"Interesting... Go on, please!", "Yes? Really? Go on",
"Weird... I'm not sure what to think of that..." ]
# greet user and get her name
userName = greetings()
# conversation: get user input, and respond
for i in range( 1000 ):
# get user's statement
userAnswer = input( "> " )
# if it is a goodbye statement, exit the loop
if isGoodBye( userAnswer ) == True:
break
# tell the user to continue speaking
myprint( random.choice( prompts ) )
# if we're here, it's because the loop stopped.
# say goodbye to the user
sayGoodBye( userName )
main()
Version 3: React to Negative User Answers
In this version the program will pick up on the user responding "No", or "Never", and respond differently if this is the case. The highlighted lines show the addition of new code.
# Eliza3.py
# D. Thiebaut
# A very short beginning program for Eliza
# with random prompts from the program.
import random
# --------------------------------------------------------------------------
# just print the string to the console
# will be transformed to something better later...
def myprint( string ):
print( string )
# sayGoodBye
# say goodbye to the user.
def sayGoodBye( name ):
myprint( "Good bye " + name )
# isGoodBye
# checks to see if what the user said is one of the keywords for
# ending the conversation.
def isGoodBye( userAnswer ):
if userAnswer.lower().strip() in [ "bye", "goodbye", "ciao" ]:
return True
else:
return False
# isNegativeAnswer
# returns True if user says a single word such as "no", or "never"
def isNegative( userAnswer ):
if userAnswer.lower().strip() in [ "no", "never" ]:
return True
else:
return False
def greetings():
myprint( "Hello there!" )
myprint( "What is your name?" )
name = input( "> " )
myprint( "Welcome " + name )
return name.capitalize()
# --------------------------------------------------------------------------
# main function
# --------------------------------------------------------------------------
def main():
prompts = ["Please go on...", "Please tell me more...",
"Interesting... Go on, please!", "Yes? Really? Go on",
"Weird... I'm not sure what to think of that..." ]
promptsNegative = [ "Such strong words!", "Really?", "Why?",
"My, we are so negative today!",
"You really think so?", "Why not?" ]
# greet user and get her name
userName = greetings()
# conversation: get user input, and respond
for i in range( 1000 ):
# get user's statement
userAnswer = input( "> " )
# if it is a goodbye statement, exit the loop
if isGoodBye( userAnswer ) == True:
break
# tell the user to continue speaking
if isNegative( userAnswer ) == True:
myprint( random.choice( promptsNegative ) )
else:
myprint( random.choice( prompts ) )
# if we're here, it's because the loop stopped.
# say goodbye to the user
sayGoodBye( userName )
main()
Version 4: Detect Answer Mentioning Family
# Eliza4.py
# D. Thiebaut
# A very short beginning program for Eliza
# with random prompts from the program.
import random
# --------------------------------------------------------------------------
# just print the string to the console
# will be transformed to something better later...
def myprint( string ):
print( string )
# sayGoodBye
# say goodbye to the user.
def sayGoodBye( name ):
myprint( "Good bye " + name )
# isGoodBye
# checks to see if what the user said is one of the keywords for
# ending the conversation.
def isGoodBye( userAnswer ):
if userAnswer.lower().strip() in [ "bye", "goodbye", "ciao" ]:
return True
else:
return False
# isNegativeAnswer
# returns True if user says a single word such as "no", or "never"
def isNegative( userAnswer ):
if userAnswer.lower().strip() in [ "no", "never" ]:
return True
else:
return False
# isFamily
# returns True if the user says something about her family
# returns False otherwise.
def isFamily( userAnswer ):
foundIt = False
for word in ["mother", "father", "mom", "dad", "sister",
"brother" ]:
if userAnswer.lower().find( word ) != -1:
foundIt = True
return foundIt
def greetings():
myprint( "Hello there!" )
myprint( "What is your name?" )
name = input( "> " ).capitalize()
myprint( "Welcome " + name )
return name
# --------------------------------------------------------------------------
# main function
# --------------------------------------------------------------------------
def main():
prompts = ["Please go on...", "Please tell me more...",
"Interesting... Go on, please!", "Yes? Really? Go on",
"Weird... I'm not sure what to think of that..." ]
promptsNegative = [ "Such strong words!", "Really?", "Why?",
"My, we are so negative today!",
"You really think so?", "Why not?" ]
familyPrompts = [ "Tell me more about your family",
"Let's talk more about your parents",
"Do you have brothers or sisters?",
"Interesting how our family comes up in the conversation",
"What about your mother?" ]
# greet user and get her name
userName = greetings()
# conversation: get user input, and respond
for i in range( 1000 ):
# get user's statement
userAnswer = input( "> " )
# if it is a goodbye statement, exit the loop
if isGoodBye( userAnswer ) == True:
break
# tell the user to continue speaking
if isNegative( userAnswer ) == True:
myprint( random.choice( promptsNegative ) )
elif isFamily( userAnswer ) == True:
myprint( random.choice( familyPrompts ) )
else:
myprint( random.choice( prompts ) )
# if we're here, it's because the loop stopped.
# say goodbye to the user
sayGoodBye( userName )
main()
Version 5: Do Reflection on answers of the form "I xxx you"
# Eliza5.py
# D. Thiebaut
# A very short beginning program for Eliza
# with random prompts from the program.
import random
# --------------------------------------------------------------------------
# just print the string to the console
# will be transformed to something better later...
def myprint( string ):
print( string )
# sayGoodBye
# say goodbye to the user.
def sayGoodBye( name ):
myprint( "Good bye " + name )
# isGoodBye
# checks to see if what the user said is one of the keywords for
# ending the conversation.
def isGoodBye( userAnswer ):
if userAnswer.lower().strip() in [ "bye", "goodbye", "ciao" ]:
return True
else:
return False
# isNegativeAnswer
# returns True if user says a single word such as "no", or "never"
def isNegative( userAnswer ):
if userAnswer.lower().strip() in [ "no", "never" ]:
return True
else:
return False
# isFamily
# returns True if the user says something about her family
# returns False otherwise.
def isFamily( userAnswer ):
foundIt = False
for word in ["mother", "father", "mom", "dad", "sister",
"brother" ]:
if userAnswer.lower().find( word ) != -1:
foundIt = True
return foundIt
def greetings():
myprint( "Hello there!" )
myprint( "What is your name?" )
name = input( "> " ).capitalize()
myprint( "Welcome " + name )
return name
# --------------------------------------------------------------------------
# main function
# --------------------------------------------------------------------------
def main():
prompts = ["Please go on...", "Please tell me more...",
"Interesting... Go on, please!", "Yes? Really? Go on",
"Weird... I'm not sure what to think of that..." ]
promptsNegative = [ "Such strong words!", "Really?", "Why?",
"My, we are so negative today!",
"You really think so?", "Why not?" ]
familyPrompts = [ "Tell me more about your family",
"Let's talk more about your parents",
"Do you have brothers or sisters?",
"Interesting how our family comes up in the conversation",
"What about your mother?" ]
# greet user and get her name
userName = greetings()
# conversation: get user input, and respond
for i in range( 1000 ):
# get user's statement
userAnswer = input( "> " )
# split answer into words
words = userAnswer.split( )
# if it is a goodbye statement, exit the loop
if isGoodBye( userAnswer ) == True:
break
# tell the user to continue speaking
if isNegative( userAnswer ) == True:
myprint( random.choice( promptsNegative ) )
elif words[0]=="I" and words[2]=="you":
words[0] = "you"
words[2] = "me"
userAnswer = " ".join( words )
myprint( userAnswer+ "?" )
elif isFamily( userAnswer ) == True:
myprint( random.choice( familyPrompts ) )
else:
myprint( random.choice( prompts ) )
# if we're here, it's because the loop stopped.
# say goodbye to the user
sayGoodBye( userName )
main()
Version 6: Adding Memory to Eliza
- To add memory to Eliza, we use the reflection, where the program recognizes answers of the form "I xxxx you" (where xxxx is some verb), and responds with "you xxxx me?", and takes this last sentence and adds it to the prompts list. This way, every so often, out of the blue, when the program does not recognize a family matter, or a reflection, or a negative comment, it will respond with "You xxxx me?"
# Eliza6.py
# D. Thiebaut
# A very short beginning program for Eliza
# with random prompts from the program.
import random
# --------------------------------------------------------------------------
# just print the string to the console
# will be transformed to something better later...
def myprint( string ):
print( string )
# sayGoodBye
# say goodbye to the user.
def sayGoodBye( name ):
myprint( "Good bye " + name )
# isGoodBye
# checks to see if what the user said is one of the keywords for
# ending the conversation.
def isGoodBye( userAnswer ):
if userAnswer.lower().strip() in [ "bye", "goodbye", "ciao" ]:
return True
else:
return False
# isNegativeAnswer
# returns True if user says a single word such as "no", or "never"
def isNegative( userAnswer ):
if userAnswer.lower().strip() in [ "no", "never" ]:
return True
else:
return False
# isFamily
# returns True if the user says something about her family
# returns False otherwise.
def isFamily( userAnswer ):
foundIt = False
for word in ["mother", "father", "mom", "dad", "sister",
"brother" ]:
if userAnswer.lower().find( word ) != -1:
foundIt = True
return foundIt
def greetings():
myprint( "Hello there!" )
myprint( "What is your name?" )
name = input( "> " ).capitalize()
myprint( "Welcome " + name )
return name
# --------------------------------------------------------------------------
# main function
# --------------------------------------------------------------------------
def main():
prompts = ["Please go on...", "Please tell me more...",
"Interesting... Go on, please!", "Yes? Really? Go on",
"Weird... I'm not sure what to think of that..." ]
promptsNegative = [ "Such strong words!", "Really?", "Why?",
"My, we are so negative today!",
"You really think so?", "Why not?" ]
familyPrompts = [ "Tell me more about your family",
"Let's talk more about your parents",
"Do you have brothers or sisters?",
"Interesting how our family comes up in the conversation",
"What about your mother?" ]
# greet user and get her name
userName = greetings()
# conversation: get user input, and respond
for i in range( 1000 ):
# get user's statement
userAnswer = input( "> " )
# split answer into words
words = userAnswer.split( )
# if it is a goodbye statement, exit the loop
if isGoodBye( userAnswer ) == True:
break
# tell the user to continue speaking
if isNegative( userAnswer ) == True:
myprint( random.choice( promptsNegative ) )
elif words[0]=="I" and words[2]=="you":
words[0] = "you"
words[2] = "me"
userAnswer = " ".join( words )
myprint( userAnswer+ "?" )
# add the question to the random prompts that
# are issued when nothing interesting has been
# found in the user input
prompts.append( "Is that why " + userAnswer+"?" )
elif isFamily( userAnswer ) == True:
myprint( random.choice( familyPrompts ) )
else:
myprint( random.choice( prompts ) )
# if we're here, it's because the loop stopped.
# say goodbye to the user
sayGoodBye( userName )
main()
Version 7: Make Eliza Talk!
Mac Version
On a Mac, you can open a Terminal window and type commands of the form: say Hello there!, and the Mac will "say" the sentence. In Python, on a Mac, you can run these commands using a simple statement, as illustrated in the code below:
import os
import time
os.system( "say hello there!" )
time.sleep( 0.5 )
os.system( "say how are you today?" )
time.sleep( 0.5 )
print( "done" )
Below is the version of Eliza where we modified the contents of the myprint() function so that it says the answers back.
# Eliza7.py
# D. Thiebaut
# A very short beginning program for Eliza
# with random prompts from the program.
import random
import time
import os
# --------------------------------------------------------------------------
# just print the string to the console
# will be transformed to something better later...
def myprint( sentence ):
print( sentence )
os.system('say ' + sentence )
# wait 0.5 seconds to give the program time to say the
# sentence
time.sleep( 0.5 )
# sayGoodBye
# say goodbye to the user.
def sayGoodBye( name ):
myprint( "Good bye " + name )
# isGoodBye
# checks to see if what the user said is one of the keywords for
# ending the conversation.
def isGoodBye( userAnswer ):
if userAnswer.lower().strip() in [ "bye", "goodbye", "ciao" ]:
return True
else:
return False
# isNegativeAnswer
# returns True if user says a single word such as "no", or "never"
def isNegative( userAnswer ):
if userAnswer.lower().strip() in [ "no", "never" ]:
return True
else:
return False
# isFamily
# returns True if the user says something about her family
# returns False otherwise.
def isFamily( userAnswer ):
foundIt = False
for word in ["mother", "father", "mom", "dad", "sister",
"brother" ]:
if userAnswer.lower().find( word ) != -1:
foundIt = True
return foundIt
def greetings():
myprint( "Hello there!" )
myprint( "What is your name?" )
name = input( "> " ).capitalize()
myprint( "Welcome " + name )
return name
# --------------------------------------------------------------------------
# main function
# --------------------------------------------------------------------------
def main():
prompts = ["Please go on...", "Please tell me more...",
"Interesting... Go on, please!", "Yes? Really? Go on",
"Weird... I'm not sure what to think of that..." ]
promptsNegative = [ "Such strong words!", "Really?", "Why?",
"My, we are so negative today!",
"You really think so?", "Why not?" ]
familyPrompts = [ "Tell me more about your family",
"Let's talk more about your parents",
"Do you have brothers or sisters?",
"Interesting how our family comes up in the conversation",
"What about your mother?" ]
# greet user and get her name
userName = greetings()
# conversation: get user input, and respond
for i in range( 1000 ):
# get user's statement
userAnswer = input( "> " )
# split answer into words
words = userAnswer.split( )
# if it is a goodbye statement, exit the loop
if isGoodBye( userAnswer ) == True:
break
# tell the user to continue speaking
if isNegative( userAnswer ) == True:
myprint( random.choice( promptsNegative ) )
elif words[0]=="I" and words[2]=="you":
words[0] = "you"
words[2] = "me"
userAnswer = " ".join( words )
myprint( userAnswer+ "?" )
# add the question to the random prompts that
# are issued when nothing interesting has been
# found in the user input
prompts.append( userAnswer+"?" )
elif isFamily( userAnswer ) == True:
myprint( random.choice( familyPrompts ) )
else:
myprint( random.choice( prompts ) )
# if we're here, it's because the loop stopped.
# say goodbye to the user
sayGoodBye( userName )
main()
When Two Bots Talk to Each Other
Recent Movies Dealing with the Turing Test
Imitation Game
Ex Machina