
    g                     F    d Z ddlZddlZddlmZ ddlmZ ddZddZd Z	y)	zNIST score implementation.    N)Counter)ngramsc                      t        | g|g|      S )a  
    Calculate NIST score from
    George Doddington. 2002. "Automatic evaluation of machine translation quality
    using n-gram co-occurrence statistics." Proceedings of HLT.
    Morgan Kaufmann Publishers Inc. https://dl.acm.org/citation.cfm?id=1289189.1289273

    DARPA commissioned NIST to develop an MT evaluation facility based on the BLEU
    score. The official script used by NIST to compute BLEU and NIST score is
    mteval-14.pl. The main differences are:

     - BLEU uses geometric mean of the ngram overlaps, NIST uses arithmetic mean.
     - NIST has a different brevity penalty
     - NIST score from mteval-14.pl has a self-contained tokenizer

    Note: The mteval-14.pl includes a smoothing function for BLEU score that is NOT
          used in the NIST score computation.

    >>> hypothesis1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which',
    ...               'ensures', 'that', 'the', 'military', 'always',
    ...               'obeys', 'the', 'commands', 'of', 'the', 'party']

    >>> hypothesis2 = ['It', 'is', 'to', 'insure', 'the', 'troops',
    ...               'forever', 'hearing', 'the', 'activity', 'guidebook',
    ...               'that', 'party', 'direct']

    >>> reference1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'that',
    ...               'ensures', 'that', 'the', 'military', 'will', 'forever',
    ...               'heed', 'Party', 'commands']

    >>> reference2 = ['It', 'is', 'the', 'guiding', 'principle', 'which',
    ...               'guarantees', 'the', 'military', 'forces', 'always',
    ...               'being', 'under', 'the', 'command', 'of', 'the',
    ...               'Party']

    >>> reference3 = ['It', 'is', 'the', 'practical', 'guide', 'for', 'the',
    ...               'army', 'always', 'to', 'heed', 'the', 'directions',
    ...               'of', 'the', 'party']

    >>> sentence_nist([reference1, reference2, reference3], hypothesis1) # doctest: +ELLIPSIS
    3.3709...

    >>> sentence_nist([reference1, reference2, reference3], hypothesis2) # doctest: +ELLIPSIS
    1.4619...

    :param references: reference sentences
    :type references: list(list(str))
    :param hypothesis: a hypothesis sentence
    :type hypothesis: list(str)
    :param n: highest n-gram order
    :type n: int
    )corpus_nist)
references
hypothesisns      N/var/www/openai/venv/lib/python3.12/site-packages/nltk/translate/nist_score.pysentence_nistr      s    h 
|j\155    c           	      Z   t        |       t        |      k(  sJ d       t               }d}| D ]F  }|D ]?  }t        d|dz         D ]  }|j                  t	        ||              |t        |      z  }A H i |D ]4  }|dd }	|	r
|	|v r||	   }
n|}
t        j                  |
||   z  d      |<   6 t               }t               }d\  }}t        d|dz         D ]'  }t        | |      D ]  \  }}t        |      }g }|D ]  }t        |      }t        |      |k\  rt        t	        ||            n	t               }t        |      |k\  rt        t	        ||            n	t               }||z  }t        fd|j                         D              }t        |j                               }|dk(  rdn||z  }|j                  ||||f        t        |      \  }}
}}||xx   |
z  cc<   ||xx   |z  cc<   ||z  }||z  } * d}|D ]  }||   ||   z  }||z  } |t        ||      z  S )	a  
    Calculate a single corpus-level NIST score (aka. system-level BLEU) for all
    the hypotheses and their respective references.

    :param references: a corpus of lists of reference sentences, w.r.t. hypotheses
    :type references: list(list(list(str)))
    :param hypotheses: a list of hypothesis sentences
    :type hypotheses: list(list(str))
    :param n: highest n-gram order
    :type n: int
    zBThe number of hypotheses and their reference(s) should be the samer      N   )r   r   c              3   4   K   | ]  \  }}|   |z    y w)N ).0_ngramcountinformation_weightss      r
   	<genexpr>zcorpus_nist.<locals>.<genexpr>   s'      !)? (/%7)?s   )lenr   rangeupdater   mathlogzipsumitemsvaluesappendmaxnist_length_penalty)list_of_references
hypothesesr	   
ngram_freqtotal_reference_wordsr   	referenceir   _mgram	numerator"nist_precision_numerator_per_ngram$nist_precision_denominator_per_ngraml_refl_sysr   hyp_lennist_score_per_ref_ref_len
hyp_ngrams
ref_ngramsngram_overlaps
_numerator_denominator
_precision	precisiondenominatorref_lennist_precisionr   s                                @r
   r   r   I   s    !"c'  LKL 
 J 
 	#I1a!e_!!&A"67 %!S^3!	 $ 
  f
*"6*I-I&*hhy:f;M/Mq&QF#  *1&+29(LE51a!e_&)*<j&I"J
*oG "$'	y> :!+ F:q12   69^q5HGF9a01gi  ",j!8  !)7)=)=)?! 
  #:#4#4#67".!"3Ql9R
"))\8D' (. :==O9P6Iy+w.q1Y>103{B3WEWEA 'J J N/.q12156 	 	)# 0 /u===r   c                    || z  }d|cxk  rdk  rdn nad\  }}t        j                  |      t        j                  |      dz  z  }t        j                  |t        j                  |      dz  z        S t        t	        |d      d      S )a"  
    Calculates the NIST length penalty, from Eq. 3 in Doddington (2002)

        penalty = exp( beta * log( min( len(hyp)/len(ref) , 1.0 )))

    where,

        `beta` is chosen to make the brevity penalty factor = 0.5 when the
        no. of words in the system output (hyp) is 2/3 of the average
        no. of words in the reference translation (ref)

    The NIST penalty is different from BLEU's such that it minimize the impact
    of the score of small variations in the length of a translation.
    See Fig. 4 in  Doddington (2002)
    r   r   )g      ?g      ?r   g      ?g        )r   r   expr"   min)r;   r0   ratioratio_xscore_xbetas         r
   r#   r#      sx      gE5}1}#xx 488G#4#99xxtxx!33443uc?C((r   )   )
__doc__	fractionsr   collectionsr   	nltk.utilr   r   r   r#   r   r   r
   <module>rI      s(    !    46na>H)r   