
    i                     J    S r SSKrSSKrSSKJr  SSKJr  SS jrSS jrS r	g)	zNIST score implementation.    N)Counter)ngramsc                      [        U /U/U5      $ )aW  
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      S/home/james-whalen/.local/lib/python3.13/site-packages/nltk/translate/nist_score.pysentence_nistr      s    h 
|j\155    c           	      x  ^ [        U 5      [        U5      :X  d   S5       e[        5       nSnU  HJ  nU HA  n[        SUS-   5       H  nUR                  [	        Xg5      5        M     U[        U5      -  nMC     ML     0 mU H:  nUSS n	U	(       a
  X;   a  X9   n
OUn
[
        R                  " XU   -  S5      TU'   M<     [        5       n[        5       nSu  p[        SUS-   5       GH*  n[        X5       GH  u  p_[        U5      n/ nU H  n[        U5      n[        U5      U:  a  [        [	        X5      5      O	[        5       n[        U5      U:  a  [        [	        Xg5      5      O	[        5       nUU-  n[        U4S jUR                  5        5       5      n[        UR                  5       5      nUS:X  a  SOUU-  nUR                  UUUU45        M     [        U5      u  nn
nnX==   U
-  ss'   X==   U-  ss'   UU-  nUU-  nGM     GM-     SnU H  nX   X   -  nUU-  nM     U[        X5      -  $ )	au  
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   8   >#    U  H  u  pTU   U-  v   M     g 7f)N ).0_ngramcountinformation_weightss      r
   	<genexpr>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_!!&"67 %!S^3!	 $ 
  f*"*I-I&*hhyf;M/Mq&QF#  *1&+29(LE1a!e_&)*<&I"J*oG "$'	y> :!+ F:12   69^q5HGF901gi  ",j!8  !)7)=)=)?! 
  #:#4#4#67".!"3Ql9R
"))\8D' (. :==O9P6Iy+w.1Y>103{B3WEWEA 'J J N/.1256 	 	)# 0 /===r   c                    X-  nSUs=:  a  S:  ag  O  OdSu  p4[         R                  " U5      [         R                  " U5      S-  -  n[         R                  " U[         R                  " U5      S-  -  5      $ [        [	        US5      S5      $ )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<   r1   ratioratio_xscore_xbetas         r
   r$   r$      st      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>rJ      s(    !    46na>H)r   