
    j                        d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	 ddl
mZ 	 ddlZ G d d	e      Zy# e$ r dZY w xY w)
z:Readability heuristics backed by the ``textstat`` library.    )annotations)AnyOptional)
BaseMetric)ScoreResult)MetricComputationErrorNc            	      v     e Zd ZdZddddddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fdZ	 	 	 	 	 	 dd	Zdd
Z xZS )Readabilityu  Compute common readability statistics using ``textstat``.

    The metric reports the Flesch Reading Ease (0–100) alongside the Flesch–Kincaid
    grade level. The score value is the reading-ease score normalised to ``[0, 1]``.
    You can optionally enforce grade bounds to turn the metric into a guardrail.

    Args:
        name: Display name for the metric result.
        track: Whether to automatically track metric results.
        project_name: Optional tracking project name.
        min_grade: Inclusive lower bound for the acceptable grade.
        max_grade: Inclusive upper bound for the acceptable grade.
        language: Locale forwarded to ``textstat`` when counting syllables.
        textstat_module: Optional ``textstat``-compatible module for dependency
            injection (mainly used in tests).
        enforce_bounds: When ``True`` the metric returns ``1.0`` if the grade lies
            within bounds and ``0.0`` otherwise, effectively acting as a guardrail.
    readability_metricTNen_USF)nametrackproject_name	min_grade	max_gradelanguagetextstat_moduleenforce_boundsc                   t         	|   |||       ||| _        nt        t	        d      t        | _        || _        || _        || _        || _        y )N)r   r   r   z`Readability metric requires the optional 'textstat' package. Install via `pip install textstat`.)	super__init__	_textstat_textstat_libImportError
_min_grade
_max_grade	_language_enforce_bounds)
selfr   r   r   r   r   r   r   r   	__class__s
            /Users/manta/Documents/Projects/TheRoad-I1/backend/.venv/lib/python3.12/site-packages/opik/evaluation/metrics/heuristics/readability.pyr   zReadability.__init__%   sd     	d%lK&,DN$!:  +DN##!-    c                   |r|j                         st        d      |j                         }| j                  j                  |      }| j                  j	                  |d      }|dk  s|dk  rt        d      | j                  j                  || j                        }t        | j                  j                  |            }t        | j                  j                  |            }||z  }	|r||z  nd}
| j                  |      }| j                  r|rdnd}|rd	nd
}n%t        dt        d|            dz  }|}d|dd|d}|||	|
|||| j                  | j                  |d
}t!        || j"                  ||      S )Nz#Text is empty (Readability metric).T)removepunctr   z-Unable to parse text for readability metrics.)langg        g      ?zText meets readability targetsz&Text falls outside readability targetsg      Y@zFlesch Reading Ease: z.2fz | Flesch-Kincaid Grade: )
flesch_reading_easeflesch_kincaid_gradewords_per_sentencesyllables_per_wordsentence_count
word_countsyllable_countr   r   within_grade_bounds)valuer   reasonmetadata)stripr   r   r*   lexicon_countr,   r   floatr&   r'   _is_within_grade_boundsr   maxminr   r   r   r   )r   outputignored_kwargscleanedr*   r+   r,   reading_easefk_grader(   r)   within_boundsr.   r/   
normalisedr0   s                   r!   scorezReadability.scoreA   s   
 V\\^()NOO,,.66w?^^11't1L
Q*/(?  66wT^^6TT^^??HI<<WEF'.8<F^j8C44X>(CcE ! 1=  S#e\":;eCJE'S'9 :))1#8  $0$,"4"4,$,#0
 	
 	
r"   c                t    | j                   || j                   k  ry| j                  || j                  kD  ryy)NFT)r   r   )r   grades     r!   r4   z#Readability._is_within_grade_bounds|   s5    ??&54??+B??&54??+Br"   )r   strr   boolr   zOptional[str]r   Optional[float]r   rC   r   rA   r   zOptional[Any]r   rB   returnNone)r7   rA   r8   r   rD   r   )r@   r3   rD   rB   )__name__
__module____qualname____doc__r   r>   r4   __classcell__)r    s   @r!   r
   r
      s    , )&*%)%))-$. . 	.
 $. #. #. . '. . 
.89
9
 9
 
	9
vr"   r
   )rI   
__future__r   typingr   r   #opik.evaluation.metrics.base_metricr   $opik.evaluation.metrics.score_resultr   opik.exceptionsr   textstatr   r   r
    r"   r!   <module>rR      sC    @ "   : < 2$
p* p	  Ms   4 >>