
    /h=                        S r SSKrSSKrSSKJr  SSKJrJrJ	r	J
r
JrJr   " S S\R                  5      r " S S\R                  5      r " S	 S
\R                  5      r " S S\R                  5      r " S S\R                  5      rg)z.
Tests for BLEU translation evaluation metric
    N)find)SmoothingFunctionbrevity_penaltyclosest_ref_lengthcorpus_bleumodified_precisionsentence_bleuc                   2    \ rS rSrS rS rS rS rS rSr	g)	TestBLEU   c                 F   SR                  5       nSR                  5       nSR                  5       nX/n[        [        XCSS95      n[        US5      S:X  d   eU R	                  USSS	9  [        [        XCS
S95      S:X  d   e[        S5      R                  5       n[        S5      R                  5       n[        S5      R                  5       nSR                  5       nXU/n[        [        XCSS95      S:X  d   e[        [        XCS
S95      S:X  d   e[        S5      R                  5       n[        S5      R                  5       nXU/n[        [        XCSS95      n[        [        XGSS95      nU R	                  USSS	9  U R	                  USSS	9  [        US5      S:X  d   e[        US5      S:X  d   e[        [        XCS
S95      n	[        [        XGS
S95      n
U R	                  U	SSS	9  U R	                  U
SSS	9  [        U	S5      S:X  d   e[        U
S5      S:X  d   eg)zU
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              Z/var/www/auris/envauris/lib/python3.13/site-packages/nltk/test/unit/translate/test_bleu.pytest_modified_precision TestBLEU.test_modified_precision   s    ',,.*002,224\
 "''9*a'P!Q+Q/69995z!L '
A>?3FFF /
 %' 	 A
 %' 	 *
 %' 	
 ~~$'
'
A>?3FFF '
A>?3FFF 5
 %' 	 *
 %' 	
 $'
 "''9*a'P!Q!&'9*a'P!Q5z!L5z!L+Q/6999+Q/6999 !&&8Q&O P %&8Q&O P4jK4jK*A.&888*A.&888    c                    S/S-  S/S-  /nS/S-  n[        U5      n[        X5      nU R                  [        XC5      SSS9  S/S-  S/S-  S/S-  S/S-  /nS/S-  n[        U5      n[        X5      n[        XC5      S	:X  d   eg )
Na         gv?r   r      r   )lenr   r   r   )r   r   
hypothesishyp_lenclosest_ref_lens        r&   test_brevity_penaltyTestBLEU.test_brevity_penaltyd   s     ebj3%!),
UQY
j/,ZAO5va 	 	
 ebj3%!)cUQY	B
UQY
j/,ZA8C???r)   c                     SR                  5       /nSR                  5       n[        S[        U5      5       H  nSU-  4U-  n[        XU5      S:X  a  M   e   g )N7The candidate has no alignment to any of the referencesJohn loves Maryr   r   r   r   ranger0   r	   r   r   r1   r   weightss        r&   test_zero_matchesTestBLEU.test_zero_matchesu   s\    OUUWX
&,,.
 q#j/*AQwj1nG AQFFF +r)   c                     SR                  5       /nSR                  5       n[        S[        U5      5       H  nSU-  4U-  n[        XU5      S:X  a  M   e   g )Nr8   r   r   r9   r;   s        r&   test_full_matchesTestBLEU.test_full_matches   s\    '--/0
&,,.
 q#j/*AQwj1nG ASHHH +r)   c                     SR                  5       /nSR                  5       nU R                  [        X5      SSS9   U R                  [        [        X5        g ! [
         a     g f = f)Nr8   zJohn loves Mary who loves Miker   r   r   r   r   r	   assertWarnsUserWarningAttributeErrorr   r   r1   s      r&   5test_partial_matches_hypothesis_longer_than_reference>TestBLEU.test_partial_matches_hypothesis_longer_than_reference   sg    '--/0
5;;=
 	}ZDcRST	[-P 		   A 
A&%A& N)
__name__
__module____qualname____firstlineno__r'   r4   r=   r@   rH   __static_attributes__rK   r)   r&   r   r      s     M9^@"GI
r)   r   c                   >    \ rS rSrS rS rS rS rS rS r	S r
S	rg
)TestBLEUFringeCases   c                 z   SR                  5       /nSR                  5       n[        U5      S-   nSU-  4U-  nU R                  [        XU5      SSS9   U R	                  [
        [        X5        SR                  5       /nSR                  5       nU R                  [        XU5      SSS9  g ! [         a     NIf = f)NzJohn loves Mary ?r8   r   r   r   r   r   )r   r0   r   r	   rD   rE   rF   r;   s        r&   2test_case_where_n_is_bigger_than_hypothesis_lengthFTestBLEUFringeCases.test_case_where_n_is_bigger_than_hypothesis_length   s    )//12
&,,.

Oa7*q. 	*':C 	 	
	[-P (--/0
&,,.
 	*':C 	 	
  		s   B- -
B:9B:c                 L    SR                  5       /n/ n[        X5      S:X  d   eg )Nr7   r   r   r	   rG   s      r&   test_empty_hypothesis)TestBLEUFringeCases.test_empty_hypothesis   s+    OUUWX

Z4999r)   c                     SR                  5       /nS/n[        5       R                  n [        XUS9  g ! [         a     g f = f)Nr7   Foo)smoothing_function)r   r   method4r	   
ValueError)r   r   r1   r^   s       r&   test_length_one_hypothesis.TestBLEUFringeCases.test_length_one_hypothesis   sI    OUUWX
W
#%--	*WM 		s   
5 
AAc                 L    / /nSR                  5       n[        X5      S:X  d   eg )Nr8   r   rX   rG   s      r&   test_empty_references)TestBLEUFringeCases.test_empty_references   s*    T
&,,.
Z4999r)   c                 0    / /n/ n[        X5      S:X  d   eg )Nr   )r	   rG   s      r&   $test_empty_references_and_hypothesis8TestBLEUFringeCases.test_empty_references_and_hypothesis   s!    T

Z4999r)   c                     SR                  5       /nSR                  5       nU R                  [        X5      SSS9   U R                  [        [        X5        g ! [
         a     g f = f)Nz	let it goz	let go itr   r   r   rC   rG   s      r&   3test_reference_or_hypothesis_shorter_than_fourgramsGTestBLEUFringeCases.test_reference_or_hypothesis_shorter_than_fourgrams   si     "'')*
 &&(
 	}ZDcRST	[-P 		rJ   c                     SR                  5       /nSR                  5       n[        R                  " S/S-  5      n[        XU5      S:X  d   eg )Nr7   r8         ?r   r   )r   nparrayr	   )r   r   r1   r<   s       r&   test_numpy_weights&TestBLEUFringeCases.test_numpy_weights   sJ    OUUWX
&,,.
((D6A:&ZW=BBBr)   rK   N)rL   rM   rN   rO   rU   rY   r`   rc   rf   ri   ro   rP   rK   r)   r&   rR   rR      s&    
6:::Cr)   rR   c                       \ rS rSrS rSrg)TestBLEUvsMteval13a   c           
         [        S5      n[        S5      n[        S5      n[        U5       n[        [        UR	                  5       S   R                  5       SS 5      nS S S 5        [        USS9 n[        USS9 n[        [        S	 U5      5      n[        [        S
 U5      5      n	[        [        SS5      W5       H)  u  p[        XSU
-  4U
-  S9n[        X-
  5      S:  a  M)   e   [        5       n[        [        SS5      U5       H5  u  p[        U	USU
-  4U
-  UR                  S9n[        X-
  5      S:  a  M5   e   S S S 5        S S S 5        g ! , (       d  f       N= f! , (       d  f       N(= f! , (       d  f       g = f)Nzmodels/wmt15_eval/ref.ruzmodels/wmt15_eval/google.ruz#models/wmt15_eval/mteval-13a.outputr   utf8)encodingc                 "    U R                  5       $ Nr   xs    r&   <lambda>6TestBLEUvsMteval13a.test_corpus_bleu.<locals>.<lambda>   s
    	r)   c                 $    U R                  5       /$ rz   r{   r|   s    r&   r~   r      s    r)   
   r   )r<   g{Gzt?)r<   r]   )r   openmapr   	readlinesr   listzipr:   r   absr   method3)r   ref_filehyp_filemteval_output_file
mteval_finmteval_bleu_scoresref_finhyp_finr1   r   imteval_bleu	nltk_bleu
chencherrys                 r&   test_corpus_bleu$TestBLEUvsMteval13a.test_corpus_bleu   s{   2356!"GH $% "%UJ,@,@,B2,F,L,L,NqQS,T!U &
 (V,h0G "#&97"CD
!#&;W"EF
&)%2,8J&KNA +"q
Q!I {67%??? 'L /0
&)%2,8J&KNA +""!$q
Q+5+=+=	!I {67%??? 'L' 1 -, &% 10 -,s=   3E2E9=A(E()AE(E(E9
E%(
E6	2E99
FrK   N)rL   rM   rN   rO   r   rP   rK   r)   r&   rr   rr      s    '@r)   rr   c                       \ rS rSrS rSrg)TestBLEUWithBadSentencei  c                 L   Sn[        S5      nUR                  5       //nUR                  5       /n U R                  [        5         U R	                  [        X45      SSS9  S S S 5        g ! , (       d  f       g = f! [         a    U R	                  [        X45      SSS9   g f = f)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   rD   rE   r   r   rF   )r   hyprefr   
hypothesess        r&   "test_corpus_bleu_with_bad_sentence:TestBLEUWithBadSentence.test_corpus_bleu_with_bad_sentence  s    a2

 yy{m_
iik]
		W!!+.&&
7Q '  /.. 	W "";z#FTU"V	Ws/   A< A+"A< +
A95A< 9A< <$B#"B#rK   N)rL   rM   rN   rO   r   rP   rK   r)   r&   r   r     s    Wr)   r   c                       \ rS rSrS rSrg)TestBLEUWithMultipleWeightsi'  c                    / SQn/ SQn/ SQn/ SQn/ SQn/ SQnSnSnS	n	[        X#U/U//X/XxU	/S
9n
U
S   [        X#U/U//X/U5      :X  d   eU
S   [        X#U/U//X/U5      :X  d   eU
S   [        X#U/U//X/U	5      :X  d   eg )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   )rl   rl   rl   rl   )r   r   r   r   r   )list_of_referencesr   r<   r   r   r   )r   )r   r   ref1aref1bref1cr"   ref2aweight_1weight_2weight_3bleu_scoress              r&   &test_corpus_bleu_with_multiple_weightsBTestBLEUWithMultipleWeights.test_corpus_bleu_with_multiple_weights(  s    
(
$
(
$

  +"!!&u 5w?|2

 1~E"UG,tlH"
 
 	
 
 1~E"UG,tlH"
 
 	
 
 1~E"UG,tlH"
 
 	
 
r)   rK   N)rL   rM   rN   rO   r   rP   rK   r)   r&   r   r   '  s    x
r)   r   )__doc__unittestnumpyrm   	nltk.datar   nltk.translate.bleu_scorer   r   r   r   r   r	   TestCaser   rR   rr   r   r   rK   r)   r&   <module>r      s       x   FLC(++ LC^(@(++ (@VWh// W,y
("3"3 y
r)   