B
    .(b$                 @   s   d dl mZ d dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
 ejdd Zd	d
 Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  ZdS )!    )unicode_literalsN)KnowledgeBase)English)EntityRuler)Spanc               C   s   t  S )N)r    r   r   V/home/dcms/DCMS/lib/python3.7/site-packages/spacy/tests/pipeline/test_entity_linker.pynlp   s    r	   c             C   s*   d}| | |  kr | | ks&n t d S )Ng-C6?)AssertionError)abdeltar   r   r   assert_almost_equal   s    r   c             C   s8  t | jdd}|jdddddgd |jdd	d
ddgd |jddddd	gd |jdddgddgd |jddgdgd | dkst| d
kst|ddddgkst|dd
ddgkst|dddd	gkstt|j	dddd t|j	dddd t|j	dddd t|j	dddd dS )zCTest the valid construction of a KB with 3 entities and two aliases   )entity_vector_lengthQ1         )entityfreqentity_vectorQ2         r   Q3   idouglasg?g?)aliasentitiesprobabilitiesadamg?)r   r    Q342g        ZdouglassssssN)
r   vocab
add_entity	add_aliasZget_size_entitiesr
   Zget_size_aliasesZ
get_vectorr   Zget_prior_prob)r	   mykbr   r   r   test_kb_valid_entities   s    r)   c          	   C   sv   t | jdd}|jdddgd |jdddgd |jd	d
dgd tt |jdddgddgd W dQ R X dS )zSTest the invalid construction of a KB with an alias linked to a non-existing entityr   )r   r   r   )r   r   r   r   r   r   r   r   r   r   r$   g?g?)r    r!   r"   N)r   r%   r&   pytestraises
ValueErrorr'   )r	   r(   r   r   r   test_kb_invalid_entities3   s    r-   c          	   C   sv   t | jdd}|jdddgd |jdddgd |jd	d
dgd tt |jddd	gddgd W dQ R X dS )zDTest the invalid construction of a KB with wrong prior probabilitiesr   )r   r   r   )r   r   r   r   r   r   r   r   r   r   g?g?)r    r!   r"   N)r   r%   r&   r*   r+   r,   r'   )r	   r(   r   r   r   test_kb_invalid_probabilitiesC   s    r.   c          	   C   sx   t | jdd}|jdddgd |jdddgd |jd	d
dgd tt  |jddd	gdddgd W dQ R X dS )zTTest the invalid construction of a KB with non-matching entity and probability listsr   )r   r   r   )r   r   r   r   r   r   r   r   r   r   g333333?g?g?)r    r!   r"   N)r   r%   r&   r*   r+   r,   r'   )r	   r(   r   r   r   test_kb_invalid_combinationQ   s    r/   c          	   C   sP   t | jdd}|jdddddgd tt |jdd	dgd W d
Q R X d
S )zMTest the invalid construction of a KB with non-matching entity vector lengthsr   )r   r   r   r   r   )r   r   r   r   r   N)r   r%   r&   r*   r+   r,   )r	   r(   r   r   r   test_kb_invalid_entity_vectora   s    r0   c             C   s  t | jdd}|jdddgd |jdddgd |jd	d
dgd |jddd	gddgd |jddgdgd t|ddkstt|ddkstt|ddkst|dd jdkst|dd jdkstt	|dd j
d t	|dd jd dS )z!Test correct candidate generationr   )r   r      )r   r   r   r      r   r   r   r   r   g?g?)r    r!   r"   r#   g?Z	shrubberyr   N)r   r%   r&   r'   lenget_candidatesr
   Zentity_Zalias_r   Zentity_freq
prior_prob)r	   r(   r   r   r   test_candidate_generationm   s    r6   c          	   C   s   t | jdd}|jdddgd |jdddgd |jd	d
dgd |jddd	gddgd |jddgdgd t|ddkst|jdddd t|ddkstt	t
 |jdddd W dQ R X t|ddkstdS )z5Test that we can append additional alias-entity pairsr   )r   r   r1   )r   r   r   r   r2   r   r   r   r   r   g?g?)r    r!   r"   r#   g?g?)r    r   r5   g333333?N)r   r%   r&   r'   r3   r4   r
   append_aliasr*   ZwarnsUserWarning)r	   r(   r   r   r   test_append_alias   s    r9   c          	   C   s   t | jdd}|jdddgd |jdddgd |jd	d
dgd |jddd	gddgd |jddgdgd tt |jdddd W dQ R X dS )zLTest that append an alias will throw an error if prior probs are exceeding 1r   )r   r   r1   )r   r   r   r   r2   r   r   r   r   r   g?g?)r    r!   r"   r#   g?g?)r    r   r5   N)r   r%   r&   r'   r*   r+   r,   r7   )r	   r(   r   r   r   test_append_invalid_alias   s    r:   c             C   s:  t | jdd}|jdddgd |jdddgd |jddgd	gd
 |jddgdgd
 | d}| | t| }ddddddg}|| | | | jdd}|| |	  d|_
d|_| j|dd d}| |}xR|jD ]H}|j}	|j}
|j }x,|jD ]"}|j|	kr|j|
kstqW qW dS )z9Test that Span.as_doc preserves the existing entity linksr   )r   r   r   )r   r   r   r   r   ZBostongffffff?)r    r!   r"   ZDenverg333333?sentencizerZGPE)labelpatternZentity_linker)nameFT)lastz(She lives in Boston. He lives in Denver.N)r   r%   r&   r'   Zcreate_pipeZadd_piper   Zadd_patternsZset_kbZbegin_trainingZincl_contextZ
incl_priorentstextkb_id_sentZas_docr
   )r	   r(   r;   ZrulerpatternsZel_piperA   docent	orig_textZ
orig_kb_idZsent_docZs_entr   r   r   test_preserving_links_asdoc   s6    





rH   c             C   s   d}| |}t t|jdks"tt|ddddd}|g|_t t|jdksRtt|jd jdksjtt|jd jdkstd	S )
z+Test that doc.ents preserves KB annotationsz(She lives in Boston. He lives in Denver.r   r   r   LOCr   )r<   Zkb_idr   N)r3   listr@   r
   r   label_rB   )r	   rA   rE   Z
boston_entr   r   r   test_preserving_links_ents   s    rL   c             C   s   d}| |}t t|jdks"t|jjd}|jjd}||ddfg|_t t|jdksdtt|jd jdks|tt|jd jdkstdS )	z+Test that doc.ents preserves KB annotationsz(She lives in Boston. He lives in Denver.r   rI   r   r   r   r   N)	r3   rJ   r@   r
   r%   stringsaddrK   rB   )r	   rA   rE   locZq1r   r   r   test_preserving_links_ents_2   s    rP   )
__future__r   r*   Zspacy.kbr   Zspacy.lang.enr   Zspacy.pipeliner   Zspacy.tokensr   Zfixturer	   r   r)   r-   r.   r/   r0   r6   r9   r:   rH   rL   rP   r   r   r   r   <module>   s$   +