a
     ˜ëjÏB  ã                   @  sÜ   d dl mZ d dlmZmZ d dlmZmZ d dlm	Z
 d dlmZmZ dd„ Zdd	„ Zd
d„ Zdddddœdd„Zdddddœdd„Zdddddœdd„Zdddddœdd„Zdd„ Zdddœdd„Zdddœdd„ZdS )é    )Úannotations)Úcommon_affixÚconv_sequences)Úis_noneÚsetupPandas)ÚIndel_py)ÚEditopÚEditopsc           	      C  sl   t | ƒ}t |ƒ}|\}}}|| ||  }||krNt||| || |  ƒ}nt||| || |  ƒ}|S )N)ÚlenÚmin)	Ús1Ús2ÚweightsÚlen1Zlen2ÚinsertÚdeleteÚreplaceZmax_dist© r   ú`/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/rapidfuzz/distance/Levenshtein_py.pyÚ_levenshtein_maximum   s    
r   c                 C  s®   t | ƒ}|\}}}ttd|d | |ƒƒ}|D ]v}|d }	|d  |7  < t|ƒD ]P}
|	}| |
 |krŠt||
 | ||
d  | |	| ƒ}||
d  }	|||
d < qRq.|d S )Nr   é   éÿÿÿÿ)r
   ÚlistÚranger   )r   r   r   r   r   r   r   ÚcacheÚch2ÚtempÚiÚxr   r   r   Ú_uniform_generic   s    
$r   c                 C  sþ   | st |ƒS dt | ƒ> d }d}t | ƒ}dt | ƒd > }i }|j}d}| D ]}	||	dƒ|B ||	< |dK }qJ|D ]Š}
||
dƒ}|}||@ | |A |B |B }|||B  B }||@ }|||@ dk7 }|||@ dk8 }|d> dB }|d> }|||B  B }||@ }qn|S ©Nr   r   )r
   Úget)r   r   ÚVPÚVNÚcurrDistÚmaskÚblockÚ	block_getr   Úch1r   ÚPM_jÚXÚD0ÚHPÚHNr   r   r   Ú_uniform_distance,   s2    


r.   ©r   r   r   N)r   Ú	processorÚscore_cutoffÚ
score_hintc                C  s„   |}|dur|| ƒ} ||ƒ}t | |ƒ\} }|du s:|dkrFt| |ƒ}n"|dkr\t | |¡}nt| ||ƒ}|du sx||kr||S |d S )a´  
    Calculates the minimum number of insertions, deletions, and substitutions
    required to change one sequence into the other according to Levenshtein with custom
    costs for insertion, deletion and substitution

    Parameters
    ----------
    s1 : Sequence[Hashable]
        First string to compare.
    s2 : Sequence[Hashable]
        Second string to compare.
    weights : tuple[int, int, int] or None, optional
        The weights for the three operations in the form
        (insertion, deletion, substitution). Default is (1, 1, 1),
        which gives all three operations a weight of 1.
    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.
    score_hint : int, optional
        Expected distance between s1 and s2. This is used to select a
        faster implementation. Default is None, which deactivates this behaviour.

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

    Raises
    ------
    ValueError
        If unsupported weights are provided a ValueError is thrown

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

    >>> from rapidfuzz.distance import Levenshtein
    >>> Levenshtein.distance("lewenstein", "levenshtein")
    2

    Setting a maximum distance allows the implementation to select
    a more efficient implementation:

    >>> Levenshtein.distance("lewenstein", "levenshtein", score_cutoff=1)
    2

    It is possible to select different weights by passing a `weight`
    tuple.

    >>> Levenshtein.distance("lewenstein", "levenshtein", weights=(1,1,2))
    3
    Nr/   )r   r   é   r   )r   r.   ÚIndelÚdistancer   )r   r   r   r0   r1   r2   Ú_Údistr   r   r   r5   P   s    Br5   c          
      C  sl   |}|dur|| ƒ} ||ƒ}t | |ƒ\} }|p0d}t| ||ƒ}t| ||d�}|| }	|du sd|	|krh|	S dS )a×  
    Calculates the levenshtein similarity in the range [max, 0] using custom
    costs for insertion, deletion and substitution.

    This is calculated as ``max - distance``, where max is the maximal possible
    Levenshtein distance given the lengths of the sequences s1/s2 and the weights.

    Parameters
    ----------
    s1 : Sequence[Hashable]
        First string to compare.
    s2 : Sequence[Hashable]
        Second string to compare.
    weights : tuple[int, int, int] or None, optional
        The weights for the three operations in the form
        (insertion, deletion, substitution). Default is (1, 1, 1),
        which gives all three operations a weight of 1.
    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.
    score_hint : int, optional
        Expected similarity between s1 and s2. This is used to select a
        faster implementation. Default is None, which deactivates this behaviour.

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

    Raises
    ------
    ValueError
        If unsupported weights are provided a ValueError is thrown
    Nr/   ©r   r   )r   r   r5   )
r   r   r   r0   r1   r2   r6   Úmaximumr7   Úsimr   r   r   Ú
similarity¢   s    0r;   c          
      C  sŽ   |}t ƒ  t| ƒst|ƒrdS |dur6|| ƒ} ||ƒ}t| |ƒ\} }|pJd}t| ||ƒ}t| ||d�}|rr|| nd}	|du s†|	|krŠ|	S dS )aû  
    Calculates a normalized levenshtein distance in the range [1, 0] using custom
    costs for insertion, deletion and substitution.

    This is calculated as ``distance / max``, where max is the maximal possible
    Levenshtein distance given the lengths of the sequences s1/s2 and the weights.

    Parameters
    ----------
    s1 : Sequence[Hashable]
        First string to compare.
    s2 : Sequence[Hashable]
        Second string to compare.
    weights : tuple[int, int, int] or None, optional
        The weights for the three operations in the form
        (insertion, deletion, substitution). Default is (1, 1, 1),
        which gives all three operations a weight of 1.
    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 None,
        which deactivates this behaviour.
    score_hint : float, optional
        Expected normalized distance between s1 and s2. This is used to select a
        faster implementation. Default is None, which deactivates this behaviour.

    Returns
    -------
    norm_dist : float
        normalized distance between s1 and s2 as a float between 1.0 and 0.0

    Raises
    ------
    ValueError
        If unsupported weights are provided a ValueError is thrown
    ç      ð?Nr/   r8   r   r   )r   r   r   r   r5   )
r   r   r   r0   r1   r2   r6   r9   r7   Ú	norm_distr   r   r   Únormalized_distanceß   s    /r>   c          	      C  sz   |}t ƒ  t| ƒst|ƒrdS |dur6|| ƒ} ||ƒ}t| |ƒ\} }|pJd}t| ||d�}d| }|du sr||krv|S dS )aÉ  
    Calculates a normalized levenshtein similarity in the range [0, 1] using custom
    costs for insertion, deletion and substitution.

    This is calculated as ``1 - normalized_distance``

    Parameters
    ----------
    s1 : Sequence[Hashable]
        First string to compare.
    s2 : Sequence[Hashable]
        Second string to compare.
    weights : tuple[int, int, int] or None, optional
        The weights for the three operations in the form
        (insertion, deletion, substitution). Default is (1, 1, 1),
        which gives all three operations a weight of 1.
    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 None,
        which deactivates this behaviour.
    score_hint : int, optional
        Expected normalized similarity between s1 and s2. This is used to select a
        faster implementation. Default is None, which deactivates this behaviour.

    Returns
    -------
    norm_sim : float
        normalized similarity between s1 and s2 as a float between 0 and 1.0

    Raises
    ------
    ValueError
        If unsupported weights are provided a ValueError is thrown

    Examples
    --------
    Find the normalized Levenshtein similarity between two strings:

    >>> from rapidfuzz.distance import Levenshtein
    >>> Levenshtein.normalized_similarity("lewenstein", "levenshtein")
    0.81818181818181

    Setting a score_cutoff allows the implementation to select
    a more efficient implementation:

    >>> Levenshtein.normalized_similarity("lewenstein", "levenshtein", score_cutoff=0.85)
    0.0

    It is possible to select different weights by passing a `weight`
    tuple.

    >>> Levenshtein.normalized_similarity("lewenstein", "levenshtein", weights=(1,1,2))
    0.85714285714285

    When a different processor is used s1 and s2 do not have to be strings

    >>> Levenshtein.normalized_similarity(["lewenstein"], ["levenshtein"], processor=lambda s: s[0])
    0.81818181818181
    g        Nr/   r8   r<   r   )r   r   r   r>   )	r   r   r   r0   r1   r2   r6   r=   Znorm_simr   r   r   Únormalized_similarity  s    Gr?   c                 C  s&  | st |ƒg g fS dt | ƒ> d }d}t | ƒ}dt | ƒd > }i }|j}d}| D ]}	||	dƒ|B ||	< |dK }qPg }
g }|D ]ž}||dƒ}|}||@ | |A |B |B }|||B  B }||@ }|||@ dk7 }|||@ dk8 }|d> dB }|d> }|||B  B }||@ }|
 |¡ | |¡ q|||
|fS r    )r
   r!   Úappend)r   r   r"   r#   r$   r%   r&   r'   r   r(   Z	matrix_VPZ	matrix_VNr   r)   r*   r+   r,   r-   r   r   r   Ú_matrixv  s:    


rA   ©r0   r2   c                C  s  |}|dur|| ƒ} ||ƒ}t | |ƒ\} }t| |ƒ\}}| |t| ƒ| … } ||t|ƒ| … }t| |ƒ\}}}	tg ddƒ}
t| ƒ| | |
_t|ƒ| | |
_|dkr¬|
S dg| }t| ƒ}t|ƒ}|dk�r¢|dk�r¢||d  d|d > @ �r|d8 }|d8 }td|| || ƒ||< qÆ|d8 }|�rh|	|d  d|d > @ �rh|d8 }td|| || ƒ||< qÆ|d8 }| | || krÆ|d8 }td|| || ƒ||< qÆ|dk�rØ|d8 }|d8 }td|| || ƒ||< �q¢|dk�r|d8 }|d8 }td|| || ƒ||< �qØ||
_|
S )u  
    Return Editops describing how to turn s1 into s2.

    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_hint : int, optional
        Expected distance between s1 and s2. This is used to select a
        faster implementation. Default is None, which deactivates this behaviour.

    Returns
    -------
    editops : Editops
        edit operations required to turn s1 into s2

    Notes
    -----
    The alignment is calculated using an algorithm of Heikki HyyrÃ¶, which is
    described [8]_. It has a time complexity and memory usage of ``O([N/64] * M)``.

    References
    ----------
    .. [8] HyyrÃ¶, Heikki. "A Note on Bit-Parallel Alignment Computation."
           Stringology (2004).

    Examples
    --------
    >>> from rapidfuzz.distance import Levenshtein
    >>> for tag, src_pos, dest_pos in Levenshtein.editops("qabxcd", "abycdf"):
    ...    print(("%7s s1[%d] s2[%d]" % (tag, src_pos, dest_pos)))
     delete s1[1] s2[0]
    replace s1[3] s2[2]
     insert s1[6] s2[5]
    Nr   r   r   r   r   )	r   r   r
   rA   r	   Z_src_lenZ	_dest_lenr   Z_editops)r   r   r0   r2   r6   Ú
prefix_lenZ
suffix_lenr7   r"   r#   ÚeditopsZeditop_listÚcolÚrowr   r   r   rD   Ÿ  sP    /
 

rD   c                C  s   t | |||d� ¡ S )uÊ  
    Return Opcodes describing how to turn s1 into s2.

    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_hint : int, optional
        Expected distance between s1 and s2. This is used to select a
        faster implementation. Default is None, which deactivates this behaviour.

    Returns
    -------
    opcodes : Opcodes
        edit operations required to turn s1 into s2

    Notes
    -----
    The alignment is calculated using an algorithm of Heikki HyyrÃ¶, which is
    described [9]_. It has a time complexity and memory usage of ``O([N/64] * M)``.

    References
    ----------
    .. [9] HyyrÃ¶, Heikki. "A Note on Bit-Parallel Alignment Computation."
           Stringology (2004).

    Examples
    --------
    >>> from rapidfuzz.distance import Levenshtein

    >>> a = "qabxcd"
    >>> b = "abycdf"
    >>> for tag, i1, i2, j1, j2 in Levenshtein.opcodes("qabxcd", "abycdf"):
    ...    print(("%7s a[%d:%d] (%s) b[%d:%d] (%s)" %
    ...           (tag, i1, i2, a[i1:i2], j1, j2, b[j1:j2])))
     delete a[0:1] (q) b[0:0] ()
      equal a[1:3] (ab) b[0:2] (ab)
    replace a[3:4] (x) b[2:3] (y)
      equal a[4:6] (cd) b[3:5] (cd)
     insert a[6:6] () b[5:6] (f)
    rB   )rD   Z
as_opcodes)r   r   r0   r2   r   r   r   Úopcodes  s    5rG   )Ú
__future__r   Zrapidfuzz._common_pyr   r   Zrapidfuzz._utilsr   r   Zrapidfuzz.distancer   r4   Z!rapidfuzz.distance._initialize_pyr   r	   r   r   r.   r5   r;   r>   r?   rA   rD   rG   r   r   r   r   Ú<module>   sD   (ùVùAùDùW-ûkû