o
    .wi$                     @   s   d dl mZ d dl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mZ d dlmZmZ esEd	gZd
dgiZG dd deZdS )    )Sequence)AnyCallableOptionalUnion)Tensor)Literal)Metric)ALLOWED_ACCUMULATE_VALUESALLOWED_ROUGE_KEYS_rouge_score_compute_rouge_score_update)_MATPLOTLIB_AVAILABLE_NLTK_AVAILABLE)_AX_TYPE_PLOT_OUT_TYPEROUGEScore.plot)
ROUGEScorenltkc                       sF  e Zd ZU dZdZeed< dZeed< dZeed< dZ	e
ed< d	Ze
ed
< 					d%dedeeegef  deeegee f  ded deeeedf f dedd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	d&d!eeeee f  d"ee 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``, splitting 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_updateg        plot_lower_boundg      ?plot_upper_boundNbestrouge1rouge2rougeL	rougeLsumuse_stemmer
normalizer	tokenizer
accumulate)avgr   
rouge_keys.kwargsreturnc                    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(   T/home/ubuntu/sommelier/.venv/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+   r4   j   s8   	

zROUGEScore.__init__predstargetc           	   	   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)r6   strr)   tgtr(   r(   r+   	<genexpr>   s    z$ROUGEScore.update.<locals>.<genexpr>c                 S   s   g | ]}|gqS r(   r(   rG   r(   r(   r+   r,      s    z%ROUGEScore.update.<locals>.<listcomp>)r=   r!   r"   r#   rouger1   N)r6   listallrF   r   r9   r=   r!   r"   r#   itemsgetattrappendtodevice)	r?   rD   rE   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-   rJ   r1   )r9   rN   r   )r?   update_outputr@   rU   r(   r(   r+   compute   s   
(zROUGEScore.computec                 C   sH   | j jg}| jD ]}t| |}t|trt|}|| qtt|S )z>Return a unique hash for the specific instance of this metric.)	rC   __name__	_defaultsrN   r6   rK   r7   rO   hash)r?   	hash_valsr*   rV   r(   r(   r+   __hash__   s   



zROUGEScore.__hash__valaxc                 C   s   |  ||S )aF  Plot a single or multiple values from the metric.

        Args:
            val: Either a single result from calling `metric.forward` or `metric.compute` or a list of these results.
                If no value is provided, will automatically call `metric.compute` and plot that result.
            ax: An matplotlib axis object. If provided will add plot to that axis

        Returns:
            Figure and Axes object

        Raises:
            ModuleNotFoundError:
                If `matplotlib` is not installed

        .. plot::
            :scale: 75

            >>> # Example plotting a single value
            >>> from torchmetrics.text.rouge import ROUGEScore
            >>> metric = ROUGEScore()
            >>> preds = "My name is John"
            >>> target = "Is your name John"
            >>> metric.update(preds, target)
            >>> fig_, ax_ = metric.plot()

        .. plot::
            :scale: 75

            >>> # Example plotting multiple values
            >>> from torchmetrics.text.rouge import ROUGEScore
            >>> metric = ROUGEScore()
            >>> preds = "My name is John"
            >>> target = "Is your name John"
            >>> values = [ ]
            >>> for _ in range(10):
            ...     values.append(metric(preds, target))
            >>> fig_, ax_ = metric.plot(values)

        )_plot)r?   r_   r`   r(   r(   r+   plot   s   *r   )FNNr   r   )NN)rZ   
__module____qualname____doc__r   bool__annotations__r   r   r   floatr   r   r   rF   r   r   r   r7   r   r4   rW   dictr   rY   intr^   r   r   rb   __classcell__r(   r(   rB   r+   r   %   sX   
 >(
	r   N)collections.abcr   typingr   r   r   r   torchr   typing_extensionsr   torchmetricsr	   "torchmetrics.functional.text.rouger
   r   r   r   torchmetrics.utilities.importsr   r   torchmetrics.utilities.plotr   r   __doctest_skip____doctest_requires__r   r(   r(   r(   r+   <module>   s   
