a
     ˜ëjÑ=  ã                   @   sž   d Z ddlZddlZddlmZ ddlmZmZm	Z	m
Z
mZmZ G dd„ dejƒZG dd„ dejƒZG d	d
„ d
ejƒZG dd„ dejƒZG dd„ dejƒZdS )z.
Tests for BLEU translation evaluation metric
é    N)Úfind)ÚSmoothingFunctionÚbrevity_penaltyÚclosest_ref_lengthÚcorpus_bleuÚmodified_precisionÚsentence_bleuc                   @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚTestBLEUc                 C   sÖ  d  ¡ }d  ¡ }d  ¡ }||g}tt||dd�ƒ}t|dƒdksDJ ‚| j|ddd	� tt||d
d�ƒdksnJ ‚tdƒ  ¡ }tdƒ  ¡ }tdƒ  ¡ }d  ¡ }|||g}tt||dd�ƒdks¾J ‚tt||d
d�ƒdksØJ ‚tdƒ  ¡ }tdƒ  ¡ }|||g}tt||dd�ƒ}tt||dd�ƒ}| j|ddd	� | j|ddd	� t|dƒdk�sRJ ‚t|dƒdk�sfJ ‚tt||d
d�ƒ}	tt||d
d�ƒ}
| j|	ddd	� | j|
ddd	� t|	dƒdk�s¾J ‚t|
dƒdk�sÒJ ‚dS )zm
        Examples from the original BLEU paper
        https://www.aclweb.org/anthology/P02-1040.pdf
        zthe cat is on the matzthere is a cat on the matzthe the the the the the theé   )Úné   g›æ§èHÒ?gvi&Œ$IÒ?©Zplacesé   ç        zWIt is a guide to action that ensures that the military will forever heed Party commandszlIt is the guiding principle which guarantees the military forces always being under the command of the PartyzQIt is the practical guide for the army always to heed the directions of the partyzof theç      ð?z^It is a guide to action which ensures that the military always obeys the commands of the partyzSIt is to insure the troops forever hearing the activity guidebook that party directg’Ö‹ã8î?g/Í„‘$Iâ?gñôJY†8î?g›æ§èHâ?gÿåœÐÒÒâ?g):õõ:±³?gû:pÎˆÒâ?grùé·¯³?N)ÚsplitÚfloatr   ÚroundÚassertAlmostEqualÚstr)ÚselfZref1Zref2Úhyp1Ú
referencesZhyp1_unigram_precisionZref3Úhyp2Zhyp2_unigram_precisionZhyp1_bigram_precisionZhyp2_bigram_precision© r   úa/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/nltk/test/unit/translate/test_bleu.pyÚtest_modified_precision   sP    ÿÿÿ
ÿÿ
z TestBLEU.test_modified_precisionc                 C   sœ   dgd dgd g}dgd }t |ƒ}t||ƒ}| jt||ƒddd� dgd dgd dgd dgd g}dgd }t |ƒ}t||ƒ}t||ƒd	ks˜J ‚d S )
NÚaé   é   é   gvàœ¥½ë?r   r   é   r   )Úlenr   r   r   )r   r   Ú
hypothesisZhyp_lenZclosest_ref_lenr   r   r   Útest_brevity_penaltyd   s    

ÿ$

zTestBLEU.test_brevity_penaltyc                 C   sL   d  ¡ g}d  ¡ }tdt|ƒƒD ]&}d| f| }t|||ƒdks J ‚q d S )Nú7The candidate has no alignment to any of the referencesúJohn loves Maryr
   r   r   ©r   Úranger"   r   ©r   r   r#   r   Úweightsr   r   r   Útest_zero_matchesu   s
    
zTestBLEU.test_zero_matchesc                 C   sL   d  ¡ g}d  ¡ }tdt|ƒƒD ]&}d| f| }t|||ƒdks J ‚q d S )Nr&   r
   r   r'   r)   r   r   r   Útest_full_matches   s
    
zTestBLEU.test_full_matchesc                 C   sT   d  ¡ g}d  ¡ }| jt||ƒddd� z|  tt||¡ W n tyN   Y n0 d S )Nr&   zJohn loves Mary who loves Miker   r   r   ©r   r   r   ÚassertWarnsÚUserWarningÚAttributeError©r   r   r#   r   r   r   Ú5test_partial_matches_hypothesis_longer_than_reference‰   s    
z>TestBLEU.test_partial_matches_hypothesis_longer_than_referenceN)Ú__name__Ú
__module__Ú__qualname__r   r$   r+   r,   r2   r   r   r   r   r	      s
   O

r	   c                   @   sD   e Z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 )ÚTestBLEUFringeCasesc                 C   sš   d  ¡ g}d  ¡ }t|ƒd }d| f| }| jt|||ƒddd� z|  tt||¡ W n tyj   Y n0 d  ¡ g}d  ¡ }| jt|||ƒddd� d S )NzJohn loves Mary ?r&   r
   r   r   r   r   )r   r"   r   r   r.   r/   r0   r)   r   r   r   Ú2test_case_where_n_is_bigger_than_hypothesis_length˜   s     
ÿ
ÿzFTestBLEUFringeCases.test_case_where_n_is_bigger_than_hypothesis_lengthc                 C   s$   d  ¡ g}g }t||ƒdks J ‚d S )Nr%   r   ©r   r   r1   r   r   r   Útest_empty_hypothesis³   s    
z)TestBLEUFringeCases.test_empty_hypothesisc                 C   sB   d  ¡ g}dg}tƒ j}zt|||d� W n ty<   Y n0 d S )Nr%   ZFoo)Úsmoothing_function)r   r   Úmethod4r   Ú
ValueError)r   r   r#   r;   r   r   r   Útest_length_one_hypothesis¹   s    
z.TestBLEUFringeCases.test_length_one_hypothesisc                 C   s$   g g}d  ¡ }t||ƒdks J ‚d S )Nr&   r   r8   r1   r   r   r   Útest_empty_referencesÃ   s    z)TestBLEUFringeCases.test_empty_referencesc                 C   s    g g}g }t ||ƒdksJ ‚d S )Nr   )r   r1   r   r   r   Ú$test_empty_references_and_hypothesisÉ   s    z8TestBLEUFringeCases.test_empty_references_and_hypothesisc                 C   sT   d  ¡ g}d  ¡ }| jt||ƒddd� z|  tt||¡ W n tyN   Y n0 d S )Nz	let it goz	let go itr   r   r   r-   r1   r   r   r   Ú3test_reference_or_hypothesis_shorter_than_fourgramsÏ   s    
zGTestBLEUFringeCases.test_reference_or_hypothesis_shorter_than_fourgramsc                 C   s:   d  ¡ g}d  ¡ }t dgd ¡}t|||ƒdks6J ‚d S )Nr%   r&   ç      Ð?r   r   )r   ÚnpÚarrayr   )r   r   r#   r*   r   r   r   Útest_numpy_weightsÝ   s    
z&TestBLEUFringeCases.test_numpy_weightsN)
r3   r4   r5   r7   r9   r=   r>   r?   r@   rD   r   r   r   r   r6   —   s   
r6   c                   @   s   e Zd Zdd„ ZdS )ÚTestBLEUvsMteval13ac              	   C   sx  t dƒ}t dƒ}t dƒ}t|ƒ�.}tt| ¡ d  ¡ dd… ƒ}W d   ƒ n1 sT0    Y  t|dd��ø}t|dd��Ê}ttd	d
„ |ƒƒ}ttdd
„ |ƒƒ}	ttddƒ|ƒD ]4\}
}t	|	|d|
 f|
 d�}t
|| ƒdk s®J ‚q®tƒ }ttddƒ|ƒD ]8\}
}t	|	|d|
 f|
 |jd�}t
|| ƒdk súJ ‚qúW d   ƒ n1 �sJ0    Y  W d   ƒ n1 �sj0    Y  d S )Nzmodels/wmt15_eval/ref.ruzmodels/wmt15_eval/google.ruz#models/wmt15_eval/mteval-13a.outputéþÿÿÿr
   éÿÿÿÿÚutf8)Úencodingc                 S   s   |   ¡ S ©N©r   ©Úxr   r   r   Ú<lambda>÷   ó    z6TestBLEUvsMteval13a.test_corpus_bleu.<locals>.<lambda>c                 S   s
   |   ¡ gS rJ   rK   rL   r   r   r   rN   ù   rO   é
   r   )r*   g{®Gázt?)r*   r:   )r   ÚopenÚmapr   Ú	readlinesr   ÚlistÚzipr(   r   Úabsr   Zmethod3)r   Zref_fileZhyp_fileZmteval_output_fileZ
mteval_finZmteval_bleu_scoresZref_finZhyp_finr#   r   ÚiZmteval_bleuZ	nltk_bleuZ
chencherryr   r   r   Útest_corpus_bleuç   s.    
<ÿüz$TestBLEUvsMteval13a.test_corpus_bleuN)r3   r4   r5   rX   r   r   r   r   rE   æ   s   rE   c                   @   s   e Zd Zdd„ ZdS )ÚTestBLEUWithBadSentencec                 C   s”   d}t dƒ}| ¡ gg}| ¡ g}zD|  t¡�& | jt||ƒddd� W d   ƒ n1 sZ0    Y  W n( tyŽ   | jt||ƒddd� Y n0 d S )NzQTeo S yb , oe uNb , R , T t , , t Tue Ar saln S , , 5istsi l , 5oe R ulO sae oR Rz›Their tasks include changing a pump on the faulty stokehold .Likewise , two species that are very similar in morphology were distinguished using genetics .r   r   r   )r   r   r.   r/   r   r   r0   )r   ZhypÚrefr   Ú
hypothesesr   r   r   Ú"test_corpus_bleu_with_bad_sentence  s    ÿ
ÿ(ÿz:TestBLEUWithBadSentence.test_corpus_bleu_with_bad_sentenceN)r3   r4   r5   r\   r   r   r   r   rY     s   rY   c                   @   s   e Zd Zdd„ ZdS )ÚTestBLEUWithMultipleWeightsc                 C   sÜ   g d¢}g d¢}g d¢}g d¢}g d¢}g d¢}d}d}d	}	t |||g|gg||g|||	gd
�}
|
d t |||g|gg||g|ƒksˆJ ‚|
d t |||g|gg||g|ƒks°J ‚|
d t |||g|gg||g|	ƒksØJ ‚d S )N)ÚItÚisr   ÚguideÚtoÚactionÚwhichÚensuresÚthatÚtheÚmilitaryÚalwaysZobeysrf   ÚcommandsÚofrf   Úparty)r^   r_   r   r`   ra   rb   re   rd   re   rf   rg   ZwillÚforeverÚheedÚPartyri   )r^   r_   rf   ZguidingZ	principlerc   Z
guaranteesrf   rg   Zforcesrh   ZbeingZunderrf   Úcommandrj   rf   rn   )r^   r_   rf   Z	practicalr`   Úforrf   Zarmyrh   ra   rm   rf   Z
directionsrj   rf   rk   )ÚheÚreadrf   ÚbookÚbecauserq   ÚwasÚ
interestedÚinÚworldÚhistory)rq   ru   rv   rw   rx   ry   rt   rq   rr   rf   rs   )r
   r   r   r   )rA   rA   rA   rA   )r   r   r   r   r
   )Zlist_of_referencesr[   r*   r   r
   r   )r   )r   r   Zref1aZref1bZref1cr   Zref2aZweight_1Zweight_2Zweight_3Zbleu_scoresr   r   r   Ú&test_corpus_bleu_with_multiple_weights(  s.    ýÿ
ÿ
ÿzBTestBLEUWithMultipleWeights.test_corpus_bleu_with_multiple_weightsN)r3   r4   r5   rz   r   r   r   r   r]   '  s   r]   )Ú__doc__ZunittestÚnumpyrB   Z	nltk.datar   Znltk.translate.bleu_scorer   r   r   r   r   r   ZTestCaser	   r6   rE   rY   r]   r   r   r   r   Ú<module>   s    
 O+