a
     ˜ëjð,  ã                   @  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	m
Z
 dddœdd„Zdd	d
„Zdddœdd„Zdddœdd„Zdddœdd„Zdd„ Zddœdd„Zddœdd„ZdS )é    )Úannotations)Úcommon_affixÚconv_sequences)Úis_noneÚsetupPandas)ÚEditopÚEditopsN)Ú	processorÚscore_cutoffc                C  sÐ   |dur|| ƒ} ||ƒ}| s dS t | |ƒ\} }dt| ƒ> d }i }|j}d}| D ]}||dƒ|B ||< |dK }qP|D ]&}	||	dƒ}
||
@ }|| || B }qtt|ƒt| ƒ d…  d¡}|du sÈ||krÌ|S dS )aâ  
    Calculates the length of the longest common subsequence

    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   é   Ú0)r   ÚlenÚgetÚbinÚcount)Ús1Ús2r	   r
   ÚSÚblockÚ	block_getÚxÚch1Úch2ÚMatchesÚuÚres© r   ú[/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/rapidfuzz/distance/LCSseq_py.pyÚ
similarity
   s&    

r   c           
      C  s~   |sdS dt |ƒ> d }| j}|D ]&}||dƒ}||@ }|| || B }q"t|ƒt |ƒ d …  d¡}	|d u sv|	|krz|	S dS ©Nr   r   r   )r   r   r   r   )
r   r   r   r
   r   r   r   r   r   r   r   r   r   Ú_block_similarityB   s    
r    c                C  sf   |dur|| ƒ} ||ƒ}t | |ƒ\} }tt| ƒt|ƒƒ}t| |ƒ}|| }|du sZ||kr^|S |d S )aŒ  
    Calculates the LCS distance in the range [0, max].

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

    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 LCS distance between two strings:

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

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

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

    Nr   )r   Úmaxr   r   )r   r   r	   r
   ÚmaximumÚsimÚdistr   r   r   ÚdistanceX   s    /
r%   c                C  s„   t ƒ  t| ƒst|ƒrdS |dur2|| ƒ} ||ƒ}| r:|s>dS t| |ƒ\} }tt| ƒt|ƒƒ}t| |ƒ| }|du s|||kr€|S dS )a2  
    Calculates a normalized LCS 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!   r   r%   )r   r   r	   r
   r"   Únorm_simr   r   r   Únormalized_distance’   s    r(   c                C  sX   t ƒ  t| ƒst|ƒrdS |dur2|| ƒ} ||ƒ}dt| |ƒ }|du sP||krT|S dS )a�  
    Calculates a normalized LCS 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

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

    >>> from rapidfuzz.distance import LCSseq
    >>> LCSseq.normalized_similarity("lewenstein", "levenshtein")
    0.8181818181818181

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

    >>> LCSseq.normalized_similarity("lewenstein", "levenshtein", score_cutoff=0.9)
    0.0

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

    >>> LCSseq.normalized_similarity(["lewenstein"], ["levenshtein"], processor=lambda s: s[0])
    0.81818181818181
    g        Nr&   r   )r   r   r(   )r   r   r	   r
   r'   r   r   r   Únormalized_similarityÂ   s    2r)   c                 C  s¬   | sdg fS dt | ƒ> d }i }|j}d}| D ]}||dƒ|B ||< |dK }q.g }|D ]0}||dƒ}	||	@ }
||
 ||
 B }| |¡ qVt|ƒt | ƒ d …  d¡}||fS r   )r   r   Úappendr   r   )r   r   r   r   r   r   r   Úmatrixr   r   r   r#   r   r   r   Ú_matrix   s"    

r,   ©r	   c                C  sú  |dur|| ƒ} ||ƒ}t | |ƒ\} }t| |ƒ\}}| |t| ƒ| … } ||t|ƒ| … }t| |ƒ\}}tg ddƒ}t| ƒ| | |_t|ƒ| | |_t| ƒt|ƒ d|  }|dkr¾|S dg| }	t| ƒ}
t|ƒ}|dk�r„|
dk�r„||d  d|
d > @ �r0|d8 }|
d8 }
td|
| || ƒ|	|< qØ|d8 }|�rz||d  d|
d > @ �sz|d8 }td|
| || ƒ|	|< qØ|
d8 }
qØ|
dk�rº|d8 }|
d8 }
td|
| || ƒ|	|< �q„|dk�rð|d8 }|d8 }td|
| || ƒ|	|< �qº|	|_|S )uf  
    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.

    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 in [6]_. It has a time complexity and memory usage of ``O([N/64] * M)``.

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

    Examples
    --------
    >>> from rapidfuzz.distance import LCSseq
    >>> for tag, src_pos, dest_pos in LCSseq.editops("qabxcd", "abycdf"):
    ...    print(("%7s s1[%d] s2[%d]" % (tag, src_pos, dest_pos)))
     delete s1[0] s2[0]
     delete s1[3] s2[2]
     insert s1[4] s2[2]
     insert s1[6] s2[5]
    Nr   é   r   ÚdeleteÚinsert)	r   r   r   r,   r   Z_src_lenZ	_dest_lenr   Z_editops)r   r   r	   Ú
prefix_lenZ
suffix_lenr#   r+   Úeditopsr$   Zeditop_listÚcolÚrowr   r   r   r2     sJ    ,
 


r2   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.

    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 in [7]_. It has a time complexity and memory usage of ``O([N/64] * M)``.

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

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

    >>> a = "qabxcd"
    >>> b = "abycdf"
    >>> for tag, i1, i2, j1, j2 in LCSseq.opcodes(a, b):
    ...    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)
     delete a[3:4] (x) b[2:2] ()
     insert a[4:4] () b[2:3] (y)
      equal a[4:6] (cd) b[3:5] (cd)
     insert a[6:6] () b[5:6] (f)
    r-   )r2   Z
as_opcodes)r   r   r	   r   r   r   Úopcodesx  s    2r5   )N)Ú
__future__r   Zrapidfuzz._common_pyr   r   Zrapidfuzz._utilsr   r   Z!rapidfuzz.distance._initialize_pyr   r   r   r    r%   r(   r)   r,   r2   r5   r   r   r   r   Ú<module>   s,   û< ü
û>û4û>üdü