Difference between revisions of "CSC111 Homework 8 2014"
(→Part 1: Improved Reflections) |
|||
(8 intermediate revisions by the same user not shown) | |||
Line 7: | Line 7: | ||
| | | | ||
<bluebox> | <bluebox> | ||
− | This assignment is about ''Artificial Intelligence'', and will require you to use tests and lists in a program that will simulate a therapist. | + | This assignment is about ''Artificial Intelligence'' and ''Data Filtering'', and will require you to use tests and lists in a program that will simulate a therapist. |
− | The program is due on 4/ | + | The program is due on Thursday 4/3/14 at midnight. You are encouraged to pair-program this assignment. |
</bluebox> | </bluebox> | ||
Line 28: | Line 28: | ||
<!--For a weird implementation of Eliza, check [http://showcase.pandorabots.com/pandora/talk?botid=d10d53a63e345abf&skin=iframe this place] out... | <!--For a weird implementation of Eliza, check [http://showcase.pandorabots.com/pandora/talk?botid=d10d53a63e345abf&skin=iframe this place] out... | ||
--> | --> | ||
− | + | <br /> | |
==Setup== | ==Setup== | ||
− | + | <br /> | |
* Copy the following [[CSC111 Eliza 1 | program]] in a file called eliza1.py | * Copy the following [[CSC111 Eliza 1 | program]] in a file called eliza1.py | ||
* Play with the program | * Play with the program | ||
Line 36: | Line 36: | ||
* Also, from time to time, use "I", "you", "me", or "your" in your answer, as in "I like you" for example. See how the program responds. We call this a ''reflection.'' | * Also, from time to time, use "I", "you", "me", or "your" in your answer, as in "I like you" for example. See how the program responds. We call this a ''reflection.'' | ||
* At some point type '''bye''' at the prompt and verify that the program stops. | * At some point type '''bye''' at the prompt and verify that the program stops. | ||
− | + | <br /> | |
==Reflection== | ==Reflection== | ||
− | + | <br /> | |
* Start your Eliza program and tell it: '''I like you''' | * Start your Eliza program and tell it: '''I like you''' | ||
Line 52: | Line 52: | ||
* See the problem? | * See the problem? | ||
− | + | <br /> | |
==Part 1: Improved Reflections == | ==Part 1: Improved Reflections == | ||
− | + | <br /> | |
Part 1 of your assignment is to improve the way your program performs reflections. | Part 1 of your assignment is to improve the way your program performs reflections. | ||
When doing a reflection, your program should transform the string "you" at the beginning of a sentence in "I". However, if the word "you" is not at the beginning of a sentence, your program will translate it into "me". | When doing a reflection, your program should transform the string "you" at the beginning of a sentence in "I". However, if the word "you" is not at the beginning of a sentence, your program will translate it into "me". | ||
Line 77: | Line 77: | ||
:::This shirt suits me well? | :::This shirt suits me well? | ||
− | + | <br /> | |
==Part 2: Picking up on keywords== | ==Part 2: Picking up on keywords== | ||
− | + | <br /> | |
Modify your program so that if you type the words ''mother'' or ''father'' anywhere in your input, the program will respond with a random sentence whose topic should include the word ''family''. | Modify your program so that if you type the words ''mother'' or ''father'' anywhere in your input, the program will respond with a random sentence whose topic should include the word ''family''. | ||
Line 87: | Line 87: | ||
Make your program also pick up on '''father''', '''brother''', and '''sister''' (and more if you feel like it). | Make your program also pick up on '''father''', '''brother''', and '''sister''' (and more if you feel like it). | ||
− | + | <br /> | |
==Part 3: Let's be polite== | ==Part 3: Let's be polite== | ||
− | + | <br /> | |
Make your program scold the user if he/she does not start the first sentence with the word '''hello''' or '''Hello'''. Feel free to find an appropriate sentence for this faux pas. | Make your program scold the user if he/she does not start the first sentence with the word '''hello''' or '''Hello'''. Feel free to find an appropriate sentence for this faux pas. | ||
− | + | <br /> | |
==Part 4: Ending the conversation== | ==Part 4: Ending the conversation== | ||
− | + | <br /> | |
− | Your program should stop not only if the user enters '''bye''', but it should also recognize '''goodbye,''' '''ciao'''. | + | Your program should stop not only if the user enters '''bye''', but it should also recognize '''goodbye,''' and '''ciao'''. |
+ | <br /> | ||
==Part 5: Aggressive user== | ==Part 5: Aggressive user== | ||
− | + | <br /> | |
If the user enters a sentence of the form '''I xxxx you''', we can imagine that the word '''xxxx''' is a verb, and that the user is being ''aggressive''. In this case Eliza should respond with a sentence of the form '''Why do you xxxx me?''' | If the user enters a sentence of the form '''I xxxx you''', we can imagine that the word '''xxxx''' is a verb, and that the user is being ''aggressive''. In this case Eliza should respond with a sentence of the form '''Why do you xxxx me?''' | ||
− | + | <br /> | |
==Part 6: Dealing with exclamation marks and question marks== | ==Part 6: Dealing with exclamation marks and question marks== | ||
− | + | <br /> | |
If the user enters a sentence of the form I lost you!, then the last word, "you" will not be reflected in "me" because the string.split() function will have kept the exclamation mark attached to the word "you." The last word is thus "you!" and will not be reflected by the current program. | If the user enters a sentence of the form I lost you!, then the last word, "you" will not be reflected in "me" because the string.split() function will have kept the exclamation mark attached to the word "you." The last word is thus "you!" and will not be reflected by the current program. | ||
Figure out a way for your program to be able to reflect the last words of a sentence, even when they are terminated by a "!", "?", or "." | Figure out a way for your program to be able to reflect the last words of a sentence, even when they are terminated by a "!", "?", or "." | ||
Be efficient in your coding. The fewer changes you need to make to your program to make it behave that way, the better. | Be efficient in your coding. The fewer changes you need to make to your program to make it behave that way, the better. | ||
− | + | <br /> | |
==Part 7: Memory== | ==Part 7: Memory== | ||
− | + | <br /> | |
Whenever the user enters a sentence of the form '''I xxxx you''', make your program remember it so that whenever it has to output a canned response, it will sometimes output '''Is that why you xxxx me?'''. | Whenever the user enters a sentence of the form '''I xxxx you''', make your program remember it so that whenever it has to output a canned response, it will sometimes output '''Is that why you xxxx me?'''. | ||
Line 139: | Line 140: | ||
<br /> | <br /> | ||
<source lang="python"> | <source lang="python"> | ||
− | from temp | + | from temp import temperatures |
</source> | </source> | ||
<br /> | <br /> | ||
− | You should now be able to read all the lines contained in the variable '''temperatures''' (there should be | + | You should now be able to read all the lines contained in the variable '''temperatures''' (there should be 2794 lines in it). |
Write a well documented program that uses functions, including a main function, that will answer the following questions. Each question should be answered by calling a function to which you will pass '''temperatures''' as a parameter. The function will print the answer and return to your main function. | Write a well documented program that uses functions, including a main function, that will answer the following questions. Each question should be answered by calling a function to which you will pass '''temperatures''' as a parameter. The function will print the answer and return to your main function. | ||
Line 167: | Line 168: | ||
<br /> | <br /> | ||
;Question 1 | ;Question 1 | ||
− | :Which month and year and in which city was the coldest temperature recorded? | + | :Which month and year and in which city was the coldest temperature recorded? Please include the temperature in your output. |
;Question 2 | ;Question 2 | ||
− | :Which month and year and in which city was the hottest temperature recorded? | + | :Which month and year and in which city was the hottest temperature recorded? Please include the temperature in your output. |
;Question 3 | ;Question 3 | ||
− | :Which city was the coldest in December 2011? The function should be able to answer for any year. The year will be passed to the function as a string parameter (see above). | + | :Which city was the coldest in December 2011? The function should be able to answer this question for any year. The year will be passed to the function as a string parameter (see above). If the temperature is "---", then the other temperature is the lowest. If the other temperature is also "---", then the program reports that the temperature was not reported in December of that year. |
;Question 4 | ;Question 4 | ||
− | :Which city saw the largest difference of temperature between min and max, a given month, and what year did this happen? | + | :Which city saw the largest difference of temperature between min and max, a given month, and what year did this happen? Include the temperature difference in your print statement. |
<br /> | <br /> | ||
Latest revision as of 12:21, 3 April 2014
--D. Thiebaut (talk) 12:35, 26 March 2014 (EDT)
This assignment is about Artificial Intelligence and Data Filtering, and will require you to use tests and lists in a program that will simulate a therapist. The program is due on Thursday 4/3/14 at midnight. You are encouraged to pair-program this assignment. |
Eliza
You will call your program hw8a.py.
Please, meet Eliza
Please read this introduction to Eliza in Wikipedia. The goal of this assignment is to write our own version of Eliza.
Setup
- Copy the following program in a file called eliza1.py
- Play with the program
- Every so often, try answering "no" or "never" and see how the program reacts.
- Also, from time to time, use "I", "you", "me", or "your" in your answer, as in "I like you" for example. See how the program responds. We call this a reflection.
- At some point type bye at the prompt and verify that the program stops.
Reflection
- Start your Eliza program and tell it: I like you
- Observe that it responds: you like me?
- This is a reflection! The program understands that you are addressing the computer, and it responds by using the same verb you used, but switches the word I for you and you for me. This way I like you becomes You like me?
- Unfortunately, your current version of Eliza is not very clever.
- Try: you do remind me of my mother
- Observe its output.
- See the problem?
Part 1: Improved Reflections
Part 1 of your assignment is to improve the way your program performs reflections.
When doing a reflection, your program should transform the string "you" at the beginning of a sentence in "I". However, if the word "you" is not at the beginning of a sentence, your program will translate it into "me".
Modify your program so that it will perform reflections correctly (at least for I, you, and me).
If you program works correctly, it will reflect the following sentence:
- You are not amusing
into
- I am not amusing?
And it should also reflect this sentence:
- This shirt suits you well
into
- This shirt suits me well?
Part 2: Picking up on keywords
Modify your program so that if you type the words mother or father anywhere in your input, the program will respond with a random sentence whose topic should include the word family.
For example, if you type My mother always calls me at the wrong time, your Eliza program could respond with something like Is that typical in your family? or Is that a family trait?
Make sure your program recognizes different versions of the keywords, such as (but not limited to) mother, Mother, or MOTHER.
Make your program also pick up on father, brother, and sister (and more if you feel like it).
Part 3: Let's be polite
Make your program scold the user if he/she does not start the first sentence with the word hello or Hello. Feel free to find an appropriate sentence for this faux pas.
Part 4: Ending the conversation
Your program should stop not only if the user enters bye, but it should also recognize goodbye, and ciao.
Part 5: Aggressive user
If the user enters a sentence of the form I xxxx you, we can imagine that the word xxxx is a verb, and that the user is being aggressive. In this case Eliza should respond with a sentence of the form Why do you xxxx me?
Part 6: Dealing with exclamation marks and question marks
If the user enters a sentence of the form I lost you!, then the last word, "you" will not be reflected in "me" because the string.split() function will have kept the exclamation mark attached to the word "you." The last word is thus "you!" and will not be reflected by the current program.
Figure out a way for your program to be able to reflect the last words of a sentence, even when they are terminated by a "!", "?", or "."
Be efficient in your coding. The fewer changes you need to make to your program to make it behave that way, the better.
Part 7: Memory
Whenever the user enters a sentence of the form I xxxx you, make your program remember it so that whenever it has to output a canned response, it will sometimes output Is that why you xxxx me?.
As an example, imagine that the user has already typed in her conversation with Eliza I dislike you and I trust you, whenever the user enters a sentence that is not reflected and not negative, the program will sometimes, but not always, respond with Is that why you dislike me?, or Is that why you trust me?.
This should really enhance the illusion that the program is intelligent and remembers the conversation.
Recommendations
- Use functions! What we have built so far is a very linear program, but it should use functions. For example, returning a canned sentence from a list of canned sentences can easily be done by a function.
- Only PG13 programs will be accepted! Be imaginative in creating your dialogs and responses, and make your programs enjoyable and fun to read and play with!
Submission
Submit your Eliza program to this URL.
Problem #2
Create a new file called temp.py and paste into it the string temperatures found on this page.
Create a second program called hw8b.py, and add the following statement at the top, below the header:
from temp import temperatures
You should now be able to read all the lines contained in the variable temperatures (there should be 2794 lines in it).
Write a well documented program that uses functions, including a main function, that will answer the following questions. Each question should be answered by calling a function to which you will pass temperatures as a parameter. The function will print the answer and return to your main function.
Here is an example of what your main function should look like:
def main():
# answer to question 1
findColdestTemp( temperatures )
# answer to question 2
findHottestTemp( temperatures )
# answer to question 3
findColdestCity( temperatures, "2011" )
# answer to question 4
findLargestSwing( temperatures )
- Question 1
- Which month and year and in which city was the coldest temperature recorded? Please include the temperature in your output.
- Question 2
- Which month and year and in which city was the hottest temperature recorded? Please include the temperature in your output.
- Question 3
- Which city was the coldest in December 2011? The function should be able to answer this question for any year. The year will be passed to the function as a string parameter (see above). If the temperature is "---", then the other temperature is the lowest. If the other temperature is also "---", then the program reports that the temperature was not reported in December of that year.
- Question 4
- Which city saw the largest difference of temperature between min and max, a given month, and what year did this happen? Include the temperature difference in your print statement.
Submission
Submit your hw8b.py program to this URL.