a
     ˜ëj0  ã                   @   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 )é    Núyou areú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   r   r   z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
   úM/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/nltk/chat/util.pyÚ
<listcomp>3   ó    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 d d 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  ‡ 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)Úmo©r   r
   r   Ú<lambda>H   r   z"Chat._substitute.<locals>.<lambda>)r   ÚsubÚlower)r   Ústrr
   r)   r   Ú_substitute=   s    
ÿzChat._substitutec                 C   sf   |  d¡}|dkrbt||d |d … ƒ}|d |… |  | |¡¡ ||d d …  }|  d¡}q
|S )Nú%r   é   é   )ÚfindÚintr.   Úgroup)r   ÚresponseÚmatchÚposÚnumr
   r
   r   Ú
_wildcardsK   s    

ÿþÿzChat._wildcardsc                 C   s€   | j D ]t\}}| |¡}|rt |¡}|  ||¡}|dd… dkrR|dd… d }|dd… dkrr|dd… d }|  S q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   r6   ÚrandomÚchoicer9   )r   r-   Úpatternr5   r6   Úrespr
   r
   r   ÚrespondW   s    


zChat.respondÚquitc                 C   sj   d}||krf|}zt dƒ}W n ty6   t|ƒ Y n0 |r|d dv rV|d d… }q<t|  |¡ƒ qd S )NÚ ú>éÿÿÿÿz!.)ÚinputÚEOFErrorÚprintrA   )r   rB   Z
user_inputr
   r
   r   Úconverseq   s    zChat.converseN)rB   )	Ú__name__Ú
__module__Ú__qualname__r   r   r.   r9   rA   rI   r
   r
   r
   r   r	   "   s   r	   )r=   r   r   r	   r
   r
   r
   r   Ú<module>   s&   ð