§
    �zIf>  ã                   ó  — 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â?gñôJY†8î?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              úV/var/www/piapp/venv/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¸*ÀdÈaÐ'PÑ'PÔ'PÑ!QÔ!QÐÝÐ+¨QÑ/Ô/°6Ò9Ð9Ð9Ð9à×ÒÐ5°zÈ!ÐÑLÔLÐLõ Õ'¨
°D¸AÐ>Ñ>Ô>Ñ?Ô?À3ÒFÐFÐFÐFõ ð/ñ
ô 
÷ Š%‰'Œ'ð 	õ ðAñ
ô 
÷ Š%‰'Œ'ð 	õ ð*ñ
ô 
÷ Š%‰'Œ'ð 	ð
 �~Š~ÑÔˆà˜D $Ð'ˆ
åÕ'¨
°D¸AÐ>Ñ>Ô>Ñ?Ô?À3ÒFÐFÐFÐFõ Õ'¨
°D¸AÐ>Ñ>Ô>Ñ?Ô?À3ÒFÐFÐFÐFõ ð5ñ
ô 
÷ Š%‰'Œ'ð 	õ ð*ñ
ô 
÷ Š%‰'Œ'ð 	ð
 ˜D $Ð'ˆ
õ "'Õ'9¸*ÀdÈaÐ'PÑ'PÔ'PÑ!QÔ!QÐÝ!&Õ'9¸*ÀdÈaÐ'PÑ'PÔ'PÑ!QÔ!QÐà×ÒÐ5°zÈ!ÐÑLÔLÐLØ×ÒÐ5°zÈ!ÐÑLÔLÐLåÐ+¨QÑ/Ô/°6Ò9Ð9Ð9Ð9ÝÐ+¨QÑ/Ô/°6Ò9Ð9Ð9Ð9õ !&Õ&8¸ÀTÈQÐ&OÑ&OÔ&OÑ PÔ PÐÝ %Õ&8¸ÀTÈQÐ&OÑ&OÔ&OÑ PÔ PÐà×ÒÐ4°jÈÐÑKÔKÐKØ×ÒÐ4°jÈÐÑ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_penaltyc   sÛ   € ð �e˜b‘j 3 %¨!¡)Ð,ˆ
Ø�U˜Q‘Yˆ
Ý�j‘/”/ˆÝ,¨Z¸ÑAÔAˆØ×ÒÝ˜O¨WÑ5Ô5°vÀað 	ñ 	
ô 	
ð 	
ð �e˜b‘j 3 %¨!¡)¨c¨U°Q©Y¸¸À¹	ÐBˆ
Ø�U˜Q‘Yˆ
Ý�j‘/”/ˆÝ,¨Z¸ÑAÔAˆÝ˜°Ñ8Ô8¸CÒ?Ð?Ð?Ð?Ð?Ð?r'   c                 óØ   — d                      ¦   «         g}d                      ¦   «         }t          dt          |¦  «        ¦  «        D ]"}d|z  f|z  }t          |||¦  «        dk    sJ ‚Œ#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   Úweightss        r%   Útest_zero_matcheszTestBLEU.test_zero_matchest   s…   € àO×UÒUÑWÔWÐXˆ
Ø&×,Ò,Ñ.Ô.ˆ
õ �q�#˜j™/œ/Ñ*Ô*ð 	Gð 	GˆAØ˜Q‘w�j 1‘nˆGÝ  ¨Z¸ÑAÔAÀQÒ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Ø˜Q‘w�j 1‘nˆGÝ  ¨Z¸ÑAÔAÀSÒ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ÔDÀcÐRSÐÑTÔTÐTð	Ø×Ò�[­-¸ÀZÑPÔPÐPÐPÐPøÝð 	ð 	ð 	ØˆDˆDð	øøøó   Á"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                   ó2   — e 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ˆ
Ø&×,Ò,Ñ.Ô.ˆ
Ý�
‰OŒO˜aÑˆØ˜‘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ˆ
Ý#Ñ%Ô%Ô-ˆð	Ý˜* jÀWÐMÑMÔMÐMÐMÐMøÝð 	ð 	ð 	ØˆDˆDð	øøø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ÔDÀcÐRSÐÑTÔTÐTð	Ø×Ò�[­-¸ÀZÑPÔPÐPÐPÐPøÝð 	ð 	ð 	ØˆDˆDð	øøørD   N)	rE   rF   rG   rL   rO   rU   rW   rY   r[   rH   r'   r%   rJ   rJ   –   sn   € € € € € ð
ð 
ð 
ð6:ð :ð :ðð ð ð:ð :ð :ð:ð :ð :ðð ð ð ð 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.outputéþÿÿÿr   éÿÿÿÿÚutf8)Úencodingc                 ó*   — |                       ¦   «         S ©N©r   ©Úxs    r%   ú<lambda>z6TestBLEUvsMteval13a.test_corpus_bleu.<locals>.<lambda>î   s   € °·²±	´	€ r'   c                 ó,   — |                       ¦   «         gS rd   re   rf   s    r%   rh   z6TestBLEUvsMteval13a.test_corpus_bleu.<locals>.<lambda>ð   s   € °·²±´°€ r'   é
   r   )r9   g{®Gázt?)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¨õ "%¥U¨J×,@Ò,@Ñ,BÔ,BÀ2Ô,F×,LÒ,LÑ,NÔ,NÈqÐQSÈtÔ,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Ð&9¸7Ñ"CÔ"CÑDÔD�
å!¥#Ð&;Ð&;¸WÑ"EÔ"EÑFÔF�
å&)­%°°2©,¬,Ð8JÑ&KÔ&Kð @ð @‘N�A�{Ý +Ø" J¸¸q¹¸
ÀQ¹ð!ñ !ô !�Iõ ˜{¨YÑ6Ñ7Ô7¸%Ò?Ð?Ð?Ð?Ð?õ /Ñ0Ô0�
Ý&)­%°°2©,¬,Ð8JÑ&KÔ&Kð @ð @‘N�A�{Ý +Ø"Ø"Ø!$ 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%   r]   r]   Ý   s(   € € € € € ð'@ð '@ð '@ð '@ð '@r'   r]   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 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   rA   )r   ÚhypÚrefr   Ú
hypothesess        r%   Ú"test_corpus_bleu_with_bad_sentencez:TestBLEUWithBadSentence.test_corpus_bleu_with_bad_sentence	  s9  € ØaˆÝð2ñ
ô 
ˆð
 —y’y‘{”{�m�_ˆ
Ø—i’i‘k”k�]ˆ
ð	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   )Ú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Œ~¥Ø�U˜EÐ" U GÐ,¨t°T¨l¸Hñ"
ô "
ò 
ð 
ð 
ð 
ð ˜1Œ~¥Ø�U˜EÐ" U GÐ,¨t°T¨l¸Hñ"
ô "
ò 
ð 
ð 
ð 
ð ˜1Œ~¥Ø�U˜EÐ" U GÐ,¨t°T¨l¸Hñ"
ô "
ò 
ð 
ð 
ð 
ð 
ð 
r'   N)rE   rF   rG   r¹   rH   r'   r%   r†   r†     s(   € € € € € ðx
ð x
ð x
ð x
ð x
r'   r†   )Ú__doc__ÚioÚunittestÚ	nltk.datar   Únltk.translate.bleu_scorer   r   r   r   r   r	   ÚTestCaser   rJ   r]   r   r†   rH   r'   r%   ú<module>rÀ      s�  ððð ð 
€	€	€	Ø €€€à Ð Ð Ð Ð Ð ðð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ðð ð ð ð ˆxÔ ñ ô ð ðFDð Dð Dð Dð D˜(Ô+ñ Dô Dð DðN(@ð (@ð (@ð (@ð (@˜(Ô+ñ (@ô (@ð (@ðVWð Wð Wð Wð W˜hÔ/ñ Wô Wð Wð(y
ð y
ð y
ð y
ð y
 (Ô"3ñ y
ô y
ð y
ð y
ð y
r'   