
    3fig                     ^    d dl Z d dlmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
  G d de
      Zy)	    N)Callable)Any)parse_json_markdown)override)StringEvaluatorc            
            e Zd ZdZ	 	 ddeeegef   dz  deegef   dz  deddf fdZe	e
defd              Ze	e
defd	              Ze	e
defd
              Zdedeez  dz  ez  ez  ez  ez  fdZe
	 ddededz  dedefd       Z xZS )JsonEditDistanceEvaluatora  An evaluator that calculates the edit distance between JSON strings.

    This evaluator computes a normalized Damerau-Levenshtein distance between two JSON strings
    after parsing them and converting them to a canonical format (i.e., whitespace and key order are normalized).
    It can be customized with alternative distance and canonicalization functions.

    Attributes:
        _string_distance (Callable[[str, str], float]): The internal distance computation function.
        _canonicalize (Callable[[Any], Any]): The internal canonicalization function.

    Examples:
        >>> evaluator = JsonEditDistanceEvaluator()
        >>> result = evaluator.evaluate_strings(
        ...     prediction='{"a": 1, "b": 2}', reference='{"a": 1, "b": 3}'
        ... )
        >>> assert result["score"] is not None

    Raises:
        ImportError: If `rapidfuzz` is not installed and no alternative `string_distance` function is provided.

    Nstring_distancecanonicalize_returnc                     t         |           ||| _        n"	 ddlm} |j                  j                  | _        ||| _        yd | _        y# t
        $ r}d}t        |      |d}~ww xY w)a  Initialize the JsonEditDistanceEvaluator.

        Args:
            string_distance: A callable that computes the distance between two strings.
                If not provided, a Damerau-Levenshtein distance from the `rapidfuzz`
                package will be used.
            canonicalize: A callable that converts a parsed JSON object into its
                canonical string form.
                If not provided, the default behavior is to serialize the JSON with
                sorted keys and no extra whitespace.

        Raises:
            ImportError: If the `rapidfuzz` package is not installed and no
                `string_distance` function is provided.
        Nr   )distancezThe default string_distance operator for the  JsonEditDistanceEvaluator requires installation of the rapidfuzz package. Please install it with `pip install rapidfuzz`.c                 2    t        j                  | dd      S )N),:T)
separators	sort_keys)jsondumps)xs    p/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_classic/evaluation/parsing/json_distance.py<lambda>z4JsonEditDistanceEvaluator.__init__.<locals>.<lambda>I   s    4::%,     )	super__init___string_distance	rapidfuzzr   ImportErrorDamerauLevenshteinnormalized_distance_canonicalize)selfr
   r   r   rfdemsg	__class__s          r   r   z"JsonEditDistanceEvaluator.__init__"   s    * 	&$3D!	.5 %($:$:$N$ND!#!-D"D  .F  "#&A-.s   A 	A+A&&A+c                      y)NF r#   s    r   requires_inputz(JsonEditDistanceEvaluator.requires_inputO   s     r   c                      y)NTr)   r*   s    r   requires_referencez,JsonEditDistanceEvaluator.requires_referenceT   s     r   c                      y)Njson_edit_distancer)   r*   s    r   evaluation_namez)JsonEditDistanceEvaluator.evaluation_nameY   s     $r   nodec                 <    t        |t              rt        |      S |S N)
isinstancestrr   )r#   r1   s     r   _parse_jsonz%JsonEditDistanceEvaluator._parse_json^   s    dC &t,,r   
prediction	referencekwargsc                     | j                  | j                  |            }| j                  | j                  |            }| j                  ||      }d|iS )Nscore)r"   r6   r   )r#   r7   r8   r9   parsedlabelr   s          r   _evaluate_stringsz+JsonEditDistanceEvaluator._evaluate_stringsc   sW     ##D$4$4Z$@A""4#3#3I#>?((7""r   )NNr3   )__name__
__module____qualname____doc__r   r5   floatr   r   propertyr   boolr+   r-   r0   dictlistintr6   r>   __classcell__)r'   s   @r   r	   r	      s=   0 ?C48+!3*e"34t;+ ucz*T1+ 	+
 
+Z     D    $ $  $ td(:U(BT(IC(ORU(U 
  !%	#	# :	# 		#
 
	# 	#r   r	   )r   collections.abcr   typingr   langchain_core.utils.jsonr   typing_extensionsr   #langchain_classic.evaluation.schemar   r	   r)   r   r   <module>rO      s%     $  9 & ?b# b#r   