
    j(                        d Z ddlmZ ddl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 eee   ee   gef   Z G d	 d
e
      Zy# e$ r dZY $w xY w)z6Character n-gram F-score (chrF/chrF++) metric wrapper.    )annotations)AnyCallableOptionalSequenceUnion)
BaseMetric)ScoreResult)MetricComputationError)
chrf_scoreNc                  v     e Zd ZdZ	 	 	 	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fdZ	 	 	 	 	 	 	 	 ddZ xZS )ChrFu  
    Compute chrF / chrF++ scores between a candidate string and references.

    By default the implementation delegates to ``nltk.translate.chrf_score`` and
    supports both chrF (character n-gram overlap) and chrF++ (when ``word_order``
    is non-zero). Scores range from `0.0` (no overlap) to `1.0` (perfect match).

    References:
      - Popović, "chrF: character n-gram F-score for automatic MT evaluation" (WMT 2015)
        https://aclanthology.org/W15-3049/
      - NLTK chrf_score module documentation
        https://www.nltk.org/api/nltk.translate.chrf_score.html
      - Hugging Face Evaluate: chrF metric overview
        https://huggingface.co/spaces/evaluate-metric/chrf

    Args:
        name: Display name for the metric result. Defaults to ``"chrf_metric"``.
        track: Whether to automatically track metric results. Defaults to ``True``.
        project_name: Optional tracking project name. Defaults to ``None``.
        beta: Weighting between precision and recall (``beta = 2`` is standard).
        ignore_whitespace: Whether whitespace is ignored before scoring.
        char_order: Maximum character n-gram order.
        word_order: Maximum word n-gram order (set ``>0`` to enable chrF++).
        lowercase: Whether to lowercase candidate and references prior to scoring.
        chrf_fn: Optional custom scoring callable for testing or offline usage.

    Example:
        >>> from opik.evaluation.metrics import ChrF
        >>> metric = ChrF(beta=2.0, char_order=6, lowercase=True)
        >>> result = metric.score(
        ...     output="The quick brown fox",
        ...     reference="The quick brown fox jumps",
        ... )
        >>> round(result.value, 4)  # doctest: +SKIP
        0.8795
    c
                     t            |||       | _        | _        | _        | _        | _        |	|	 _        y t        t        d      d fd}
|
 _        y )N)nametrackproject_namezfchrF metric requires the optional 'nltk' package. Install via `pip install nltk` or provide `chrf_fn`.c                    	 t        t        j                  || j                              S # t        $ r" t        t        j                  ||             cY S w xY w)N)beta)floatnltk_chrf_scoresentence_chrf_beta	TypeError)	candidate
referencesselfs     /Users/manta/Documents/Projects/TheRoad-I1/backend/.venv/lib/python3.12/site-packages/opik/evaluation/metrics/heuristics/chrf.py_computezChrF.__init__.<locals>._computeU   s\    
W '55&%!%  ! W !>!>z9!UVVWs   *. (AA)r   Sequence[str]r   r   returnr   )
super__init__r   _ignore_whitespace_char_order_word_order
_lowercase_chrf_fnr   ImportError)r   r   r   r   r   ignore_whitespace
char_order
word_order	lowercasechrf_fnr   	__class__s   `          r   r"   zChrF.__init__9   st     	d%lK
"3%%##DM&!@ 
W %DM    c                   |j                         st        d      t        |t              r|g}nt	        |      }|rt        d |D              rt        d      | j                  r.|j                         }|D cg c]  }|j                          }}n|}| j                  ||      }t        t        |      | j                  dt        |      d      S c c}w )Nz!Candidate is empty (chrF metric).c              3  >   K   | ]  }|j                            y w)N)strip).0refs     r   	<genexpr>zChrF.score.<locals>.<genexpr>p   s      GJSSYY[Js   z!Reference is empty (chrF metric).zchrF score: z.4f)valuer   reason)r2   r   
isinstancestrlistanyr&   lowerr'   r
   r   r   )r   output	referenceignored_kwargsr   output_textr4   r6   s           r   scorez
ChrF.scored   s     ||~()LMMi%#JiJS GJ GG()LMM?? ,,.K1;<##))+J< Kk:6,!%,s!34
 	
 =s   :C)	chrf_metricTNg       @F   r   FN)r   r9   r   boolr   zOptional[str]r   r   r)   rD   r*   intr+   rE   r,   rD   r-   zOptional[ChrFFn]r    None)r=   r9   r>   zUnion[str, Sequence[str]]r?   r   r    r
   )__name__
__module____qualname____doc__r"   rA   __classcell__)r.   s   @r   r   r      s    #N "&*"'$()%)% )% $	)%
 )%  )% )% )% )% ")% 
)%V

 -
 	

 

r/   r   )rJ   
__future__r   typingr   r   r   r   r   #opik.evaluation.metrics.base_metricr	   $opik.evaluation.metrics.score_resultr
   opik.exceptionsr   nltk.translater   r   r(   r9   r   ChrFFnr    r/   r   <module>rT      sd    < " ; ; : < 2< 
8C=(3-0%7	8l
: l
  Os   A AA