
    j]                         d dl mZmZmZmZ d dlmZ d dlmZm	Z	 	 d dl
mZ  G d dej                        Zded	ed
edee   dee   f
dZy# e$ r dZY 3w xY w)    )AnyListOptionalUnion)MetricComputationError)base_metricscore_result)rouge_scorerNc                        e Zd ZdZ	 	 	 	 	 	 	 ddedededededee   dee   f fd	Zd
ede	ee
e   f   dedej                  fdZ xZS )ROUGEa9  
    A metric that computes the ROUGE, or Recall-Oriented Understudy for Gisting Evaluation score between an output and reference string mainly used for evaluating text summarization.
    ROUGE is case insensitive, meaning that upper case letters are treated the same way as lower case letters.
    This metrics is a wrapper around the Google Research reimplementation of ROUGE, which is based on the `rouge-score` library.

    References:
        - https://github.com/google-research/google-research/tree/master/rouge
        - https://huggingface.co/spaces/evaluate-metric/rouge

    Args:
        name: The name of the metric. Defaults to "rouge_metric".
        track: Whether to track the metric. Defaults to True.
        rouge_type: Type of ROUGE score to compute. Defaults to "rouge1". Must be one of the following:
                    - "rouge1": unigram (1-gram) based scoring
                    - "rouge2": bigram (2-gram) based scoring
                    - "rougeL": Longest common subsequence based scoring
                    - "rougeLSum": splits text using '\n'"
        use_stemmer: Whether to use stemming when computing ROUGE. Defaults to False.
        split_summaries: Whether to split summaries into sentences. Defaults to False.
        tokenizer: A tokenizer to use when splitting summaries into sentences. Defaults to None.
        project_name: Optional project name to track the metric in for the cases when there are no parent span/trace to inherit project name from.

    Example:
        >>> from opik.evaluation.metrics import ROUGE
        >>> rouge_metric = ROUGE()
        >>> result = rouge_metric.score(
        ...     output="The quick brown fox jumps over the lazy dog.",
        ...     reference="The quick brown fox jumps over the lazy dog."
        ... )
        >>> print(result.value)
        1.0
    nametrack
rouge_typeuse_stemmersplit_summaries	tokenizerproject_namec                     t         	|   |||       h d}||vrt        d| d| d      || _        t	        ||||      | _        y )N)r   r   r   >   rouge1rouge2rougeL	rougeLsumzInvalid rouge_type 'z'. Must be one of .r   r   r   r   )super__init__r   _rouge_type_build_rouge_backend_rouge)
selfr   r   r   r   r   r   r   valid_rouge_types	__class__s
            /Users/manta/Documents/Projects/TheRoad-I1/backend/.venv/lib/python3.12/site-packages/opik/evaluation/metrics/heuristics/rouge.pyr   zROUGE.__init__-   sm     	d%lKG..(&zl2DEVDWWXY  &*!#+	
    output	referenceignored_kwargsreturnc                 R   |j                         st        d      t        |t              r|j                         st        d      |g}nht        |t              rXt        |      dk(  rt        d      t        d |D              st        d      |D ]  }|j                         rt        d       | j                  }| j                  t        d      | j                  j                  ||      }||   j                  }t        j                  || j                  | d|d	
      S )a  
        Compute the ROUGE score based on the given rouge_type between the output and reference strings.

        Args:
            output: The output string to score.
            reference: The reference string or list of reference strings.
            **ignored_kwargs: Additional keyword arguments that are ignored.

        Returns:
            score_result.ScoreResult with:
              - `value`: The ROUGE score (float).
              - `name`: The metric name.
              - `reason`: A short explanation (e.g. "rouge1 score: 0.91").

        Raises:
            MetricComputationError:
                - If the candidate or any reference is empty.
                - If the reference is not a string or a list of strings.
        zCandidate is empty.zReference is empty.r   c              3   <   K   | ]  }t        |t                y w)N)
isinstancestr).0items     r#   	<genexpr>zROUGE.score.<locals>.<genexpr>l   s     Cz$,s   z0Reference must be a string or a list of strings.zEncountered empty reference.z!ROUGE backend is not initialized.z score: z.4f)valuer   reason)stripr   r+   r,   listlenallr   r   score_multifmeasurer	   ScoreResultr   )r    r%   r&   r'   ref_strrouge_score_typeresultsrouge_f1_values           r#   scorezROUGE.scoreG   s   2 ||~()>??i%??$,-BCC"I	4(9~",-BCCCCC,F  %}}01OPP %  ++;;()LMM++)))V< !12;;'' &'xs/CD
 	
r$   )rouge_metricTr   FFNN)__name__
__module____qualname____doc__r,   boolr   r   r   r   r   r	   r8   r=   __classcell__)r"   s   @r#   r   r      s    F #"! %#'&*

 
 	

 
 
 C=
 sm
48
8
 d3i(8
 	8

 
	!	!8
r$   r   r   r   r   r   r(   c                 X    t         t        d      t        j                  | g|||      S )Nzh`rouge-score` libraries are required for ROUGE score calculation. Install via `pip install rouge-score`.)r   r   r   )r
   ImportErrorRougeScorerr   s       r#   r   r      s?     5
 	

 ##	'	 r$   )typingr   r   r   r   opik.exceptionsr   opik.evaluation.metricsr   r	   rouge_scorer
   rF   
BaseMetricr   r,   rC   r    r$   r#   <module>rN      s    - - 2 =(
t
K"" t
n  	
 } c]w  Ls   A AA