o
    .wi6                  .   @   s  d dl Z d dlmZ d dlmZmZmZmZmZm	Z	 d dl
Z
d dl
mZ d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ d dl m!Z! d dl"m#Z# d dl$m%Z% d dl&m'Z' d dl(m)Z) d dl*m+Z+ d dl,m-Z- d dl.m/Z/ d dl0m1Z1 ddgiZ2e/sddgZ3e4e5e	e5e4e5e	e6e5 e6e7 f f f f Z8e	e8e6e8 f Z9														 			d{d!e	e6e5 e4e5ef f d"e	e6e5 e4e5ef f d#ee5 d$ee7 d%e:d&ee d'ed(eeee4e5ef gef  d)e:d*e:d+ee	e5e
j;f  d,e7d-e7d.e7d/e:d0e5d1e:d2ee5 d3ee5 d4e4e5e	ee6e< e5f f f(d5dZ=			d|d!e	e5ee5 f d"ee	e5ee5 f  d6e7d7e:d8eee<  d4efd9d:Z>d!e	e5e6e5 f d"e	e5e6e5 f d4efd;d<Z?	=	>	?			d}d!e	e5ee5 f d"ee	e5ee5 f  d@e7dAe7dBe<dCe:dDe:dEe:d4e	ee@eef f fdFdGZA	 		?	H	I	Jd~d!e	e5ee5 f d"ee	e5ee5 f  dKedL dEe:dMe<dNe<dOe<dPe<d4e	ee@eef f fdQdRZB	S	T	U	V						 	V	dd!e	e5ee5 f d"e	e5ee5 f d#e	e5e jCf dWe<dXed*e:dMee< dBee< d+ee	e5e
j;f  d,ee7 d-e7d.e7d)e:dEe:d4e	ee@eef f fdYdZDd!e	e5e6e5 f d"e	e5e6e5 f d4efdZd[ZEdd!ed"ed\ee7 d4efd]d^ZF	_				`dd!e	e5ee5 f d"e	e5ee5 eee5  f daedb dce:ddeee5ge5f  deeee5gee5 f  dfe	e5e@e5dgf f d4e4e5ef fdhdZG			i		dd!ee5 d"eee5  d6e7d7e:djedk dCe:d8eee<  d4efdldmZHd!e	e4e5e5f e6e4e5e5f  f d"e9d4e4e5ef fdndoZI			V		dd!e	e5ee5 f d"ee	e5ee5 f  dpe:dqe:dCe:dre:dEe:d4e	ee@eee f f fdsdtZJd!e	e5e6e5 f d"e	e5e6e5 f d4efdudvZKd!e	e5e6e5 f d"e	e5e6e5 f d4efdwdxZLd!e	e5e6e5 f d"e	e5e6e5 f d4efdydzZMdS )    N)Sequence)AnyCallableListLiteralOptionalUnion)Tensor)Module)
bert_score)
bleu_score)char_error_rate)
chrf_score)extended_edit_distance)$_ALLOWED_INFORMATION_MEASURE_LITERAL)infolm)match_error_rate)
perplexity)rouge_score)sacre_bleu_score)squad)translation_edit_rate)word_error_rate)word_information_lost)word_information_preserved)_TRANSFORMERS_GREATER_EQUAL_4_4)_deprecated_root_import_func_rouge_scorenltk_bert_score_infolmF   @      enpredstargetmodel_name_or_path
num_layers
all_layersmodeluser_tokenizeruser_forward_fnverboseidfdevice
max_length
batch_sizenum_threadsreturn_hashlangrescale_with_baselinebaseline_pathbaseline_urlreturnc                 C   s   t dd tdi d| d|d|d|d|d|d	|d
|d|d|	d|
d|d|d|d|d|d|d|d|S )af  Wrapper for deprecated import.

    >>> preds = ["hello there", "general kenobi"]
    >>> target = ["hello there", "master kenobi"]
    >>> score = _bert_score(preds, target)
    >>> from pprint import pprint
    >>> pprint(score)
    {'f1': tensor([1.0000, 0.9961]),
     'precision': tensor([1.0000, 0.9961]),
     'recall': tensor([1.0000, 0.9961])}

    r   textr%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   N )r   r   )r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r:   r:   e/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/torchmetrics/functional/text/_deprecated.pyr   '   sP   
!	
n_gramsmoothweightsc                 C   s   t dd t| ||||dS )zWrapper for deprecated import.

    >>> preds = ['the cat is on the mat']
    >>> target = [['there is a cat on the mat', 'a cat is on the mat']]
    >>> _bleu_score(preds, target)
    tensor(0.7598)

    r   r9   r%   r&   r<   r=   r>   )r   r   r?   r:   r:   r;   _bleu_score`   s   
r@   c                 C      t dd t| |dS )zWrapper for deprecated import.

    >>> preds = ["this is the prediction", "there is an other sample"]
    >>> target = ["this is the reference", "there is another one"]
    >>> _char_error_rate(preds=preds, target=target)
    tensor(0.3415)

    r   r9   r%   r&   )r   r   rB   r:   r:   r;   _char_error_rates      
	rC                @n_char_ordern_word_orderbeta	lowercase
whitespacereturn_sentence_level_scorec              
   C   "   t dd t| |||||||dS )zWrapper for deprecated import.

    >>> preds = ['the cat is on the mat']
    >>> target = [['there is a cat on the mat', 'a cat is on the mat']]
    >>> _chrf_score(preds, target)
    tensor(0.8640)

    r   r9   r%   r&   rH   rI   rJ   rK   rL   rM   )r   r   rO   r:   r:   r;   _chrf_score      
rP   333333?皙?      ?language)r$   jaalpharhodeletion	insertionc              
   C   rN   )zWrapper for deprecated import.

    >>> preds = ["this is the prediction", "here is an other sample"]
    >>> target = ["this is the reference", "here is another one"]
    >>> _extended_edit_distance(preds=preds, target=target)
    tensor(0.3078)

    r   r9   r%   r&   rU   rM   rW   rX   rY   rZ   )r   r   r[   r:   r:   r;   _extended_edit_distance   rQ   r\   bert-base-uncased      ?kl_divergenceTtemperatureinformation_measurec                 C   s.   t dd t| |||||||||	|
|||dS )a<  Wrapper for deprecated import.

    >>> preds = ['he read the book because he was interested in world history']
    >>> target = ['he was interested in world history because he read the book']
    >>> _infolm(preds, target, model_name_or_path='google/bert_uncased_L-2_H-128_A-2', idf=False)
    tensor(-0.1784)

    r   r9   r%   r&   r'   r`   ra   r.   rW   rJ   r/   r0   r1   r2   r-   rM   )r   r   rb   r:   r:   r;   r       s"   
c                 C   rA   )zWrapper for deprecated import.

    >>> preds = ["this is the prediction", "there is an other sample"]
    >>> target = ["this is the reference", "there is another one"]
    >>> _match_error_rate(preds=preds, target=target)
    tensor(0.4444)

    r   r9   rB   )r   r   rB   r:   r:   r;   _match_error_rate   rD   rc   ignore_indexc                 C   s   t dd t| ||dS )zWrapper for deprecated import.

    >>> from torch import rand, randint
    >>> preds = rand(2, 8, 5)
    >>> target = randint(5, (2, 8))
    >>> target[0, 6:] = -100
    >>> _perplexity(preds, target, ignore_index=-100)
    tensor(5.8540)

    r   r9   r%   r&   rd   )r   r   re   r:   r:   r;   _perplexity   s   
rf   bestrouge1rouge2rougeL	rougeLsum
accumulate)avgrg   use_stemmer
normalizer	tokenizer
rouge_keys.c              	   C       t dd t| ||||||dS )a  Wrapper for deprecated import.

    >>> preds = "My name is John"
    >>> target = "Is your name John"
    >>> from pprint import pprint
    >>> pprint(_rouge_score(preds, target))
    {'rouge1_fmeasure': tensor(0.7500),
        'rouge1_precision': tensor(0.7500),
        'rouge1_recall': tensor(0.7500),
        'rouge2_fmeasure': tensor(0.),
        'rouge2_precision': tensor(0.),
        'rouge2_recall': tensor(0.),
        'rougeL_fmeasure': tensor(0.5000),
        'rougeL_precision': tensor(0.5000),
        'rougeL_recall': tensor(0.5000),
        'rougeLsum_fmeasure': tensor(0.5000),
        'rougeLsum_precision': tensor(0.5000),
        'rougeLsum_recall': tensor(0.5000)}

    r   r9   r%   r&   rm   ro   rp   rq   rr   )r   r   rt   r:   r:   r;   r     s   
13atokenize)noneru   zhintlcharc              	   C   rs   )zWrapper for deprecated import.

    >>> preds = ['the cat is on the mat']
    >>> target = [['there is a cat on the mat', 'a cat is on the mat']]
    >>> _sacre_bleu_score(preds, target)
    tensor(0.7598)

    r   r9   r%   r&   r<   r=   rv   rK   r>   )r   r   r{   r:   r:   r;   _sacre_bleu_score.     
r|   c                 C   rA   )a3  Wrapper for deprecated import.

    >>> preds = [{"prediction_text": "1976", "id": "56e10a3be3433e1400422b22"}]
    >>> target = [{"answers": {"answer_start": [97], "text": ["1976"]},"id": "56e10a3be3433e1400422b22"}]
    >>> _squad(preds, target)
    {'exact_match': tensor(100.), 'f1': tensor(100.)}

    r   r9   rB   )r   r   rB   r:   r:   r;   _squadK  rD   r~   	normalizeno_punctuationasian_supportc              	   C   rs   )zWrapper for deprecated import.

    >>> preds = ['the cat is on the mat']
    >>> target = [['there is a cat on the mat', 'a cat is on the mat']]
    >>> _translation_edit_rate(preds, target)
    tensor(0.1538)

    r   r9   r%   r&   r   r   rK   r   rM   )r   r   r   r:   r:   r;   _translation_edit_rateX  r}   r   c                 C   rA   )zWrapper for deprecated import.

    >>> preds = ["this is the prediction", "there is an other sample"]
    >>> target = ["this is the reference", "there is another one"]
    >>> _word_error_rate(preds=preds, target=target)
    tensor(0.5000)

    r   r9   rB   )r   r   rB   r:   r:   r;   _word_error_rateu  rD   r   c                 C   rA   )zWrapper for deprecated import.

    >>> preds = ["this is the prediction", "there is an other sample"]
    >>> target = ["this is the reference", "there is another one"]
    >>> _word_information_lost(preds, target)
    tensor(0.6528)

    r   r9   rB   )r   r   rB   r:   r:   r;   _word_information_lost  rD   r   c                 C   rA   )zWrapper for deprecated import.

    >>> preds = ["this is the prediction", "there is an other sample"]
    >>> target = ["this is the reference", "there is another one"]
    >>> _word_information_preserved(preds, target)
    tensor(0.3472)

    r   r9   rB   )r   r   rB   r:   r:   r;   _word_information_preserved  rD   r   )NNFNNNFFNr!   r"   r#   Fr$   FNN)r#   FN)rE   rF   rG   FFF)r$   FrG   rR   rS   rT   )r]   r^   r_   TNNNNr"   r   TF)N)rg   FNNrh   )r#   Fru   FN)FFTFF)Noscollections.abcr   typingr   r   r   r   r   r   torchr	   torch.nnr
   !torchmetrics.functional.text.bertr   !torchmetrics.functional.text.bleur    torchmetrics.functional.text.cerr   !torchmetrics.functional.text.chrfr    torchmetrics.functional.text.eedr   #torchmetrics.functional.text.infolmr   +_INFOLM_ALLOWED_INFORMATION_MEASURE_LITERALr    torchmetrics.functional.text.merr   'torchmetrics.functional.text.perplexityr   "torchmetrics.functional.text.rouger   'torchmetrics.functional.text.sacre_bleur   "torchmetrics.functional.text.squadr    torchmetrics.functional.text.terr    torchmetrics.functional.text.werr    torchmetrics.functional.text.wilr    torchmetrics.functional.text.wipr   torchmetrics.utilities.importsr   torchmetrics.utilities.printsr   __doctest_requires____doctest_skip__dictstrlistintSQUAD_SINGLE_TARGET_TYPESQUAD_TARGETS_TYPEboolr/   floatr   r@   rC   tuplerP   r\   PathLiker    rc   rf   r   r|   r~   r   r   r   r   r:   r:   r:   r;   <module>   s    
,	

<

.	
"	
"	

.+ 

,


:
..2