NOTE: some newlines added by hand. Script started on Tue Jan 9 09:32:04 2001 oxygen.cs.pitt.edu:1:1> python Python 1.5.2 (#1, Sep 17 1999, 20:15:36) [GCC egcs-2.91.66 19990314/Linux (egcs- on linux-i386 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>> import therapist Therapist --------- Talk to the program by typing in plain English, using normal upper- and lower-case letters and punctuation. Enter "quit" when done. ======================================================================== Hello. How are you feeling today? >I'm ok. Why do you tell me you're ok? >Because you asked me how I am feeling. What other reasons come to mind? >I'm mad at the postman. Do you enjoy being mad at the postman? >No. Very interesting. >He lost my letters. How do you feel when you say that? >Angry. How does that make you feel? >I'm mad at my friend. How does being mad at your friend make you feel? >I'm mad at my friend. Why do you think you're mad at your friend? >quit Thank you for talking with me. >>> reload(therapist) Therapist --------- Talk to the program by typing in plain English, using normal upper- and lower-case letters and punctuation. Enter "quit" when done. ======================================================================== Hello. How are you feeling today? >I need a bath. Would it really help you to get a bath? >Yes. OK, but can you elaborate a bit? >I need a bath. Why do you need a bath? >I need a bath. Why do you need a bath? >I need a bath. Are you sure you need a bath? >quit Thank you for talking with me. >>> therapist.gPats[1] File "", line 1 therapist.gPats[1] ^ SyntaxError: invalid syntax >>> therapist.gPats[1] ["Why don't you \\(.*\\)", ["Do you really think I don't %1?", 'Perhaps eventually I will %1.', 'Do you really want me to %1?']] >>> therapist.gPats[1][0] "Why don't you \\(.*\\)" >>> therapist.gPats[1][1] ["Do you really think I don't %1?", 'Perhaps eventually I will %1.', 'Do you really want me to %1?'] >>> therapist.gPats[0] ['I need \\(.*\\)', ['Why do you need %1?', 'Would it really help you to get %1?', 'Are you sure you need %1?']] >>> therapist.gPats[0][0] 'I need \\(.*\\)' >>> therapist.gPats[0][1] ['Why do you need %1?', 'Would it really help you to get %1?', 'Are you sure you need %1?'] >>> therapist.gPats[2] ["Why can't I \\(.*\\)", ['Do you think you should be able to %1?', 'If you could %1, what would you do?', "I don't know -- why can't you %1?", 'Have you really tried?']] >>> therapist.gPats[2][1] ['Do you think you should be able to %1?', 'If you could %1, what would you do?', "I don't know -- why can't you %1?", 'Have you really tried?'] >>> therapist.gPats[2][1][0] 'Do you think you should be able to %1?' >>> therapist.gPats[2][1][2] "I don't know -- why can't you %1?" >>> therapist.gReflections["am"] 'are' >>> therapist.translate("I am fine", therapist.gReflections) 'you are fine' >>> t = raw_input("my-prompt ") my-prompt my input >>> t 'my input' >>> t[-1] 't' >>> t[-2] 'u' >>> t = t[:-1] >>> t 'my inpu' >>> oxygen.cs.pitt.edu:1:2> ^D Script done on Tue Jan 9 09:40:52 2001 ============ The question was brought up class, could the for-loop in translate be rewritten as follows: for i in words: if words[i] in keys: words[i] = dict[words[i]] This would work, because i would sequence through the list elements, not through their positions in the list. You would get a type error. Consider the following: >>> mylist = [20,21,22,23] [20, 21, 22, 23] >>> for i in mylist: ... print i ... print mylist[i] ... 20 Traceback (innermost last): File "", line 3, in ? IndexError: list index out of range >>> for i in mylist: ... print i ... 20 21 22 23 >>> ======= Lambda functions are more restrictive in python than in lisp: the body of the function is a single expression whose value is the return function. Other functions require return statements to return values. ======= A regular expression example. Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>> p = re.compile('(.)([a-z]+)([1-9]+)') Traceback (innermost last): File "", line 1, in ? NameError: re >>> import re >>> p = re.compile('(.)([a-z]+)([1-9]+)') >>> m = p.match('5nancy345a') >>> m.group() '5nancy345' >>> m.group(1) '5' >>> m.group(2) 'nancy' >>> m.group(3) '345' >>> m.group(1,3,2) ('5', '345', 'nancy') >>> m.group(3,2,1) ('345', 'nancy', '5') >>> m.groups() ('5', 'nancy', '345') ...