B
    >?[                 @   sb   d dl mZ d dlZd dlZd dlmZ ddddddd	d
dddddddddZG dd deZdS )    )print_functionN)inputz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 | _ || _|  | _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 |t j|fqS  )recompile
IGNORECASE).0xyr
   r
   -lib/python3.7/site-packages/nltk/chat/util.py
<listcomp>7   s    z!Chat.__init__.<locals>.<listcomp>N)_pairs_reflections_compile_reflections_regex)selfZpairsreflectionsr
   r
   r   __init__'   s    zChat.__init__c          	   C   s6   t | j tdd}tddttj	|tj
S )NT)keyreversez	\b({0})\b|)sortedr   keyslenr   r   formatjoinmapescaper   )r   Zsorted_reflr
   r
   r   r   ;   s    zChat._compile_reflectionsc                s    j  fdd| 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|  |    S )N)r   stringstartend)Zmo)r   r
   r   <lambda>L   s    z"Chat._substitute.<locals>.<lambda>)r   sublower)r   strr
   )r   r   _substituteA   s    
zChat._substitutec             C   sj   | d}xZ|dkrdt||d |d  }|d | | || ||d d   }| d}qW |S )N%r         )findintr+   group)r   responsematchposZnumr
   r
   r   
_wildcardsO   s    

,zChat._wildcardsc             C   s   xz| j D ]p\}}||}|rt|}| ||}|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
        Nz?..z???)r   r3   randomZchoicer5   )r   r*   patternr2   r3   Zrespr
   r
   r   respond[   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| | qW d S )N >z!.)r   EOFErrorprintr;   )r   r<   Z
user_inputr
   r
   r   converseu   s    
zChat.converseN)r<   )	__name__
__module____qualname__r   r   r+   r5   r;   rB   r
   r
   r
   r   r	   &   s   r	   )	Z
__future__r   r   r9   Z	six.movesr   r   objectr	   r
   r
   r
   r   <module>
   s(   