
    Ng=                        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ej                  Z G d dej                  Z G d	 d
ej                  Z G d dej                  Z G 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                   ,    e Zd Zd Zd Zd Zd Zd ZdS )TestBLEUc                    d                                 }d                                 }d                                 }||g}t          t          ||d                    }t          |d          dk    sJ |                     |dd	           t          t          ||d
                    dk    sJ t          d                                           }t          d                                           }t          d                                           }d                                 }|||g}t          t          ||d                    dk    sJ t          t          ||d
                    dk    sJ t          d                                           }t          d                                           }|||g}t          t          ||d                    }t          t          ||d                    }|                     |dd	           |                     |dd	           t          |d          dk    sJ t          |d          dk    sJ t          t          ||d
                    }	t          t          ||d
                    }
|                     |	d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?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)selfref1ref2hyp1
referenceshyp1_unigram_precisionref3hyp2hyp2_unigram_precisionhyp1_bigram_precisionhyp2_bigram_precisions              ^/var/www/html/ai-engine/env/lib/python3.11/site-packages/nltk/test/unit/translate/test_bleu.pytest_modified_precisionz TestBLEU.test_modified_precision   sY    ',,..*0022,2244D\
 "''9*da'P'P'P!Q!Q+Q//699995z!LLL '
DA>>>??3FFFF /
 
 %'' 	 A
 
 %'' 	 *
 
 %'' 	
 ~~D$'
'
DA>>>??3FFFF '
DA>>>??3FFFF 5
 
 %'' 	 *
 
 %'' 	
 D$'
 "''9*da'P'P'P!Q!Q!&'9*da'P'P'P!Q!Q5z!LLL5z!LLL+Q//69999+Q//69999 !&&8TQ&O&O&O P P %&8TQ&O&O&O P P4jKKK4jKKK*A..&8888*A..&888888    c                 V   dgdz  dgdz  g}dgdz  }t          |          }t          ||          }|                     t          ||          dd           dgdz  dgdz  dgdz  dgdz  g}dgdz  }t          |          }t          ||          }t          ||          d	k    sJ d S )
Na         gv?r   r      r   )lenr   r   r   )r   r   
hypothesishyp_lenclosest_ref_lens        r%   test_brevity_penaltyzTestBLEU.test_brevity_penaltyd   s     ebj3%!),
UQY
j//,ZAAOW55va 	 	
 	
 	
 ebj3%!)cUQY	B
UQY
j//,ZAA88C??????r'   c                     d                                 g}d                                 }t          dt          |                    D ]"}d|z  f|z  }t          |||          dk    sJ #d S )N7The candidate has no alignment to any of the referencesJohn loves Maryr   r   r   r   ranger.   r	   r   r   r/   r   weightss        r%   test_zero_matcheszTestBLEU.test_zero_matchesu   s    OUUWWX
&,,..
 q#j//** 	G 	GAQwj1nG ZAAQFFFFF	G 	Gr'   c                     d                                 g}d                                 }t          dt          |                    D ]"}d|z  f|z  }t          |||          dk    sJ #d S )Nr5   r   r   r6   r8   s        r%   test_full_matcheszTestBLEU.test_full_matches   s    '--//0
&,,..
 q#j//** 	I 	IAQwj1nG ZAASHHHHH	I 	Ir'   c                    d                                 g}d                                 }|                     t          ||          dd           	 |                     t          t          ||           d S # t
          $ r Y d S w xY w)Nr5   zJohn loves Mary who loves Miker   r   r   r   r   r	   assertWarnsUserWarningAttributeErrorr   r   r/   s      r%   5test_partial_matches_hypothesis_longer_than_referencez>TestBLEU.test_partial_matches_hypothesis_longer_than_reference   s    '--//0
5;;==
 	}ZDDcRSTTT	[-ZPPPPP 	 	 	DD	   "A5 5
BBN)__name__
__module____qualname__r&   r2   r:   r<   rC    r'   r%   r   r      sl        M9 M9 M9^@ @ @"G G GI I I
 
 
 
 
r'   r   c                   8    e Zd Zd Zd Zd Zd Zd Zd Zd Z	dS )	TestBLEUFringeCasesc                    d                                 g}d                                 }t          |          dz   }d|z  f|z  }|                     t          |||          dd           	 |                     t
          t          ||           n# t          $ r Y nw xY wd                                 g}d                                 }|                     t          |||          dd           d S )NzJohn loves Mary ?r5   r   r   r   r   r   )r   r.   r   r	   r?   r@   rA   r8   s        r%   2test_case_where_n_is_bigger_than_hypothesis_lengthzFTestBLEUFringeCases.test_case_where_n_is_bigger_than_hypothesis_length   s   )//112
&,,..

OOa7*q. 	*j'::C 	 	
 	
 	
	[-ZPPPP 	 	 	D	
 (--//0
&,,..
 	*j'::C 	 	
 	
 	
 	
 	
s   -"B 
BBc                 `    d                                 g}g }t          ||          dk    sJ d S )Nr4   r   r   r	   rB   s      r%   test_empty_hypothesisz)TestBLEUFringeCases.test_empty_hypothesis   s<    OUUWWX

Z44999999r'   c                     d                                 g}dg}t                      j        }	 t          |||           d S # t          $ r Y d S w xY w)Nr4   Foo)smoothing_function)r   r   method4r	   
ValueError)r   r   r/   rS   s       r%   test_length_one_hypothesisz.TestBLEUFringeCases.test_length_one_hypothesis   sm    OUUWWX
W
#%%-	*jWMMMMMM 	 	 	DD	s   A 
AAc                 `    g g}d                                 }t          ||          dk    sJ d S )Nr5   r   rN   rB   s      r%   test_empty_referencesz)TestBLEUFringeCases.test_empty_references   s;    T
&,,..
Z44999999r'   c                 <    g g}g }t          ||          dk    sJ d S )Nr   )r	   rB   s      r%   $test_empty_references_and_hypothesisz8TestBLEUFringeCases.test_empty_references_and_hypothesis   s/    T

Z44999999r'   c                    d                                 g}d                                 }|                     t          ||          dd           	 |                     t          t          ||           d S # t
          $ r Y d S w xY w)Nz	let it goz	let go itr   r   r   r>   rB   s      r%   3test_reference_or_hypothesis_shorter_than_fourgramszGTestBLEUFringeCases.test_reference_or_hypothesis_shorter_than_fourgrams   s     "''))*
 &&((
 	}ZDDcRSTTT	[-ZPPPPP 	 	 	DD	rD   c                     d                                 g}d                                 }t          j        dgdz            }t          |||          dk    sJ d S )Nr4   r5         ?r   r   )r   nparrayr	   )r   r   r/   r9   s       r%   test_numpy_weightsz&TestBLEUFringeCases.test_numpy_weights   s_    OUUWWX
&,,..
(D6A:&&ZW==BBBBBBr'   N)
rE   rF   rG   rL   rO   rU   rW   rY   r[   r`   rH   r'   r%   rJ   rJ      s        
 
 
6: : :  : : :: : :  C C C C Cr'   rJ   c                       e Zd Zd ZdS )TestBLEUvsMteval13ac           	         t          d          }t          d          }t          d          }t          |          5 }t          t          |                                d                                         dd                   }d d d            n# 1 swxY w Y   t          |d          5 }t          |d          5 }t          t          d	 |                    }t          t          d
 |                    }	t          t          dd          |          D ]6\  }
}t          |	|d|
z  f|
z            }t          ||z
            dk     sJ 7t                      }t          t          dd          |          D ]<\  }
}t          |	|d|
z  f|
z  |j                  }t          ||z
            dk     sJ =	 d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Nzmodels/wmt15_eval/ref.ruzmodels/wmt15_eval/google.ruz#models/wmt15_eval/mteval-13a.outputr   utf8)encodingc                 *    |                                  S Nr   xs    r%   <lambda>z6TestBLEUvsMteval13a.test_corpus_bleu.<locals>.<lambda>   s    		 r'   c                 ,    |                                  gS ri   rj   rk   s    r%   rm   z6TestBLEUvsMteval13a.test_corpus_bleu.<locals>.<lambda>   s     r'   
   r   )r9   g{Gzt?)r9   rR   )r   openmapr   	readlinesr   listzipr7   r   absr   method3)r   ref_filehyp_filemteval_output_file
mteval_finmteval_bleu_scoresref_finhyp_finr/   r   imteval_bleu	nltk_bleu
chencherrys                 r%   test_corpus_bleuz$TestBLEUvsMteval13a.test_corpus_bleu   s
   233566!"GHH $%% 	V "%UJ,@,@,B,B2,F,L,L,N,NqQSt,T!U!U	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V
 (V,,, 	@h000 @G "#&9&97"C"CDD
!#&;&;W"E"EFF
&)%2,,8J&K&K @ @NA{ +"Jq
Q! ! !I {Y677%????? /00
&)%2,,8J&K&K @ @NA{ +""!$q
Q+5+=	! ! !I {Y677%?????@'@ @ @ @ @ @ @ @ @ @ @ @ @ @ @	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@sI   ABBB-G?C;G;GG	GG	GG#&G#N)rE   rF   rG   r   rH   r'   r%   rb   rb      s(        '@ '@ '@ '@ '@r'   rb   c                       e Zd Zd ZdS )TestBLEUWithBadSentencec                    d}t          d          }|                                gg}|                                g}	 |                     t                    5  |                     t          ||          dd           d d d            d S # 1 swxY w Y   d S # t          $ r* |                     t          ||          dd           Y d S w xY 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   r?   r@   r   r   rA   )r   hyprefr   
hypothesess        r%   "test_corpus_bleu_with_bad_sentencez:TestBLEUWithBadSentence.test_corpus_bleu_with_bad_sentence  s;   a2
 

 yy{{m_
iikk]
		W!!+..  &&
J77Q '                     	W 	W 	W "";z:#F#FTU"VVVVVV	Ws5   B 'B?B BB BB 0CCN)rE   rF   rG   r   rH   r'   r%   r   r     s(        W W W W Wr'   r   c                       e Zd Zd ZdS )TestBLEUWithMultipleWeightsc                 Z   g d}g d}g d}g d}g d}g d}d}d}d	}	t          |||g|gg||g|||	g
          }
|
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alwaysobeysr   commandsofr   party)r   r   r)   r   r   r   r   r   r   r   r   willforeverheedPartyr   )r   r   r   guiding	principler   
guaranteesr   r   forcesr   beingunderr   commandr   r   r   )r   r   r   	practicalr   forr   armyr   r   r   r   
directionsr   r   r   )hereadr   bookbecauser   was
interestedinworldhistory)r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   )r]   r]   r]   r]   )r   r   r   r   r   )list_of_referencesr   r9   r   r   r   )r   )r   r   ref1aref1bref1cr!   ref2aweight_1weight_2weight_3bleu_scoress              r%   &test_corpus_bleu_with_multiple_weightszBTestBLEUWithMultipleWeights.test_corpus_bleu_with_multiple_weights(  s   
 
 
(
 
 
$
 
 
(
 
 
$
 
 

 
 
  +"!!&u 5w?d|x2
 
 

 1~UE"UG,tTlH"
 "
 
 
 
 
 1~UE"UG,tTlH"
 "
 
 
 
 
 1~UE"UG,tTlH"
 "
 
 
 
 
 
 
r'   N)rE   rF   rG   r   rH   r'   r%   r   r   '  s(        x
 x
 x
 x
 x
r'   r   )__doc__unittestnumpyr^   	nltk.datar   nltk.translate.bleu_scorer   r   r   r   r   r	   TestCaser   rJ   rb   r   r   rH   r'   r%   <module>r      s                                 x    FLC LC LC LC LC(+ LC LC LC^(@ (@ (@ (@ (@(+ (@ (@ (@VW W W W Wh/ W W W,y
 y
 y
 y
 y
("3 y
 y
 y
 y
 y
r'   