o
    yi]                     @   s   d dl mZmZmZmZmZmZmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZmZmZmZ d dlmZ ddgiZG d	d
 d
eZdS )    )AnyCallableDictListSequenceTupleUnion)Tensor)Literal)Metric)ALLOWED_ACCUMULATE_VALUESALLOWED_ROUGE_KEYS_rouge_score_compute_rouge_score_update)_NLTK_AVAILABLE)
ROUGEScorenltkc                       s   e Zd ZU dZdZeed< dZeed< dZeed< 						dd
ede	e
ge
f de	e
gee
 f ded dee
ee
df f def fddZdee
ee
 f dee
ee
 eee
  f ddfddZdee
ef fddZdefddZ  ZS )r   a
  `Calculate Rouge Score`_, used for automatic summarization.

    This implementation should imitate the behaviour of the ``rouge-score`` package `Python ROUGE Implementation`

    As input to ``forward`` and ``update`` the metric accepts the following input:

    - ``preds`` (:class:`~Sequence`): An iterable of predicted sentences or a single predicted sentence
    - ``target`` (:class:`~Sequence`): An iterable of target sentences
      or an iterable of interables of target sentences
      or a single target sentence

    As output of ``forward`` and ``compute`` the metric returns the following output:

    - ``rouge`` (:class:`~Dict`): A dictionary of tensor rouge scores for each input str rouge key

    Args:
        use_stemmer: Use Porter stemmer to strip word suffixes to improve matching.
        normalizer: A user's own normalizer function.
            If this is ``None``, replacing any non-alpha-numeric characters with spaces is default.
            This function must take a ``str`` and return a ``str``.
        tokenizer:
            A user's own tokenizer function. If this is ``None``, spliting by spaces is default
            This function must take a ``str`` and return ``Sequence[str]``
        accumulate:
            Useful in case of multi-reference rouge score.

            - ``avg`` takes the avg of all references with respect to predictions
            - ``best`` takes the best fmeasure score obtained between prediction and multiple corresponding references.

        rouge_keys: A list of rouge types to calculate.
            Keys that are allowed are ``rougeL``, ``rougeLsum``, and ``rouge1`` through ``rouge9``.
        kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.

    Example:
        >>> from torchmetrics.text.rouge import ROUGEScore
        >>> preds = "My name is John"
        >>> target = "Is your name John"
        >>> rouge = ROUGEScore()
        >>> from pprint import pprint
        >>> pprint(rouge(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)}


    Raises:
        ValueError:
            If the python packages ``nltk`` is not installed.
        ValueError:
            If any of the ``rouge_keys`` does not belong to the allowed set of keys.
    Fis_differentiableThigher_is_betterfull_state_updateNbestrouge1rouge2rougeL	rougeLsumuse_stemmer
normalizer	tokenizer
accumulate)avgr   
rouge_keys.kwargsc                    s   t  jdi | |sd|v rtstddd l}t|ts!|f}|D ]}|tvr3td| dt q#|t	vrBtd| dt	 || _
dd |D | _|rU|jj nd | _|| _|| _|| _| j
D ]}	d	D ]}
| j|	 d
|
 g d d qhqdd S )Nr   zUStemmer and/or `rougeLsum` requires that `nltk` is installed. Use `pip install nltk`.r   zGot unknown rouge key z. Expected to be one of zGot unknown accumulate value c                 S   s   g | ]}t | qS  )r   ).0keyr#   r#   K/home/ubuntu/.local/lib/python3.10/site-packages/torchmetrics/text/rouge.py
<listcomp>~   s    z'ROUGEScore.__init__.<locals>.<listcomp>fmeasure	precisionrecall_)dist_reduce_fxr#   )super__init__r   ModuleNotFoundErrorr   
isinstancetupler   
ValueErrorr   r!   rouge_keys_valuesstemporterPorterStemmerstemmerr   r   r   	add_state)selfr   r   r   r   r!   r"   r   r%   	rouge_keyscore	__class__r#   r&   r/   a   s8   	

zROUGEScore.__init__predstargetreturnc           	   	   C   s   t |trtdd |D rt |tr|gndd |D }t |tr%|g}t |tr.|gg}t||| j| j| j| j| j	d}|
 D ]$\}}|D ]}|
 D ]\}}t| d| d| || j qNqHqBdS )	z*Update state with predictions and targets.c                 s   s    | ]}t |tV  qd S N)r1   strr$   tgtr#   r#   r&   	<genexpr>   s    z$ROUGEScore.update.<locals>.<genexpr>c                 S   s   g | ]}|gqS r#   r#   rD   r#   r#   r&   r'      s    z%ROUGEScore.update.<locals>.<listcomp>)r8   r   r   r   rouger,   N)r1   listallrC   r   r4   r8   r   r   r   itemsgetattrappendtodevice)	r:   r?   r@   outputr;   metricsmetrictpvaluer#   r#   r&   update   s,   

	&zROUGEScore.updatec                 C   sH   i }| j D ]}dD ]}t| d| d| |d| d| < q	qt|S )zCCalculate (Aggregate and provide confidence intervals) ROUGE score.r(   rG   r,   )r4   rK   r   )r:   update_outputr;   rR   r#   r#   r&   compute   s   
(zROUGEScore.computec                 C   sH   | j jg}| jD ]}t| |}t|trt|}|| qtt|S rB   )	r>   __name__	_defaultsrK   r1   rH   r2   rL   hash)r:   	hash_valsr%   rS   r#   r#   r&   __hash__   s   



zROUGEScore.__hash__)FNNr   r   )rW   
__module____qualname____doc__r   bool__annotations__r   r   r   rC   r   r
   r   r   r   r/   rT   r   r	   rV   intr[   __classcell__r#   r#   r=   r&   r      s@   
 =(
	r   N)typingr   r   r   r   r   r   r   torchr	   typing_extensionsr
   torchmetricsr   "torchmetrics.functional.text.rouger   r   r   r   torchmetrics.utilities.importsr   __doctest_requires__r   r#   r#   r#   r&   <module>   s   $
