o
    rZh=                     @   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s"J | j|ddd	 tt||d
ddks7J td  }td  }td  }d  }|||g}tt||dddks_J tt||d
ddkslJ 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sJ t|ddksJ tt||d
d}	tt||d
d}
| j|	ddd	 | j|
ddd	 t|	ddksJ t|
ddksJ 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?gJY8?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   Q/var/www/auris/lib/python3.10/site-packages/nltk/test/unit/translate/test_bleu.pytest_modified_precision   sd   

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sLJ 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 )N7The 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      
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   r,   zTestBLEU.test_full_matchesc                 C   T   d  g}d  }| jt||ddd z| tt|| W d S  ty)   Y d S w )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-   r4   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y5   Y nw d  g}d  }| jt|||ddd d S )NzJohn loves Mary ?r&   r
   r   r   r   r   )r   r"   r   r   r0   r1   r2   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   r3   r   r   r   test_empty_hypothesis   s   
z)TestBLEUFringeCases.test_empty_hypothesisc                 C   sB   d  g}dg}t j}z
t|||d W d S  ty    Y d S w )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   r:   r3   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   r3   r   r   r   $test_empty_references_and_hypothesis   s   z8TestBLEUFringeCases.test_empty_references_and_hypothesisc                 C   r.   )Nz	let it goz	let go itr   r   r   r/   r3   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sJ 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)
r5   r6   r7   r9   r;   r?   r@   rA   rB   rF   r   r   r   r   r8      s    
r8   c                   @      e Zd Zdd ZdS )TestBLEUvsMteval13ac              	   C   s  t d}t d}t d}t|}tt| d  dd }W d    n1 s*w   Y  t|dd}t|dde}ttd	d
 |}ttdd
 |}	ttdd|D ]\}
}t	|	|d|
 f|
 d}t
|| dk sqJ qWt }ttdd|D ]\}
}t	|	|d|
 f|
 |jd}t
|| dk sJ q}W d    n1 sw   Y  W d    d S W d    d S 1 sw   Y  d S )Nzmodels/wmt15_eval/ref.ruzmodels/wmt15_eval/google.ruz#models/wmt15_eval/mteval-13a.outputr
   utf8)encodingc                 S   s   |   S Nr   xr   r   r   <lambda>   s    z6TestBLEUvsMteval13a.test_corpus_bleu.<locals>.<lambda>c                 S   s
   |   gS rM   rN   rO   r   r   r   rQ      s   
 
   r   )r*   g{Gzt?)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)r5   r6   r7   rZ   r   r   r   r   rH          rH   c                   @   rG   )TestBLEUWithBadSentencec                 C   s   d}t d}| gg}| g}z%| t | jt||ddd W d    W d S 1 s/w   Y  W d S  tyK   | jt||ddd Y d S w )NzQTeo S yb , oe uNb , R , T t , , t Tue Ar saln S , , 5istsi l , 5oe R ulO sae oR RzTheir 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   r0   r1   r   r   r2   )r   Zhyprefr   
hypothesesr   r   r   "test_corpus_bleu_with_bad_sentence  s"   
&z:TestBLEUWithBadSentence.test_corpus_bleu_with_bad_sentenceN)r5   r6   r7   r_   r   r   r   r   r\     r[   r\   c                   @   rG   )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sDJ |
d t |||g|gg||g|ksXJ |
d t |||g|gg||g|	kslJ d S )N)Itisr   guidetoactionwhichensuresthatthemilitaryalwaysZobeysri   commandsofri   party)ra   rb   r   rc   rd   re   rh   rg   rh   ri   rj   ZwillforeverheedPartyrl   )ra   rb   ri   ZguidingZ	principlerf   Z
guaranteesri   rj   Zforcesrk   ZbeingZunderri   commandrm   ri   rq   )ra   rb   ri   Z	practicalrc   forri   Zarmyrk   rd   rp   ri   Z
directionsrm   ri   rn   )hereadri   bookbecausert   was
interestedinworldhistory)rt   rx   ry   rz   r{   r|   rw   rt   ru   ri   rv   )r
   r   r   r   )rC   rC   rC   rC   )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)r5   r6   r7   r}   r   r   r   r   r`   '  r[   r`   )__doc__ZunittestnumpyrD   Z	nltk.datar   Znltk.translate.bleu_scorer   r   r   r   r   r   ZTestCaser	   r8   rH   r\   r`   r   r   r   r   <module>   s     
 O+