You can get the code for the parent class Clock we discussed in class from here. If you have not implemented the DigitalClock class in ps07, please complete that before you attempt this exercise. Your goal for this exercise is to implement an AnalogClock class which is subclass (child class) of Clock.
Take a moment to answer these questions:
The code below is an example of how to draw text on the screen.
from graphics import *
# create the graphics window
win = GraphWin("Extra Problem Set", 300, 300)
# create a line connecting the points at coordinates (10, 10) and (100, 100)
line1 = Line( Point( 10, 10 ), Point(100, 100) )
line1.draw( win )
# process events
win.mainloop()
Run your program and make sure the line appears on the screen. Try changing the line style - look at the setWidth and setArrow methods in the documentation (Sections 3 and 3.2).
Here is an example of what the clock should look like. Feel free to play with its appearance.

The clock's components are the face, the marks for the hours and minutes, the three hands - for the hour, the minutes and the seconds. You can start by first drawing marks only for the hours - 3, 6, 9, 12. The other marks are optional.
You will have to think how to find the points on the concentric circles - both for drawing the marks and for drawing the hands based on the time.
Hint: Trigonometry anyone? Remember the definitions of sine and cosine function and the notorius Unit circle?
The unit circle is centered at (0, 0) and has radius 1.
A point on the circle has coordinates (x, y) where x = cost and y = sint.Now what if we have a circle with radius r but still centered at 0.
A point on the circle will have coordinates (x, y) where x = rcost and y = rsint.And if we move the center of the circle to a point (a,b).
A point on the circle will have coordinates (x, y) where
x = a + rcost andy = b + rsint.
In the case of drawing the clock, you have to remeber that 1) the y-axis is flipped in the coordinate system for drawing graphics and 2) you can assume that the angle z = 0 when the hour hand is pointing at 12 o'clock and it grows as we move clockwise. In the canonical example above, the angle grows in counterclockwise direction. In our case, you will need to express the coordinates of the point (x, y) in the figure below based on z, r and c (where c is the center point of the circle).
Hint: Create a method get_point_on_circle that will take the center point of the circle, c, the radius, r, and the angle, z, as parameters and will return the Point(x,y) coordinates of the particular point on the circle. Then you could draw the seconds hand like this:
hand_angle = 2 * math.pi * (self.second / 60.0) pend = self.get_point_on_circle(self.pos, 0.8 * self.radius, hand_angle)) sec_hand = Line(self.pos, pend) sec_hand.setArrow('last') sec_hand.draw(win)center is the center of the clock face, self.radius is the radius of the clock face (here we want the length of the hand to be 80% of the radius of the clock face), and self.sec are the seconds based on the current time. *Reminder* You will have to import the math module to use the math functions sin, cos and the value pi.
Now make your clock track the time. You might need to look at the undraw method on the graphics objects in the documentation (Sections 3).
When you are done, print a copy of the file and turn it in. Make sure your name is in the comment section of your program.
Submit the file electronically by typing:
athena% submit extra clocks.py
ELIZA was a computer program created by Joseph Weizenbaum in the early 1960s. The program was named after the character Eliza from the play Pygmalion by George Bernard Shaw where she was taught how to speak as an aristocrat without truly understanding what she was saying. The goal of the program was to simulate a Rogerian psychotherapist by taking the patient's answers and reformulating them as questions. ELIZA is a classic example of natural language processing (NLP) and it uses simple pattern matching techniques. Here is a description of how the program works.
In this exercise, we will implement a simple version of ELIZA, but before you start try it out either on this page or that one.
Start by creating a file eliza.py and a class Eliza. We are going to add the capabilities of the class step by step.
Create a method that greets the user and invites them to share their problems. Give the method an appropriate name.
Create a method called run that would run therapy session. Since currently the only other capability your object has is to print a greeting, add some code in run to print the greeting. Test your program by creating an object and calling the run method.
therapist = Eliza() therapist.run()
Make sure your program prints the greeting.
Add an attribute to your Eliza class that would hold a list of canned responses. Example responses would be. Feel free to add your own.
Please tell me more Why do you say that? In what way? I am listening... I see... Please, go on... I am not sure I understand
Then create a method print_canned_response that would print a random canned response. To pick a random response from the list, use the randint function from the random module.
Now modify the run method, so that it will wait for the user to type something and then print a random canned response. Make your program continue printing randomed canned responses until the user types 'bye'.
Again run your program and make sure it works.
One of the ways Eliza appears to be intelligent is to repeat what the user says but changing the sentence from first person to second person. For example, if the user says "I am stressed", Eliza would respond "You are stressed?". You can accompish this by replacing 'I' with 'you', 'am' with 'are', 'my' with 'your', etc.
Add a method called perform_reflection that takes a string as a parameter and returns a string with the appropriate words replaced.
Hint remember the split method on string and the join method on list. Here is an example.user_phrase = 'Doom and gloom is coming' words = user_phrase.split() print words delimeter = ' ' print delimeter.join(words)
Here is the list of reflections you should support:
Note use " (double quotes) if the string you are creating has a single quote in it "I'm". You can also use single quotes but then you need to add an escape character \ before the ' so that the python interpreter will know to ignore it when it does the matching, e.g. 'I\'m'.
Another hint: Remember dictionaries are your friend. Add another attribute to your class that will be a dictionary of reflections.
Once you have all the appropriate reflections replaced, we need to add the ? at the end of the sentence. Modify the last word in the list so that it has a ? at the end.
For example, if your list of words now contains [ 'You', 'are', 'bored', 'today' ], transform it into [ 'You', 'are', 'bored', 'today?' ]. Do this only if any substitutions were performed.
Once you are done, you need to modify your run method again. Now instead of just printing a canned response every time, first check if you can perform a reflection, if you can do it, otherwise print a canned response.
Again try your program and make sure it works.
All is good but not quite. What if the user says 'I hate you'? The function you wrote in the previous section will return 'You hate you?'. Not exactly what we wanted. Change your code so that it replaces 'you' with 'me'. You will have to add more rules in order to be able to decide if you should replace 'you' with 'I' or 'me' but you don't have to do this here. If you have time at the end, you can think about some heuristics to do that.
Once you make the change, make sure your program provides the correct response 'You hate me?'
Now let's add more variety to Eliza's responses. Let's make the program look for specific keywords in the user responses and respond with a canned response based on the keyword. For example, if the user uses 'No', or 'never', the program can resond with 'You are awfully negative', 'No?', 'Really?', etc.
Or if the user's response contains the words mother or father, the program will respond with a random canned response that includes the world family. For example, 'My mother always wakes me up too early', Eliza would say 'Is that typical of your family?' or 'Tell me more about your family', etc. Feel free to add other keywords and come up with your own canned responses.
Now modify your Eliza class, so that it has a method or methods that would check for the corresponding keywords in the user sentence and produce an appropriate response. There are many ways to do this - you can either create a separate method for each type of keywords or you can create only one. It is up to you.
Once you are done, modify the run method, so that it first checks for keywords and if it finds any, it prints an appropriate response, otherwise it checks for refelctions and if there are none, it will default to printing a generic canned response.
Again run your program and make sure it works.
When you are done, print a copy of the file and turn it in. Make sure your name is in the comment section of your program.
Submit the file electronically by typing:
athena% submit extra eliza.py