3
d                 @   sH   d dl Z d dlZddddddddd	d
dddddddZG dd dZdS )    Nzyou arezyou wereyouz	you wouldzyou havezyou willyourzI amzI waszI havezI willmyZmineme)zi amzi wasizi'mzi'dzi'vezi'llr   zyou arezyou werezyou'vezyou'llr   Zyoursr   r   c               @   sB   e Zd Zi fddZdd Zdd Zdd Zd	d
 ZdddZdS )Chatc             C   s$   dd |D | _ || _| j | _dS )a  
        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: list of tuple
        :param pairs: The patterns and responses
        :type reflections: dict
        :param reflections: A mapping between first and second person expressions
        :rtype: None
        c             S   s"   g | ]\}}t j|t j|fqS  )recompile
IGNORECASE).0xyr   r   ./tmp/pip-build-v9q4h5k9/nltk/nltk/chat/util.py
<listcomp>3   s    z!Chat.__init__.<locals>.<listcomp>N)_pairs_reflections_compile_reflections_regex)selfpairsreflectionsr   r   r   __init__#   s    zChat.__init__c             C   s2   t | jtdd}tjdjdjttj|tj	S )NT)keyreversez\b({})\b|)
sortedr   lenr	   r
   formatjoinmapescaper   )r   Zsorted_reflr   r   r   r   7   s    zChat._compile_reflectionsc                s    j j fdd|j S )z
        Substitute words in the string, according to the specified reflections,
        e.g. "I'm" -> "you are"

        :type str: str
        :param str: The string to be mapped
        :rtype: str
        c                s    j | j| j | j   S )N)r   stringstartend)mo)r   r   r   <lambda>H   s    z"Chat._substitute.<locals>.<lambda>)r   sublower)r   strr   )r   r   _substitute=   s    
zChat._substitutec             C   sj   |j d}xZ|dkrdt||d |d  }|d | | j|j| ||d d   }|j d}qW |S )N%r         )findintr*   group)r   responsematchposnumr   r   r   
_wildcardsK   s    

,zChat._wildcardsc             C   s   xz| j D ]p\}}|j|}|rtj|}| j||}|dd dkrT|dd d }|d	d dkrt|dd
 d }|S qW dS )z
        Generate a response to the user input.

        :type str: str
        :param str: The string to be mapped
        :rtype: str
        r-   Nz?..z???r8   r8   r8   )r   r2   randomchoicer5   )r   r)   patternr1   r2   respr   r   r   respondW   s    


zChat.respondquitc             C   st   d}xj||krn|}yt d}W n tk
r:   t| Y nX |rx|d dkr\|d d }qBW t| j| qW d S )N >r,   z!.rA   )inputEOFErrorprintr=   )r   r>   Z
user_inputr   r   r   converseq   s    
zChat.converseN)r>   )	__name__
__module____qualname__r   r   r*   r5   r=   rE   r   r   r   r   r   "   s   r   )r9   r	   r   r   r   r   r   r   <module>   s$   