o
    rZh0                     @   s   d dl Z d dlZi 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ddddZG dd dZdS )    Nzi amzyou arezi waszyou wereiyouzi'mzi'dz	you wouldzi'vezyou havezi'llzyou willZmyZyourzI amzI waszyou'vezI havezyou'llzI willZyoursZminemec                   @   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   =/var/www/auris/lib/python3.10/site-packages/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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   s    z"Chat._substitute.<locals>.<lambda>)r   sublower)r   strr   r$   r   _substitute=   s   
zChat._substitutec                 C   sl   | d}|dkr4t||d |d  }|d | | || ||d d   }| d}|dks	|S )N%r         )findintr)   group)r   responsematchposnumr   r   r   
_wildcardsK   s   


zChat._wildcardsc                 C   s   | j D ]:\}}||}|r=t|}| ||}|dd dkr)|dd d }|dd dkr9|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   r1   randomchoicer4   )r   r(   patternr0   r1   respr   r   r   respondW   s   


zChat.respondquitc                 C   s~   d}||kr=|}zt d}W n ty   t| Y nw |r7|d dv r0|d d }|d dv s$t| | ||ksd S d S )N >z!.)inputEOFErrorprintr<   )r   r=   Z
user_inputr   r   r   converseq   s   zChat.converseN)r=   )	__name__
__module____qualname__r   r   r)   r4   r<   rD   r   r   r   r   r   "   s    r   )r8   r   r   r   r   r   r   r   <module>   sH   
	
