
    j                         d dl mZmZ d dlmZmZ d dlmZ 	 d dlZd dl	m
Z
  G d dej                        Zy# e$ r dZdZ
Y !w xY w)    )AnyOptional)base_metricscore_result)MetricComputationErrorN)vaderc                   f     e Zd ZdZ	 	 	 d
dededee   f fdZdedede	j                  fd	Z xZS )	Sentimenta  
    A metric that analyzes the sentiment of text using NLTK's VADER sentiment analyzer.

    Returns sentiment scores for positive, neutral, negative, and compound sentiment.
    The compound score is a normalized score between -1.0 (extremely negative) and
    1.0 (extremely positive).

    Args:
        name: The name of the metric. Defaults to "sentiment_metric".
        track: Whether to track the metric. Defaults to True.
        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 Sentiment
        >>> sentiment_metric = Sentiment()
        >>> result = sentiment_metric.score("I love this product! It's amazing.")
        >>> print(result.value)  # Compound score (e.g., 0.8802)
        >>> print(result.metadata)  # All sentiment scores
    nametrackproject_namec                    t         |   |||       t        t        t	        d      	 t        j
                         | _        y # t        $ r1 t        j                  d       t        j
                         | _        Y y w xY w)N)r   r   r   z`nltk` library is required for sentiment analysis. Install via `pip install nltk` and then download the vader_lexicon: `python -m nltk.downloader vader_lexicon`.vader_lexicon)	super__init__nltkr   ImportErrorSentimentIntensityAnalyzer	_analyzerLookupErrordownload)selfr   r   r   	__class__s       /Users/manta/Documents/Projects/TheRoad-I1/backend/.venv/lib/python3.12/site-packages/opik/evaluation/metrics/heuristics/sentiment.pyr   zSentiment.__init__$   s     	% 	 	
 <5== 	@"==?DN 	@MM/*"==?DN	@s   A 7B ?B outputignored_kwargsreturnc           	          |j                         st        d      | j                  j                  |      }|d   }|dk\  rd}n
|dk  rd}nd}t	        j
                  || j                  d| d	|d
d|      S )a  
        Analyze the sentiment of the provided text.

        Args:
            output: The text to analyze for sentiment.
            **ignored_kwargs: Additional keyword arguments that are ignored.

        Returns:
            score_result.ScoreResult: A ScoreResult object with:
                - value: The compound sentiment score (-1.0 to 1.0)
                - name: The metric name
                - reason: A brief explanation of the sentiment analysis
                - metadata: Dictionary containing all sentiment scores (pos, neu, neg, compound)

        Raises:
            MetricComputationError: If the input text is empty.
        z+Empty text provided for sentiment analysis.compoundg?positivegnegativeneutralzText sentiment analysis: z (compound score: z.4f))valuer   reasonmetadata)stripr   r   polarity_scoresr   ScoreResultr   )r   r   r   sentiment_scorescompound_scoresentiment_categorys         r   scorezSentiment.score>   s    $ ||~()VWW>>99&A)*5T!!+u$!+!*'' ./A.BBTUcdgThhij%	
 	
    )sentiment_metricTN)__name__
__module____qualname____doc__strboolr   r   r   r   r)   r-   __classcell__)r   s   @r   r
   r
      s\    . '&*	@@ @ sm	@4$
C $
3 $
<;S;S $
r.   r
   )typingr   r   opik.evaluation.metricsr   r   opik.exceptionsr   r   nltk.sentimentr   r   
BaseMetricr
    r.   r   <module>r=      sH      = 2$T
&& T
  DEs   
8 	AA