³ò
3ÒÇIc           @   sÂ   d  d k  Z  d  d k Z d  d k Z h  d d <d d <d d <d d	 <d
 d <d d <d d <d d <d d <d d <d d <d d <d d <d d <Z d e f d     YZ d S(   i˙˙˙˙Nt   aret   amt   weret   wast   yout   is	   you woulds   i'ds   you haves   i'ves   you wills   i'llt   yourt   mys   I haves   you'ves   I wills   you'llt   minet   yourst   met   Chatc           B   s;   e  Z h  d   Z d   Z d   Z d   Z d d  Z RS(   c         C   sL   g  } | D]( \ } } | t  i | t  i  | f q ~ |  _ | |  _ d S(   sÏ  
        Initialize the chatbot.  Pairs is a list of patterns and responses.  Each
        pattern is a regular expression matching the user's statement or question,
        e.g. r'I like (.*)'.  For each such pattern a list of possible responses
        is given, e.g. ['Why do you like %1', 'Did you ever dislike %1'].  Material
        which is matched by parenthesized sections of the patterns (e.g. .*) is mapped to
        the numbered positions in the responses, e.g. %1.

        @type pairs: C{list} of C{tuple}
        @param pairs: The patterns and responses
        @type reflections: C{dict}
        @param reflections: A mapping between first and second person expressions
        @rtype: C{None}
        N(   t   ret   compilet
   IGNORECASEt   _pairst   _reflections(   t   selft   pairst   reflectionst   _[1]t   xt   y(    (    s$   /p/zhu/06/nlp/nltk/nltk/chat/util.pyt   __init__!   s    ?c         C   s_   d } xR t  i t  i |   D]8 } |  i i |  o |  i | } n | d | 7} q W| S(   sċ   
        Substitute words in the string, according to the specified reflections,
        e.g. "I'm" -> "you are"
        
        @type str: C{string}
        @param str: The string to be mapped
        @rtype: C{string}
        t    t    (   t   stringt   splitt   lowerR   t   has_key(   R   t   strt   wordst   word(    (    s$   /p/zhu/06/nlp/nltk/nltk/chat/util.pyt   _substitute5   s    
 c         C   s   t  i | d  } xn | d j o` t  i | | d | d ! } | |  |  i | i |   | | d } t  i | d  } q W| S(   Nt   %i    i   i   (   R   t   findt   atoiR!   t   group(   R   t   responset   matcht   post   num(    (    s$   /p/zhu/06/nlp/nltk/nltk/chat/util.pyt
   _wildcardsF   s    c         C   s£   x |  i  D] \ } } | i |  } | oo t i |  } |  i | |  } | d d j o | d  d } n | d d j o | d  d } n | Sq
 q
 Wd S(   s¤   
        Generate a response to the user input.
        
        @type str: C{string}
        @param str: The string to be mapped
        @rtype: C{string}
        iŝ˙˙˙s   ?.t   .s   ??t   ?N(   R   R'   t   randomt   choiceR*   (   R   R   t   patternR&   R'   t   resp(    (    s$   /p/zhu/06/nlp/nltk/nltk/chat/util.pyt   respondP   s    

 ##t   quitc         C   s   d } x | | j oq | } y t  d  } Wn t j
 o | GHn X| o5 x  | d d j o | d  } qQ W|  i |  GHq	 q	 Wd  S(   NR   t   >i˙˙˙˙s   !.(   t	   raw_inputt   EOFErrorR1   (   R   R2   t   input(    (    s$   /p/zhu/06/nlp/nltk/nltk/chat/util.pyt   converseh   s    
#(   t   __name__t
   __module__R   R!   R*   R1   R7   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/chat/util.pyR       s
   		
	(   R   R   R-   R   t   objectR   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/chat/util.pys   <module>   s$   													