Difference between revisions of "CSC111 Lab 8 2011"

From dftwiki3
Jump to: navigation, search
(Rosette)
(Catching key words)
 
(19 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
--[[User:Thiebaut|D. Thiebaut]] 15:04, 26 October 2011 (EDT)
 
--[[User:Thiebaut|D. Thiebaut]] 15:04, 26 October 2011 (EDT)
 
----
 
----
 +
=Break and Continue=
 +
 +
* Solve the [[Exercises on break, continue, and in statements|exercises we did in class ]] on Tuesday.
 +
 
=Rosette=
 
=Rosette=
 
[[Image:RosetteChatbot.png|right|150px]]
 
[[Image:RosetteChatbot.png|right|150px]]
Line 16: Line 20:
  
 
<br />
 
<br />
+
 
<br />
 
 
 
<br />
 
 
 
<br />
 
 
 
<br />
 
 
 
<br />
 
 
 
<br />
 
 
 
 
<br />
 
<br />
  
Line 39: Line 31:
 
==Playing with Eliza==
 
==Playing with Eliza==
  
Eliza is named after Eliza Doolittle, of the 1913 George Bernard Shaw play ''Pigmalion'', which is the inspiration for the musical My Fair Lady.  
+
Eliza is named after Eliza Doolittle, of the 1913 George Bernard Shaw play ''Pigmalion'', which is the inspiration for the musical ''My Fair Lady'', of which you can find many snipets on YouTube, as illusrated below.
 
<br />
 
<br />
<center><videoflash>Doz5w2W-jAY</videoflash></center>
+
<center><videoflash>AZqESl-0OkA</videoflash></center>
 
<br />
 
<br />
  
Line 59: Line 51:
 
       CTRL-x k
 
       CTRL-x k
  
=The initial program=
+
=An initial Eliza program=
  
Cut and paste the program below into a file called eliza.py:
+
Copy and paste the program below into a file called '''eliza.py'''.  Feel free to work with Idle or with emacs.
 
<br />
 
<br />
 
<source lang="python">
 
<source lang="python">
Line 84: Line 76:
 
def main():
 
def main():
  
     # 1) greet user
+
     # greet user
 
     print(  "Welcome.  Please tell me of your problems: " )
 
     print(  "Welcome.  Please tell me of your problems: " )
 
     print( '(You may quit at any time by answering "bye")' )
 
     print( '(You may quit at any time by answering "bye")' )
 
      
 
      
     # 2) repeat a huge number of times
+
     # repeat a huge number of times
 
     for i in range( 10000 ):
 
     for i in range( 10000 ):
  
         # 2.1) get user's statement about her life
+
         # get user's statement about her life
 
         answer = input( "\n> " )
 
         answer = input( "\n> " )
 
         if ( answer.lower() == "bye" ):
 
         if ( answer.lower() == "bye" ):
 
             break
 
             break
 
              
 
              
         # 2.2) respond semi-intelligently
+
         # respond semi-intelligently
 
         # cycle through the list of canned sentences...
 
         # cycle through the list of canned sentences...
 
         print( random.choice( canned ) )
 
         print( random.choice( canned ) )
Line 107: Line 99:
  
  
Run the program and enter at least 6 or 7 sentences. Observe how the program repeats itself in the way it responds to the user.
+
* Run the program and enter at least 6 or 7 sentences. Observe how the program repeats itself in the way it responds to the user.
 +
 
 +
* Go ahead and add a few more canned answers if you wish.  This will make it harder for a human to figure out the repetition, but not foolproof.
 +
 
 +
* By the way, you do not have to type sentences that make sense to test your program. Just the letter 'a' is enough of an input!
 +
 
 +
=Detect Family Members=
 +
 
 +
* Let's start by checking to see if the user is mentioning "mother" in the conversation.  If so, we'll have our program respond with some family-oriented statement.
 +
 
 +
* How can we see if the user mentioned ''mother'' in her sentence?  There are several possibilities.  The one we'll use here is to use the string ''find()'' method.  To see how it works, try these python statements in the python shell:
 +
 
 +
 
 +
>>> answer = "My mother sent me"
 +
>>> answer.find( "mother" )
 +
...
 +
>>> answer.find( "My" )
 +
...
 +
>>> answer.find( "father" )
 +
...
 +
>>> answer.find( "me" )
 +
...
 +
>>> answer.find( "MOTHER" )
 +
...
 +
>>>
 +
 
 +
* Notice that ''find()'' returns the ''index'' of where a word appears in the string '''answer'''.  If the string doesn't appear in ''answer'', then ''find()'' returns -1.
  
Go ahead and add a few more canned answers if you want :-) This will make it harder for a human to figure out the repetition, but not foolproof.
+
* So, something like this should work for us:
 +
<br />
 +
<br />
 +
<source lang="python">
 +
    answer = input( "> " )
  
By the way, you do not have to type sentences that make sense to test your program. Just the letter 'a' is enough of an input!
+
    # did user mention "mother"?
 +
    if answer.find( "mother" )  != -1:
 +
        print( "tell me more about your family, please." )
  
 +
</source>
 +
<br />
 +
<br />
 +
{| style="width:100%; background:silver"
 +
|-
 +
|
 +
==Challenge 1==
 +
|}
 +
[[Image:QuestionMark1.jpg|right|120px]]
  
=Dealing with Negative Answers=
+
* Integrate this solution in your program.  Make sure your program detects correctly the word ''mother'' whether it is spelled in lower or upper case.
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
{| style="width:100%; background:silver"
 +
|-
 +
|
 +
==Challenge 2==
 +
|}
 +
[[Image:QuestionMark4.jpg|right|120px]]
  
* If the user responds with just one word, such as "No", "Never", or "Not", let's make Eliza respond with a sentence of this type "Why so negative?".
+
* Make your program check as well for the words ''father'', ''brother'', ''sister'', ''son'', ''daughter''.  
 +
<br />
 +
<br />
 +
<br />
 +
<br />
  
=Reflection=
+
=Simple Reflection=
  
One way for Eliza to appear intelligent is to say the same thing the user just said, but to change the sentence from the first person to the second person. For example, if the user says "I am tired", Eliza will respond "You are tired".
+
* One way for Eliza to appear intelligent is to say the same thing the user just said, but to change the sentence from the first person to the second person. For example, if the user says "I am tired", Eliza might respond "You are tired?".
We can program that by replacing all occurrences of "I" by "you", "am" by "are", "my" by "your", and "me" by "you".
 
  
How can this be done?
+
* We can program that by replacing all occurrences of "I" by "you", "am" by "are", "my" by "your", and "me" by "you".
  
Here is a possible solution (code it in a separate program):
+
* Here is a possible solution (code it in a separate program):
  
 
<br />
 
<br />
Line 154: Line 201:
 
<br />
 
<br />
  
Go ahead and implement this modification. Play with it. Verify that when you use "I" in a sentence, the program replaces it by "you". Understand why the program actually does that.
+
* Go ahead and implement this modification. Play with it. Verify that when you use "I" in a sentence, the program replaces it by "you". Understand why the program actually does that.
 +
<br />
 +
<br />
 +
{| style="width:100%; background:silver"
 +
|-
 +
|
 +
==Challenge 3==
 +
|}
 +
[[Image:QuestionMark2.jpg|right|120px]]
  
Once you have tested this, go ahead and make your program also replace
 
  
"me" by "you"
+
* Once you have tested this, go ahead and make your program also replace
"am" by "are"
+
** "me" by "you"
"I've" by "you've"
+
** "am" by "are"
"I'm" by "you're"
+
** "I've" by "you've"
"I'll" by "you'll"
+
** "I'm" by "you're"
"was" by "were"
+
** "I'll" by "you'll"
"my" by "your"
+
** "was" by "were"
"mine" by "yours"
+
** "my" by "your"
 +
** "mine" by "yours"
 +
 
 +
<br />
 +
<br />
  
 
==Adding a ? at the end of the reflected sentence==
 
==Adding a ? at the end of the reflected sentence==
  
* Once you have your list of new words newWords containing the reflected words, modify the last word of this list and add a "?" at the end of it.
+
* Once you have your list of new words newWords containing the reflected words, either modify the last word of this list and add a "?" at the end of it, or simply add a "?" to the end of the sentence.
  
* In other words, if newWords is [ "You", "are", "bored", "today" ], transform it into [ "You", "are", "bored", "today?" ].
+
==Reflection or Canned Answer?==
 +
 
 +
* Depending on how you have modified your program, it may respond to the user with two sentences. One reflected and one canned.
 +
 
 +
* What you should do is make your program output the reflected answer when it finds out that it was able to change some of the words in the user's string, and make it output a canned answer when there is no reflection.
 +
 
 +
* For example, if the user says "I am bored", your Eliza program will respond "you are bored?", but if the user says "never!", your program will respond with an answer of the type "Please go on..."
  
==Reflection or Canned Answer?==
 
  
Depending on how you have modified your program, it may respond to the user with two sentences. One reflected and one canned.
+
'' '''Hint #1''': you could compare the original list of words entered by the user, to the list of words generated after the reflection.''
  
What you should do is make your program output the reflected answer when it finds out that it was able to change some of the words in the user's string, and make it output a canned answer when there is no reflection.
+
'' '''Hint #2''': Remember that we saw the '''continue''' statement, and how it works...''
  
For example, if the user says "I am bored", your Eliza program will respond "you are bored?", but if the user says "never!", your program will respond with an answer of the type "Please go on..."
+
<br />
 +
<br />
 +
{| style="width:100%; background:silver"
 +
|-
 +
|
 +
==Challenge 4==
 +
|}
 +
[[Image:QuestionMark3.jpg|right|120px]]
  
Figure out what kind of test you can use for your program to know if it is reflecting the user input, or not, and output either the reflected sentence, or a canned answer.
 
  
'' '''Hints #1: you could compare the original list of words entered by the user, to the list of words generated after the reflection.''' ''
+
Test your program!  It should be able to reflect sentences such as "I like you", and should detect when feed it a family-related word.   And if it can't reflect and doesn't find a reference to a family member, it should respond with a canned answer.  It should really start showing some amount of (fake) artificial intelligence!
  
Test your program! It should really start showing some artificial intelligence!
+
<br />
 +
<br />
 +
<br />
  
 
=Second level reflection: Switching second person to first person=
 
=Second level reflection: Switching second person to first person=
Line 193: Line 264:
 
* Right now your program will output "you don't like you?" which is not quite right. So we should try to fix this.
 
* Right now your program will output "you don't like you?" which is not quite right. So we should try to fix this.
  
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
{| style="width:100%; background:silver"
 +
|-
 +
|
 +
==Challenge 5==
 +
|}
 +
[[Image:QuestionMark5.jpg|right|120px]]
 +
<br />
 
* Go ahead, and make your program replace the 2nd person in the user's input by the first person. You will see that without doing a semantic analysis of the sentence, it is impossible to know whether "you" should be replaced by "I", or by "me". At this point, we will not worry about this, and simply replace "you" by "me" and this will have a higher probability of looking correct.
 
* Go ahead, and make your program replace the 2nd person in the user's input by the first person. You will see that without doing a semantic analysis of the sentence, it is impossible to know whether "you" should be replaced by "I", or by "me". At this point, we will not worry about this, and simply replace "you" by "me" and this will have a higher probability of looking correct.
  
 
* If your modification is correct, and the user enters "I don't like you", your program should output "you don't like me?".
 
* If your modification is correct, and the user enters "I don't like you", your program should output "you don't like me?".
 
+
<br />
 
=Catching key words=
 
=Catching key words=
  
* You may have noticed that with the real Eliza program, when you say something like "No", or "never", the program picks up on this and responds something like "why are you so negative?".
+
* You may have noticed that with the real Eliza program, when your answer is a single word such as  "No", or "never", the program picks up on this and responds something like "why are you so negative?".
  
* Let's make your program pick up on a "NO" answer.
+
* Let's make your program pick up on a single-word "NO" answer.
  
 
* All you need is an if statement that will test whether the user's answer is one word only, and this word is "no".
 
* All you need is an if statement that will test whether the user's answer is one word only, and this word is "no".
Line 207: Line 289:
 
<br />
 
<br />
 
<source lang="python">
 
<source lang="python">
    # check for reflection
+
        answer = input( "> " )
    if words != newWords:
+
         if answer.lower() =="no":
         # then output the reflected answer
+
             print( "you are begin very negative today!" )
        ...
+
            ...
    else:
+
 
        if len( words ) == 1 and words[0] =="no":
 
             print "you are begin very negative today!"
 
        else:
 
            # print canned answer
 
 
</source>
 
</source>
 
<br />
 
<br />
  
* Note the '''and''' operator: it is a logical operator. It forces the whole expression of the if-statement to be true only if the two parts on each side of the "and" are true.
+
 
 +
* Integrate this new test in your program.
  
 
==Random "negative canned" answers==
 
==Random "negative canned" answers==
Line 225: Line 304:
 
Instead of your program always outputting "you are begin very negative today!" in the case explored in the previous section, make it select its response from a new series of canned negative responses (like "No?", "Really?", "You are awfully negative", "why not?", etc.)
 
Instead of your program always outputting "you are begin very negative today!" in the case explored in the previous section, make it select its response from a new series of canned negative responses (like "No?", "Really?", "You are awfully negative", "why not?", etc.)
  
=Picking up on "No" and "Never"=
+
<br />
 +
<br />
 +
{| style="width:100%; background:silver"
 +
|-
 +
|
 +
==Challenge 6==
 +
|}
 +
[[Image:QuestionMark8.jpg|right|120px]]
  
* What if we not only wanted our program to pick up on "No", but also on "Never"? All we need to do is change the if statement to read "if the user's answer contains 1 word only, and this word is "no" or "never"..."
+
<br />
 +
* What if we not only wanted our program to pick up on "No", but also on "Never", "Niet", or "Not"? All we need to do is change the if statement to read "if the user's answer contains 1 word only, and this word is "no" or "never"..."
  
 
* Here is the test in python:
 
* Here is the test in python:
  
         if len( words )==1 and ( words[0]=="no" or words[0]=="never" ):
+
<br />
             index = random.randrange( len( cannedNegative ) )
+
<source lang="python">
            print cannedNegative[ index ]
+
         if answer in [ "no", "never", "niet", "not" ]:
        else:
+
             print( random.choice( cannedNegative ) )
            # print canned answer
+
</source>
 +
<br />
 +
 
 +
<br />
 +
<br />
 +
{| style="width:100%; background:silver"
 +
|-
 +
|
 +
==Challenge of the Day==
 +
|}
 +
[[Image:QuestionMark9.jpg|right|120px]]
 +
 
 +
<br />
 +
How can we make Eliza remember some of the sentences she said before, and make her repeat them later?  For example, assume that at some point Eliza tells the user "You like me?" as a reflection of "I like you".  Then, a few minutes later, when Eliza doesn't find anything interesting in what the user says (no negation, no family members, nothing to reflect), instead of saying a simple canned answer, Eliza could say "Is that why you like me?".  In other words, make the reflected answers from Eliza also become part of the canned answers, with "Is that" appended in front of them...
 +
<br />
 +
 
 +
<br />
 +
 
 +
<br />
 +
 
 +
<br />
 +
 
 +
<br />
 +
 
 +
=Learning More about Eliza=
 +
 
 +
* If you want to know more about this important program in the history of computer science, you may enjoy reading [http://www.cs.umbc.edu/courses/331/papers/eliza.html this paper].
 +
 
 +
<br />
 +
<br />
 +
<br />
 +
 
 +
<br />
 +
You are now ready to work on [[CSC111 Homework 6 2011| Homework 6]].  It does not deal with Eliza, as we hadn't covered enough of this material after the lecture on Tuesday.  Instead, you will have to work with moving circles in a graphics window.

Latest revision as of 11:29, 27 October 2011

--D. Thiebaut 15:04, 26 October 2011 (EDT)


Break and Continue

Rosette

RosetteChatbot.png
  • Rosette is this year's winner of the Loebner prize for best chatterbot, or chatbot. Give her a try, and see how long it takes for you to make her make a mistake that a human behing would not make:









Eliza!

Today we are going to therapy and will consult with Doctor Eliza.

Playing with Eliza

Eliza is named after Eliza Doolittle, of the 1913 George Bernard Shaw play Pigmalion, which is the inspiration for the musical My Fair Lady, of which you can find many snipets on YouTube, as illusrated below.


Joseph Weizenbaum originated the program. A good description of what how Eliza works can be found here.

First, play with Eliza a bit to get a feel for it. You can either use this page, or that one, or use the "therapist" included in emacs. To start the therapist in emacs, start emacs in your 111a-xx account, and type the following commands:

     Shift ESC 
then
     x doctor 

and press the Enter key. Emacs will switch to "doctor" mode... When you are done and want to return to editing your program, type

     CTRL-x k

An initial Eliza program

Copy and paste the program below into a file called eliza.py. Feel free to work with Idle or with emacs.

# eliza.py
#
# a very simple beginning for an eliza-like program
# 
# 1) greet user
# 2) repeat a huge number of times:
#   2.1)  get user's statement about life
#   2.2)  respond in a way that seems intelligent
#
# Addition:
#  cycles through a series of canned answers to get
#  the user to think the computer is listening
import random

canned = [ "\nPlease tell me more",
           "\nI see...",
           "\nI am listening..." ]

def main():

     # greet user
     print(  "Welcome.  Please tell me of your problems: " )
     print( '(You may quit at any time by answering "bye")' )
     
     # repeat a huge number of times
     for i in range( 10000 ):

         # get user's statement about her life
         answer = input( "\n> " )
         if ( answer.lower() == "bye" ):
             break
            
         # respond semi-intelligently
         # cycle through the list of canned sentences...
         print( random.choice( canned ) )

main()




  • Run the program and enter at least 6 or 7 sentences. Observe how the program repeats itself in the way it responds to the user.
  • Go ahead and add a few more canned answers if you wish. This will make it harder for a human to figure out the repetition, but not foolproof.
  • By the way, you do not have to type sentences that make sense to test your program. Just the letter 'a' is enough of an input!

Detect Family Members

  • Let's start by checking to see if the user is mentioning "mother" in the conversation. If so, we'll have our program respond with some family-oriented statement.
  • How can we see if the user mentioned mother in her sentence? There are several possibilities. The one we'll use here is to use the string find() method. To see how it works, try these python statements in the python shell:


>>> answer = "My mother sent me"
>>> answer.find( "mother" )
...
>>> answer.find( "My" )
...
>>> answer.find( "father" )
...
>>> answer.find( "me" )
...
>>> answer.find( "MOTHER" )
...
>>> 
  • Notice that find() returns the index of where a word appears in the string answer. If the string doesn't appear in answer, then find() returns -1.
  • So, something like this should work for us:



    answer = input( "> " )

    # did user mention "mother"?
    if answer.find( "mother" )  != -1:
         print( "tell me more about your family, please." )



Challenge 1

QuestionMark1.jpg
  • Integrate this solution in your program. Make sure your program detects correctly the word mother whether it is spelled in lower or upper case.






Challenge 2

QuestionMark4.jpg
  • Make your program check as well for the words father, brother, sister, son, daughter.





Simple Reflection

  • One way for Eliza to appear intelligent is to say the same thing the user just said, but to change the sentence from the first person to the second person. For example, if the user says "I am tired", Eliza might respond "You are tired?".
  • We can program that by replacing all occurrences of "I" by "you", "am" by "are", "my" by "your", and "me" by "you".
  • Here is a possible solution (code it in a separate program):


def main():
  answer = input( "\n> " )
  
  # split user's sentence into words     
  words = answer.split( ) 
  
  #create a copy new list of words that will be Eliza's response
  newWords = []
  for word in words:
       newWords.append( word )
  
  # take each word of user sentence and if the word represents 
  # the first person singular, switch it to second person.
  for i in range( len( words ) ):           
    if words[i] == "I": 
         newWords[i] = "you"

  # join the list of words in newWords in a string, and print it         
  sentence =  ' '.join( newWords )
  print( sentence )

main()


  • Go ahead and implement this modification. Play with it. Verify that when you use "I" in a sentence, the program replaces it by "you". Understand why the program actually does that.



Challenge 3

QuestionMark2.jpg


  • Once you have tested this, go ahead and make your program also replace
    • "me" by "you"
    • "am" by "are"
    • "I've" by "you've"
    • "I'm" by "you're"
    • "I'll" by "you'll"
    • "was" by "were"
    • "my" by "your"
    • "mine" by "yours"



Adding a ? at the end of the reflected sentence

  • Once you have your list of new words newWords containing the reflected words, either modify the last word of this list and add a "?" at the end of it, or simply add a "?" to the end of the sentence.

Reflection or Canned Answer?

  • Depending on how you have modified your program, it may respond to the user with two sentences. One reflected and one canned.
  • What you should do is make your program output the reflected answer when it finds out that it was able to change some of the words in the user's string, and make it output a canned answer when there is no reflection.
  • For example, if the user says "I am bored", your Eliza program will respond "you are bored?", but if the user says "never!", your program will respond with an answer of the type "Please go on..."


Hint #1: you could compare the original list of words entered by the user, to the list of words generated after the reflection.

Hint #2: Remember that we saw the continue statement, and how it works...



Challenge 4

QuestionMark3.jpg


Test your program! It should be able to reflect sentences such as "I like you", and should detect when feed it a family-related word. And if it can't reflect and doesn't find a reference to a family member, it should respond with a canned answer. It should really start showing some amount of (fake) artificial intelligence!




Second level reflection: Switching second person to first person

  • What if the user says to the program "I don't like you"?
  • Right now your program will output "you don't like you?" which is not quite right. So we should try to fix this.





Challenge 5

QuestionMark5.jpg


  • Go ahead, and make your program replace the 2nd person in the user's input by the first person. You will see that without doing a semantic analysis of the sentence, it is impossible to know whether "you" should be replaced by "I", or by "me". At this point, we will not worry about this, and simply replace "you" by "me" and this will have a higher probability of looking correct.
  • If your modification is correct, and the user enters "I don't like you", your program should output "you don't like me?".


Catching key words

  • You may have noticed that with the real Eliza program, when your answer is a single word such as "No", or "never", the program picks up on this and responds something like "why are you so negative?".
  • Let's make your program pick up on a single-word "NO" answer.
  • All you need is an if statement that will test whether the user's answer is one word only, and this word is "no".


        answer = input( "> " )
        if answer.lower() =="no":
            print( "you are begin very negative today!" )
            ...



  • Integrate this new test in your program.

Random "negative canned" answers

Instead of your program always outputting "you are begin very negative today!" in the case explored in the previous section, make it select its response from a new series of canned negative responses (like "No?", "Really?", "You are awfully negative", "why not?", etc.)



Challenge 6

QuestionMark8.jpg


  • What if we not only wanted our program to pick up on "No", but also on "Never", "Niet", or "Not"? All we need to do is change the if statement to read "if the user's answer contains 1 word only, and this word is "no" or "never"..."
  • Here is the test in python:


        if answer in [ "no", "never", "niet", "not" ]:
            print( random.choice( cannedNegative ) )




Challenge of the Day

QuestionMark9.jpg


How can we make Eliza remember some of the sentences she said before, and make her repeat them later? For example, assume that at some point Eliza tells the user "You like me?" as a reflection of "I like you". Then, a few minutes later, when Eliza doesn't find anything interesting in what the user says (no negation, no family members, nothing to reflect), instead of saying a simple canned answer, Eliza could say "Is that why you like me?". In other words, make the reflected answers from Eliza also become part of the canned answers, with "Is that" appended in front of them...





Learning More about Eliza

  • If you want to know more about this important program in the history of computer science, you may enjoy reading this paper.





You are now ready to work on Homework 6. It does not deal with Eliza, as we hadn't covered enough of this material after the lecture on Tuesday. Instead, you will have to work with moving circles in a graphics window.