B
    /`                 @   s  d dl Z d dlZd dlZd dlZd dlZd dlmZmZmZm	Z	m
Z
 d dlm  mZ d dlmZ d dlmZmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlZeeZ e
e	ef e
e	ef d	d
dZ!ee
e	ef  ee
e	ef  dddZ"G dd deZ#dS )    N)AnyListOptionalTextDict)DOCS_URL_COMPONENTS)ENTITIESTEXT)RasaNLUModelConfig)EntityExtractor)Metadata)Message)matchreturnc             C   sT   | d  ddkrB| d  di  d| d  di  dd}n| d  d}|S )Nvaluetypeintervaltofrom)r   r   )get)r   r    r   \/home/dcms/DCMS/lib/python3.7/site-packages/rasa/nlu/extractors/duckling_entity_extractor.pyextract_value   s
    r   )matchesr   c          
   C   s\   g }xR| D ]J}t |}|d |d |d|dd |d|d |d d}|| q
W |S )	Nstartendbodytextg      ?r   Zdim)r   r   r   r   
confidenceZadditional_infoentity)r   r   append)r   	extractedr   r   r   r   r   r   convert_duckling_format_to_rasa    s    
r"   c            	       s  e Zd ZdZddddddZdeeeef  ee dd fddZ	e
eeef ed dd	d
Zee dddZee dddZeeeeef dddZeeeeeef  dddZeeedddZeeddddZe
deeef eee ed  ed dddZ  ZS ) DucklingEntityExtractorzESearches for structured entites, e.g. dates, using a duckling server.N   )
dimensionsurllocaletimezonetimeout)component_configlanguager   c                s   t  | || _d S )N)super__init__r+   )selfr*   r+   )	__class__r   r   r-   J   s    z DucklingEntityExtractor.__init__)r*   configr   c             C   s   | ||j S )N)r+   )clsr*   r0   r   r   r   createS   s    zDucklingEntityExtractor.create)r   c             C   s<   | j ds0| jpd}d|| }|| j d< | j dS )Nr'    z{}_{})r*   r   r+   formatupper)r.   r+   Z
locale_fixr   r   r   _localeZ   s
    

zDucklingEntityExtractor._localec             C   s"   t jdrt jd S | jdS )zBReturn url of the duckling service. Environment var will override.ZRASA_DUCKLING_HTTP_URLr&   )osenvironr   r*   )r.   r   r   r   _urlc   s    
zDucklingEntityExtractor._url)r   reference_timer   c             C   s,   | j d }||  | j dt||dS )Nr%   r(   )r   r'   tzZdimsZreftime)r*   r6   r   jsondumps)r.   r   r:   r%   r   r   r   _payloadj   s    

z DucklingEntityExtractor._payloadc          
   C   s   t |  d}yh| ||}ddi}tj|||| jdd}|jdkrR|	 S t
d| d|j d	|j  g S W n> tjjtjjfk
r } zt
d
| g S d}~X Y nX dS )a  Sends the request to the duckling server and parses the result.

        Args:
            text: Text for duckling server to parse.
            reference_time: Reference time in milliseconds.

        Returns:
            JSON response from duckling server with parse data.
        z/parsezContent-Typez0application/x-www-form-urlencoded; charset=UTF-8r)   )dataheadersr)      z9Failed to get a proper response from remote duckling at 'z. Status Code: z. Response: a  Failed to connect to duckling http server. Make sure the duckling server is running/healthy/not stale and the proper host and port are set in the configuration. More information on how to run the server can be found on github: https://github.com/facebook/duckling#quickstart Error: {}N)endpoints_utilsZ
concat_urlr9   r>   requestspostr*   r   status_coder<   loggererrorr   
exceptionsConnectionErrorReadTimeoutr4   )r.   r   r:   	parse_urlpayloadr@   responseer   r   r   _duckling_parset   s*    

z'DucklingEntityExtractor._duckling_parse)messager   c          
   C   s`   | j d k	rPyt| j d S  tk
rN } ztd| j | W d d }~X Y nX tt   d S )Ni  z\Could not parse timestamp {}. Instead current UTC time will be passed to duckling. Error: {})timeint
ValueErrorloggingwarningr4   )rP   rN   r   r   r   _reference_time_from_message   s    
z4DucklingEntityExtractor._reference_time_from_message)rP   kwargsr   c             K   s   |   d k	rH| |}| |t|}t|}| jd }t||}ng }t	j
jjjdtd d | |}|jt|tg | dd d S )Nr%   zDuckling HTTP component in pipeline, but no `url` configuration in the config file nor is `RASA_DUCKLING_HTTP_URL` set as an environment variable. No entities will be extracted!z#DucklingEntityExtractor)ZdocsT)Zadd_to_output)r9   rV   rO   r   r	   r"   r*   r#   Zfilter_irrelevant_entitiesrasaZsharedutilsioZraise_warningr   Zadd_extractor_namesetr   )r.   rP   rW   r:   r   Zall_extractedr%   r!   r   r   r   process   s    




zDucklingEntityExtractor.process)meta	model_dirmodel_metadatacached_componentrW   r   c             K   s   |r| dnd }| ||S )Nr+   )r   )r1   r]   r^   r_   r`   rW   r+   r   r   r   load   s    
zDucklingEntityExtractor.load)NN)NNN)__name__
__module____qualname____doc__defaultsr   r   r   r   r-   classmethodr
   r2   r6   r9   rR   r>   r   rO   staticmethodr   rV   r\   r   ra   __classcell__r   r   )r/   r   r#   6   s2    	
-  r#   )$rQ   r<   rT   r7   rC   typingr   r   r   r   r   Zrasa.utils.endpointsrY   Z	endpointsrB   Zrasa.shared.constantsr   Zrasa.shared.nlu.constantsr   r	   Zrasa.nlu.configr
   Zrasa.nlu.extractors.extractorr   Zrasa.nlu.modelr   Z%rasa.shared.nlu.training_data.messager   Zrasa.shared.utils.iorX   	getLoggerrb   rF   r   r"   r#   r   r   r   r   <module>   s$   
 