
    VhA                     `   d dl mZmZ d dlmZmZmZmZ d dlm	Z	m
Z
 d dlmZ d dlmZ ej                   fdee   dee   deegef   d	eeeeef      eeeef      f   fd
Zdee   dee   d	eeeeef      eeeef      eeeef      f   fdZdeeeef      deeeef      d	eeeeef      eeeef      eeeef      f   fdZ e       fdeeeef      deeeef      ded	eeeeef      eeeef      eeeef      f   fdZ e       fdee   dee   ded	eeeeef      eeeef      eeeef      f   fdZe
fdeeeef      deeeef      de	d	eeeeef      eeeef      eeeef      f   fdZe
fdee   dee   de	d	eeeeef      eeeef      eeeef      f   fdZ e       e
fdeeeef      deeeef      dede	d	eeeeef      eeeef      eeeef      f   f
dZ e       e
fdee   dee   dede	d	eeeeef      eeeef      eeeef      f   f
dZdeeeef      d	efdZej                    e       e
dddfdee   dee   deegef   dede	dededed	efdZej                    e       e
dddfd eee      dee   deegef   dede	dededed	efd!Zy")#    )chainproduct)CallableIterableListTuple)WordNetCorpusReaderwordnet)StemmerI)PorterStemmer
hypothesis	reference
preprocessreturnc                     t        | t              rt        d|        t        |t              rt        d|       t        t	        t        ||                   }t        t	        t        ||                  }||fS )a7  
    Takes in pre-tokenized inputs for hypothesis and reference and returns
    enumerated word lists for each of them

    :param hypothesis: pre-tokenized hypothesis
    :param reference: pre-tokenized reference
    :preprocess: preprocessing method (default str.lower)
    :return: enumerated words list
    z?"hypothesis" expects pre-tokenized hypothesis (Iterable[str]): z="reference" expects pre-tokenized reference (Iterable[str]): )
isinstancestr	TypeErrorlist	enumeratemap)r   r   r   enum_hypothesis_listenum_reference_lists        K/home/dcms/DCMS/lib/python3.12/site-packages/nltk/translate/meteor_score.py_generate_enumsr      s     *c"Mj\Z
 	
 )S!KI;W
 	
  	#j**E FGyZ)CDE!444    c                 8    t        | |      \  }}t        ||      S )a  
    matches exact words in hypothesis and reference
    and returns a word mapping based on the enumerated
    word id between hypothesis and reference

    :param hypothesis: pre-tokenized hypothesis
    :param reference: pre-tokenized reference
    :return: enumerated matched tuples, enumerated unmatched hypothesis tuples,
             enumerated unmatched reference tuples
    )r   _match_enums)r   r   r   r   s       r   exact_matchr   /   s'     1@
I0V--,.ABBr   r   r   c                 4   g }t        t        |             ddd   D ]u  }t        t        |            ddd   D ]V  }| |   d   ||   d   k(  s|j                  | |   d   ||   d   f       | j                  |       |j                  |        u w || |fS )a  
    matches exact words in hypothesis and reference and returns
    a word mapping between enum_hypothesis_list and enum_reference_list
    based on the enumerated word id.

    :param enum_hypothesis_list: enumerated hypothesis list
    :param enum_reference_list: enumerated reference list
    :return: enumerated matched tuples, enumerated unmatched hypothesis tuples,
             enumerated unmatched reference tuples
    N   r   )rangelenappendpop)r   r   
word_matchijs        r   r   r   @   s     J3+,-dd3 s./026 	A#A&q)-@-CA-FF!!)!,Q/1DQ1G1JK %((+#''*	 +-@@@r   stemmerc                     | D cg c]  }|d   |j                  |d         f }}|D cg c]  }|d   |j                  |d         f }}t        ||      S c c}w c c}w )af  
    Stems each word and matches them in hypothesis and reference
    and returns a word mapping between enum_hypothesis_list and
    enum_reference_list based on the enumerated word id. The function also
    returns a enumerated list of unmatched words for hypothesis and reference.

    :param enum_hypothesis_list: enumerated hypothesis list
    :param enum_reference_list: enumerated reference list
    :param stemmer: nltk.stem.api.StemmerI object (default PorterStemmer())
    :return: enumerated matched tuples, enumerated unmatched hypothesis tuples,
             enumerated unmatched reference tuples
    r   r"   )stemr   )r   r   r*   	word_pairstemmed_enum_hypothesis_liststemmed_enum_reference_lists         r   _enum_stem_matchr0   [   s    $ EY$7@1w||IaL12$  $
 EX#7@1w||IaL12# # 46QRR$#s
    A Ac                 <    t        | |      \  }}t        |||      S )a  
    Stems each word and matches them in hypothesis and reference
    and returns a word mapping between hypothesis and reference

    :param hypothesis: pre-tokenized hypothesis
    :param reference: pre-tokenized reference
    :param stemmer: nltk.stem.api.StemmerI object (default PorterStemmer())
    :return: enumerated matched tuples, enumerated unmatched hypothesis tuples,
             enumerated unmatched reference tuples
    r*   )r   r0   )r   r   r*   r   r   s        r   
stem_matchr3   w   s)     1@
I0V--02EwWWr   r
   c           
         g }t        t        |             ddd   D ]  }t        t        j                  d  |j
                  | |   d         D                    j                  | |   d   h      }t        t        |            ddd   D ]O  }||   d   |v s|j                  | |   d   ||   d   f       | j                  |       |j                  |          || |fS )a^  
    Matches each word in reference to a word in hypothesis
    if any synonym of a hypothesis word is the exact match
    to the reference word.

    :param enum_hypothesis_list: enumerated hypothesis list
    :param enum_reference_list: enumerated reference list
    :param wordnet: a wordnet corpus reader object (default nltk.corpus.wordnet)
    Nr!   c              3   J   K   | ]  }d  |j                         D          yw)c              3      K   | ]6  }|j                         j                  d       dk  r|j                          8 yw)_r   N)namefind).0lemmas     r   	<genexpr>z3_enum_wordnetsyn_match.<locals>.<genexpr>.<genexpr>   s7      zz|((-1 JJLs   <>N)lemmas)r:   synsets     r   r<   z)_enum_wordnetsyn_match.<locals>.<genexpr>   s-        !'  s   !#r"   r   )	r#   r$   setr   from_iterablesynsetsunionr%   r&   )r   r   r
   r'   r(   hypothesis_synsr)   s          r   _enum_wordnetsyn_matchrD      s    J3+,-dd3    .goo.B1.Ea.HI  	
 %%a(+,
- 	 s./026 	A"1%a(O;!!)!,Q/1DQ1G1JK %((+#''*	& +-@@@r   c                 <    t        | |      \  }}t        |||      S )ae  
    Matches each word in reference to a word in hypothesis if any synonym
    of a hypothesis word is the exact match to the reference word.

    :param hypothesis: pre-tokenized hypothesis
    :param reference: pre-tokenized reference
    :param wordnet: a wordnet corpus reader object (default nltk.corpus.wordnet)
    :return: list of mapped tuples
    r
   )r   rD   )r   r   r
   r   r   s        r   wordnetsyn_matchrG      s-     1@
I0V--!17 r   c                     t        | |      \  }} }t        | ||      \  }} }t        | ||      \  }} }t        ||z   |z   d       | |fS )a  
    Aligns/matches words in the hypothesis to reference by sequentially
    applying exact match, stemmed match and wordnet based synonym match.
    in case there are multiple matches the match which has the least number
    of crossing is chosen. Takes enumerated list as input instead of
    string input

    :param enum_hypothesis_list: enumerated hypothesis list
    :param enum_reference_list: enumerated reference list
    :param stemmer: nltk.stem.api.StemmerI object (default PorterStemmer())
    :param wordnet: a wordnet corpus reader object (default nltk.corpus.wordnet)
    :return: sorted list of matched tuples, unmatched hypothesis list,
             unmatched reference list
    r2   rF   c                     | d   S )Nr    )wordpairs    r   <lambda>z#_enum_align_words.<locals>.<lambda>   s    XVW[ r   )key)r   r0   rD   sorted)r   r   r*   r
   exact_matchesstem_matcheswns_matchess          r   _enum_align_wordsrR      s    ( @L1@<M')< ?O17?;L&(; >T17>:K%':
 	L(;6<X	
 	 r   c                 >    t        | |      \  }}t        ||||      S )aY  
    Aligns/matches words in the hypothesis to reference by sequentially
    applying exact match, stemmed match and wordnet based synonym match.
    In case there are multiple matches the match which has the least number
    of crossing is chosen.

    :param hypothesis: pre-tokenized hypothesis
    :param reference: pre-tokenized reference
    :param stemmer: nltk.stem.api.StemmerI object (default PorterStemmer())
    :param wordnet: a wordnet corpus reader object (default nltk.corpus.wordnet)
    :return: sorted list of matched tuples, unmatched hypothesis list, unmatched reference list
    r*   r
   )r   rR   )r   r   r*   r
   r   r   s         r   align_wordsrU      s/    $ 1@
I0V--17G r   matchesc                     d}d}|t        |       dz
  k  rP| |dz      d   | |   d   dz   k(  r| |dz      d   | |   d   dz   k(  r|dz  }E|dz  }|dz  }|t        |       dz
  k  rP|S )ab  
    Counts the fewest possible number of chunks such that matched unigrams
    of each chunk are adjacent to each other. This is used to calculate the
    fragmentation part of the metric.

    :param matches: list containing a mapping of matched words (output of align_words)
    :return: Number of chunks a sentence is divided into post alignment
    r   r"   )r$   )rV   r(   chunkss      r   _count_chunksrY     s     	
AF
c'lQ
AEN1A!22AEN1A!22FA	Q! c'lQ
 Mr   g?g      @g      ?alphabetagammac                 ^   t        || |      \  }}	t        |      }
t        |	      }t        ||	||      \  }}}t        |      }	 t        |      |
z  }t        |      |z  }||z  ||z  d|z
  |z  z   z  }t        t	        |            }||z  }|||z  z  }d|z
  |z  S # t
        $ r Y yw xY w)a  
    Calculates METEOR score for single hypothesis and reference as per
    "Meteor: An Automatic Metric for MT Evaluation with HighLevels of
    Correlation with Human Judgments" by Alon Lavie and Abhaya Agarwal,
    in Proceedings of ACL.
    https://www.cs.cmu.edu/~alavie/METEOR/pdf/Lavie-Agarwal-2007-METEOR.pdf


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

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


    >>> round(single_meteor_score(reference1, hypothesis1),4)
    0.6944

        If there is no words match during the alignment the method returns the
        score as 0. We can safely  return a zero instead of raising a
        division by zero error as no match usually implies a bad translation.

    >>> round(single_meteor_score(['this', 'is', 'a', 'cat'], ['non', 'matching', 'hypothesis']),4)
    0.0

    :param reference: pre-tokenized reference
    :param hypothesis: pre-tokenized hypothesis
    :param preprocess: preprocessing function (default str.lower)
    :param stemmer: nltk.stem.api.StemmerI object (default PorterStemmer())
    :param wordnet: a wordnet corpus reader object (default nltk.corpus.wordnet)
    :param alpha: parameter for controlling relative weights of precision and recall.
    :param beta: parameter for controlling shape of penalty as a
                 function of as a function of fragmentation.
    :param gamma: relative weight assigned to fragmentation penalty.
    :return: The sentence-level METEOR score.
    )r   rT   r"   g        )r   r$   rR   floatrY   ZeroDivisionError)r   r   r   r*   r
   rZ   r[   r\   enum_hypothesisenum_referencetranslation_lengthreference_lengthrV   r7   matches_count	precisionrecallfmeanchunk_count	frag_fracpenaltys                        r   single_meteor_scorerk     s    X '6I*'#O^ _->*%'MGQ LM-(+==	}%(88V#	(9QY&<P(PQM'23-/	 io%GK5    s   A	B   	B,+B,
referencesc           	      D    t        fd| D              S )a	  
    Calculates METEOR score for hypothesis with multiple references as
    described in "Meteor: An Automatic Metric for MT Evaluation with
    HighLevels of Correlation with Human Judgments" by Alon Lavie and
    Abhaya Agarwal, in Proceedings of ACL.
    https://www.cs.cmu.edu/~alavie/METEOR/pdf/Lavie-Agarwal-2007-METEOR.pdf


    In case of multiple references the best score is chosen. This method
    iterates over single_meteor_score and picks the best pair among all
    the references for a given hypothesis

    >>> 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']

    >>> round(meteor_score([reference1, reference2, reference3], hypothesis1),4)
    0.6944

        If there is no words match during the alignment the method returns the
        score as 0. We can safely  return a zero instead of raising a
        division by zero error as no match usually implies a bad translation.

    >>> round(meteor_score([['this', 'is', 'a', 'cat']], ['non', 'matching', 'hypothesis']),4)
    0.0

    :param references: pre-tokenized reference sentences
    :param hypothesis: a pre-tokenized hypothesis sentence
    :param preprocess: preprocessing function (default str.lower)
    :param stemmer: nltk.stem.api.StemmerI object (default PorterStemmer())
    :param wordnet: a wordnet corpus reader object (default nltk.corpus.wordnet)
    :param alpha: parameter for controlling relative weights of precision and recall.
    :param beta: parameter for controlling shape of penalty as a function
                 of as a function of fragmentation.
    :param gamma: relative weight assigned to fragmentation penalty.
    :return: The sentence-level METEOR score.
    c              3   D   K   | ]  }t        |         yw))r   r*   r
   rZ   r[   r\   N)rk   )	r:   r   rZ   r[   r\   r   r   r*   r
   s	     r   r<   zmeteor_score.<locals>.<genexpr>  s=        	!		
 		
s    )max)rl   r   r   r*   r
   rZ   r[   r\   s    ```````r   meteor_scorerp   [  s'    d    $  r   N)	itertoolsr   r   typingr   r   r   r   nltk.corpusr	   r
   nltk.stem.apir   nltk.stem.porterr   r   lowerintr   r   r   r0   r3   rD   rG   rR   rU   rY   r^   rk   rp   rJ   r   r   <module>rx      sc   % 2 2 4 " * (+yy55}5 #$5 4c3h $uS#X"778	5:CC*23-C
4c3h $uS#X"7eCHo9NNOC"AuS#X/AeCHo.A 4c3h $uS#X"7eCHo9NNOA< &SuS#X/SeCHo.S S 4c3h $uS#X"7eCHo9NNO	S> &XX}X X 4c3h $uS#X"7eCHo9NNO	X, $+"AuS#X/"AeCHo."A !"A 4c3h $uS#X"7eCHo9NNO	"AP $+} ! 4c3h $uS#X"7eCHo9NNO	. &#*	&uS#X/&eCHo.& & !	&
 4c3h $uS#X"7eCHo9NNO&X &#*	}  !	
 4c3h $uS#X"7eCHo9NNO04c3h0 S 2 (+yy%#*>!}>!>! #$>! 	>!
 !>! >! >! >! >!H (+yy%#*>#'>> #$> 	>
 !> > > > >r   