o
    °#¨e°  ã                   @  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)   úW/var/www/myenv/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 )a@  
    Calculates a normalized Damerau-Levenshtein distance 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û