o
    ҷh                     @  s   d dl mZ d dlmZmZmZ d dlmZ d dlm	Z	 dd
dZ
ddddddZddddddZddddddZddddddZdS )    )annotations)CallableHashableSequence)conv_sequences)is_nones1Sequence[Hashable]s2returnintc                 C  s  t t| t|d }i }|j}t|d }|g| }|g| }tt|}||d< tdt| d D ]}	||}}d}
|d }|	|d< |}tdt|d D ]y}||d  | |	d  ||d  k }||d  d }|| d }t|||}| |	d  ||d  kr|}
||d  ||< |}n0|||d  d}|
}|| dkr|| |	|  }t||}n|	| dkr|||  }t||}|| }|||< qO|	|| |	d  < q3|t| S )N      r   )maxlengetlistrangemin)r   r
   maxVallast_row_idlast_row_id_getsizeFRR1Rilast_col_id	last_i2l1Tjdiagleftuptempkl	transpose r)   [/var/www/html/venv/lib/python3.10/site-packages/rapidfuzz/distance/DamerauLevenshtein_py.py"_damerau_levenshtein_distance_zhao   sF   


$

r+   N)	processorscore_cutoffr,   (Callable[..., Sequence[Hashable]] | Noner-   
int | Nonec                C  sL   |dur|| } ||}t | |\} }t| |}|du s ||kr"|S |d S )a  
    Calculates the Damerau-Levenshtein distance.

    Parameters
    ----------
    s1 : Sequence[Hashable]
        First string to compare.
    s2 : Sequence[Hashable]
        Second string to compare.
    processor: callable, optional
        Optional callable that is used to preprocess the strings before
        comparing them. Default is None, which deactivates this behaviour.
    score_cutoff : int, optional
        Maximum distance between s1 and s2, that is
        considered as a result. If the distance is bigger than score_cutoff,
        score_cutoff + 1 is returned instead. Default is None, which deactivates
        this behaviour.

    Returns
    -------
    distance : int
        distance between s1 and s2

    Examples
    --------
    Find the Damerau-Levenshtein distance between two strings:

    >>> from rapidfuzz.distance import DamerauLevenshtein
    >>> DamerauLevenshtein.distance("CA", "ABC")
    2
    Nr   )r   r+   )r   r
   r,   r-   distr)   r)   r*   distance:   s   &
r1   c                C  sb   |dur|| } ||}t | |\} }tt| t|}t| |}|| }|du s-||kr/|S dS )a*  
    Calculates the Damerau-Levenshtein similarity in the range [max, 0].

    This is calculated as ``max(len1, len2) - distance``.

    Parameters
    ----------
    s1 : Sequence[Hashable]
        First string to compare.
    s2 : Sequence[Hashable]
        Second string to compare.
    processor: callable, optional
        Optional callable that is used to preprocess the strings before
        comparing them. Default is None, which deactivates this behaviour.
    score_cutoff : int, optional
        Maximum distance between s1 and s2, that is
        considered as a result. If the similarity is smaller than score_cutoff,
        0 is returned instead. Default is None, which deactivates
        this behaviour.

    Returns
    -------
    similarity : int
        similarity between s1 and s2
    Nr   )r   r   r   r1   )r   r
   r,   r-   maximumr0   simr)   r)   r*   
similarityi   s    
r4   float | Nonefloatc                C  s~   t | st |r
dS |dur|| } ||}t| |\} }tt| t|}t| |}|r1|| nd}|du s;||kr=|S dS )aB  
    Calculates a normalized Damerau-Levenshtein similarity in the range [1, 0].

    This is calculated as ``distance / max(len1, len2)``.

    Parameters
    ----------
    s1 : Sequence[Hashable]
        First string to compare.
    s2 : Sequence[Hashable]
        Second string to compare.
    processor: callable, optional
        Optional callable that is used to preprocess the strings before
        comparing them. Default is None, which deactivates this behaviour.
    score_cutoff : float, optional
        Optional argument for a score threshold as a float between 0 and 1.0.
        For norm_dist > score_cutoff 1.0 is returned instead. Default is 1.0,
        which deactivates this behaviour.

    Returns
    -------
    norm_dist : float
        normalized distance between s1 and s2 as a float between 0 and 1.0
          ?Nr   r   )r   r   r   r   r1   )r   r
   r,   r-   r2   r0   	norm_distr)   r)   r*   normalized_distance   s   
r9   c                C  sd   t | st |r
dS |dur|| } ||}t| |\} }t| |}d| }|du s.||kr0|S dS )a:  
    Calculates a normalized Damerau-Levenshtein similarity in the range [0, 1].

    This is calculated as ``1 - normalized_distance``

    Parameters
    ----------
    s1 : Sequence[Hashable]
        First string to compare.
    s2 : Sequence[Hashable]
        Second string to compare.
    processor: callable, optional
        Optional callable that is used to preprocess the strings before
        comparing them. Default is None, which deactivates this behaviour.
    score_cutoff : float, optional
        Optional argument for a score threshold as a float between 0 and 1.0.
        For norm_sim < score_cutoff 0 is returned instead. Default is 0,
        which deactivates this behaviour.

    Returns
    -------
    norm_sim : float
        normalized similarity between s1 and s2 as a float between 0 and 1.0
    g        Nr7   r   )r   r   r9   )r   r
   r,   r-   r8   norm_simr)   r)   r*   normalized_similarity   s   
r;   )r   r	   r
   r	   r   r   )
r   r	   r
   r	   r,   r.   r-   r/   r   r   )
r   r	   r
   r	   r,   r.   r-   r5   r   r6   )
__future__r   typingr   r   r   rapidfuzz._common_pyr   rapidfuzz._utilsr   r+   r1   r4   r9   r;   r)   r)   r)   r*   <module>   s"   
23/1