ó
ł`]c           @   sŚ   d  Z  d d l Z d d l Z d d l Z d d l m Z d d l m Z m Z m	 Z	 d d l m
 Z
 m Z m Z d e j f d     YZ d e j f d	     YZ d
 e j f d     YZ d e j f d     YZ d S(   s.   
Tests for BLEU translation evaluation metric
i’’’’N(   t   find(   t   modified_precisiont   brevity_penaltyt   closest_ref_length(   t   sentence_bleut   corpus_bleut   SmoothingFunctiont   TestBLEUc           B   s5   e  Z d    Z d   Z d   Z d   Z d   Z RS(   c         C   s©  d j    } d j    } d j    } | | g } t t | | d d  } t | d  d k sf t  |  j | d d	 d t t | | d d
  d k s£ t  t d  j    } t d  j    } t d  j    } d j    } | | | g } t t | | d d  d k st  t t | | d d
  d k sBt  t d  j    } t d  j    } | | | g } t t | | d d  } t t | | d d  } |  j | d d	 d |  j | d d	 d t | d  d k sņt  t | d  d k st  t t | | d d
  }	 t t | | d d
  }
 |  j |	 d d	 d |  j |
 d d	 d t |	 d  d k st  t |
 d  d k s„t  d S(   sl   
        Examples from the original BLEU paper
        http://www.aclweb.org/anthology/P02-1040.pdf
        s   the cat is on the mats   there is a cat on the mats   the the the the the the thet   ni   i   gę§čHŅ?gvi&$IŅ?t   placesi   g        sW   It is a guide to action that ensures that the military will forever heed Party commandssl   It is the guiding principle which guarantees the military forces always being under the command of the PartysQ   It is the practical guide for the army always to heed the directions of the partys   of theg      š?s^   It is a guide to action which ensures that the military always obeys the commands of the partysS   It 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(   t   splitt   floatR   t   roundt   AssertionErrort   assertAlmostEqualt   str(   t   selft   ref1t   ref2t   hyp1t
   referencest   hyp1_unigram_precisiont   ref3t   hyp2t   hyp2_unigram_precisiont   hyp1_bigram_precisiont   hyp2_bigram_precision(    (    sA   lib/python2.7/site-packages/nltk/test/unit/translate/test_bleu.pyt   test_modified_precision   sF    '''c         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Ņ t  d  S(
   Nt   ai   i   i   gvą„½ė?R	   i   i   g      š?(   t   lenR   R   R   R   (   R   R   t
   hypothesist   hyp_lent   closest_ref_len(    (    sA   lib/python2.7/site-packages/nltk/test/unit/translate/test_bleu.pyt   test_brevity_penaltyc   s    .c         C   sn   d j    g } d j    } xL t d t |   D]5 } d | g | } t | | |  d k s1 t  q1 Wd  S(   Ns7   The candidate has no alignment to any of the referencess   John loves Maryi   g      š?i    (   R
   t   rangeR   R   R   (   R   R   R   R   t   weights(    (    sA   lib/python2.7/site-packages/nltk/test/unit/translate/test_bleu.pyt   test_zero_matchest   s
    c         C   sn   d j    g } d j    } xL t d t |   D]5 } d | g | } t | | |  d k s1 t  q1 Wd  S(   Ns   John loves Maryi   g      š?(   R
   R"   R   R   R   (   R   R   R   R   R#   (    (    sA   lib/python2.7/site-packages/nltk/test/unit/translate/test_bleu.pyt   test_full_matches~   s
    c         C   sl   d j    g } d j    } |  j t | |  d d d y |  j t t | |  Wn t k
 rg n Xd  S(   Ns   John loves Marys   John loves Mary who loves Mikeg        R	   i   (   R
   R   R   t   assertWarnst   UserWarningt   AttributeError(   R   R   R   (    (    sA   lib/python2.7/site-packages/nltk/test/unit/translate/test_bleu.pyt5   test_partial_matches_hypothesis_longer_than_reference   s    (   t   __name__t
   __module__R   R!   R$   R%   R)   (    (    (    sA   lib/python2.7/site-packages/nltk/test/unit/translate/test_bleu.pyR      s
   	O		
	
t   TestBLEUFringeCasesc           B   s5   e  Z d    Z d   Z d   Z d   Z d   Z RS(   c         C   sĶ   d j    g } d j    } t |  d } d | g | } |  j t | | |  d d d y |  j t t | |  Wn t k
 r n Xd j    g } d j    } |  j t | | |  d d d d  S(   Ns   John loves Mary ?s   John loves Maryi   g      š?g        R	   i   (   R
   R   R   R   R&   R'   R(   (   R   R   R   R   R#   (    (    sA   lib/python2.7/site-packages/nltk/test/unit/translate/test_bleu.pyt2   test_case_where_n_is_bigger_than_hypothesis_length   s    c         C   s4   d j    g } g  } t | |  d k s0 t  d  S(   Ns7   The candidate has no alignment to any of the referencesi    (   R
   R   R   (   R   R   R   (    (    sA   lib/python2.7/site-packages/nltk/test/unit/translate/test_bleu.pyt   test_empty_hypothesis²   s    c         C   s4   g  g } d j    } t | |  d k s0 t  d  S(   Ns   John loves Maryi    (   R
   R   R   (   R   R   R   (    (    sA   lib/python2.7/site-packages/nltk/test/unit/translate/test_bleu.pyt   test_empty_referencesø   s    	c         C   s.   g  g } g  } t  | |  d k s* t  d  S(   Ni    (   R   R   (   R   R   R   (    (    sA   lib/python2.7/site-packages/nltk/test/unit/translate/test_bleu.pyt$   test_empty_references_and_hypothesis¾   s    	c         C   sl   d j    g } d j    } |  j t | |  d d d y |  j t t | |  Wn t k
 rg n Xd  S(   Ns	   let it gos	   let go itg        R	   i   (   R
   R   R   R&   R'   R(   (   R   R   R   (    (    sA   lib/python2.7/site-packages/nltk/test/unit/translate/test_bleu.pyt3   test_reference_or_hypothesis_shorter_than_fourgramsÄ   s    (   R*   R+   R-   R.   R/   R0   R1   (    (    (    sA   lib/python2.7/site-packages/nltk/test/unit/translate/test_bleu.pyR,      s
   				t   TestBLEUvsMteval13ac           B   s   e  Z d    Z RS(   c         C   s“  t  d  } t  d  } t  d  } t | d  - } t t | j   d j   d d ! } Wd  QXt j | d d d	 4} t j | d d d	 } t t d
   |   } t t d   |   }	 xb t t	 d d  |  D]H \ }
 } t
 |	 | d d |
 f |
 } t | |  d k  sį t  qį Wt   } xk t t	 d d  |  D]Q \ }
 } t
 |	 | d d |
 f |
 d | j } t | |  d k  sOt  qOWWd  QXWd  QXd  S(   Ns   models/wmt15_eval/ref.rus   models/wmt15_eval/google.rus#   models/wmt15_eval/mteval-13a.outputt   riž’’’i   i’’’’t   encodingt   utf8c         S   s
   |  j    S(   N(   R
   (   t   x(    (    sA   lib/python2.7/site-packages/nltk/test/unit/translate/test_bleu.pyt   <lambda>ä   t    c         S   s   |  j    g S(   N(   R
   (   R6   (    (    sA   lib/python2.7/site-packages/nltk/test/unit/translate/test_bleu.pyR7   ę   R8   i
   R#   g      š?g{®Gįzt?t   smoothing_function(   R    t   opent   mapR   t	   readlinesR
   t   iot   listt   zipR"   R   t   absR   R   t   method3(   R   t   ref_filet   hyp_filet   mteval_output_filet
   mteval_fint   mteval_bleu_scorest   ref_fint   hyp_finR   R   t   it   mteval_bleut	   nltk_bleut
   chencherry(    (    sA   lib/python2.7/site-packages/nltk/test/unit/translate/test_bleu.pyt   test_corpus_bleuŌ   s*    ,% 	%(   R*   R+   RM   (    (    (    sA   lib/python2.7/site-packages/nltk/test/unit/translate/test_bleu.pyR2   Ó   s   t   TestBLEUWithBadSentencec           B   s   e  Z d    Z RS(   c      	   C   s£   d } t  d  } | j   g g } | j   g } y9 |  j t  $ |  j t | |  d d d Wd  QXWn0 t k
 r |  j t | |  d d d n Xd  S(   NsQ   Teo S yb , oe uNb , R , T t , , t Tue Ar saln S , , 5istsi l , 5oe R ulO sae oR Rs   Their tasks include changing a pump on the faulty stokehold .Likewise , two species that are very similar in morphology were distinguished using genetics .g        R	   i   (   R   R
   R&   R'   R   R   R(   (   R   t   hypt   refR   t
   hypotheses(    (    sA   lib/python2.7/site-packages/nltk/test/unit/translate/test_bleu.pyt"   test_corpus_bleu_with_bad_sentence’   s    	#(   R*   R+   RR   (    (    (    sA   lib/python2.7/site-packages/nltk/test/unit/translate/test_bleu.pyRN   ž   s   (   t   __doc__t	   functoolsR=   t   unittestt	   nltk.dataR    t   nltk.translate.bleu_scoreR   R   R   R   R   R   t   TestCaseR   R,   R2   RN   (    (    (    sA   lib/python2.7/site-packages/nltk/test/unit/translate/test_bleu.pyt   <module>   s   =+