Ë
    ¡[;jÏB  ã                  óÈ   — 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„ Zd„ Z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„ Zd
d
dœd„Zd
d
dœd„Zy
)é    )Úannotations)Úcommon_affixÚconv_sequences)Úis_noneÚsetupPandas)ÚIndel_py)ÚEditopÚEditopsc                óÂ   — t        | «      }t        |«      }|\  }}}||z  ||z  z   }||k\  rt        |||z  ||z
  |z  z   «      }|S t        |||z  ||z
  |z  z   «      }|S )N)ÚlenÚmin)	Ús1Ús2ÚweightsÚlen1Úlen2ÚinsertÚdeleteÚreplaceÚmax_dists	            újG:\00. PROJECTS\API\Inventory\templateJSON\kerjaOCR\Lib\site-packages\rapidfuzz/distance/Levenshtein_py.pyÚ_levenshtein_maximumr      sƒ   € Üˆr‹7€DÜˆr‹7€DØ%Ñ€FˆF�Gà�f‰}˜t f™}Ñ,€Hàˆt‚|Ü�x ¨¡°4¸$±;À&Ñ2HÑ!HÓIˆð €Oô �x ¨¡°4¸$±;À&Ñ2HÑ!HÓIˆà€Oó    c                ó*  — t        | «      }|\  }}}t        t        d|dz   |z  |«      «      }|D ]]  }|d   }	|dxx   |z  cc<   t        |«      D ];  }
|	}| |
   |k7  rt        ||
   |z   ||
dz      |z   |	|z   «      }||
dz      }	|||
dz   <   Œ= Œ_ |d   S )Nr   é   éÿÿÿÿ)r   ÚlistÚranger   )r   r   r   r   r   r   r   ÚcacheÚch2ÚtempÚiÚxs               r   Ú_uniform_genericr$      sÀ   € Üˆr‹7€DØ%Ñ€FˆF�GÜ”�q˜4 !™8 vÑ-¨vÓ6Ó7€EãˆØ�Q‰xˆØˆa‹�FÑ‹Ü�t–ˆAØˆAØ�!‰u˜Š|Ü˜˜a™ 6Ñ)¨5°°Q±©<¸&Ñ+@À$ÈÁ.ÓQ�Ø˜˜Q™‘<ˆDØˆE�!�a‘%ŠLñ ð ð �‰9Ðr   c                ó˜  — | st        |«      S dt        | «      z  dz
  }d}t        | «      }dt        | «      dz
  z  }i }|j                  }d}| D ]  }	 ||	d«      |z  ||	<   |dz  }Œ |D ]]  }
 ||
d«      }|}||z  |z   |z  |z  |z  }|||z   z  }||z  }|||z  dk7  z  }|||z  dk7  z  }|dz  dz  }|dz  }|||z   z  }||z  }Œ_ |S ©Nr   r   )r   Úget)r   r   ÚVPÚVNÚcurrDistÚmaskÚblockÚ	block_getr#   Úch1r    ÚPM_jÚXÚD0ÚHPÚHNs                   r   Ú_uniform_distancer4   ,   s'  € ÙÜ�2‹wˆà
Œs�2‹w‰,˜!Ñ	€BØ	
€BÜ�2‹w€HØ”�R“˜1‘Ñ€Dà€EØ—	‘	€IØ	€AÛˆÙ˜s AÓ&¨Ñ*ˆˆc‰
Ø	ˆa‰‰ð ó ˆá˜˜aÓ ˆØˆØ�B‘˜"‰} Ñ" aÑ'¨"Ñ,ˆà�B˜‘G�*‰_ˆØ�"‰Wˆà�R˜$‘Y 1Ñ$Ñ$ˆØ�R˜$‘Y 1Ñ$Ñ$ˆà�A‰g˜‰]ˆØ�1‰WˆØ�B˜‘G�*‰_ˆØ�"‰W‰ð ð" €Or   ©r   r   r   N)r   Ú	processorÚscore_cutoffÚ
score_hintc               óÞ   — |}|� || «      }  ||«      }t        | |«      \  } }|�|dk(  rt        | |«      }n)|dk(  rt        j                  | |«      }nt	        | ||«      }|�||k  r|S |dz   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
    r5   )r   r   é   r   )r   r4   ÚIndelÚdistancer$   )r   r   r   r6   r7   r8   Ú_Údists           r   r<   r<   P   s�   € ðD 	€AØÐÙ�r‹]ˆÙ�r‹]ˆä˜B Ó#�F€BˆØ€˜' YÒ.Ü   RÓ(‰Ø	�IÒ	Ü�~‰~˜b "Ó%‰ä  B¨Ó0ˆà Ð(¨D°LÒ,@ˆ4ÐWÀ|ÐVWÑGWÐWr   c               óª   — |}|� || «      }  ||«      }t        | |«      \  } }|xs d}t        | ||«      }t        | ||¬«      }||z
  }	|�|	|k\  r|	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
    r5   ©r   r   )r   r   r<   )
r   r   r   r6   r7   r8   r=   Úmaximumr>   Úsims
             r   Ú
similarityrC   ¢   sx   € ð` 	€AØÐÙ�r‹]ˆÙ�r‹]ˆä˜B Ó#�F€BˆØÒ"˜€GÜ" 2 r¨7Ó3€GÜ�B˜ GÔ,€DØ
�D‰.€CØÐ'¨3°,Ò+>ˆ3ÐFÀQÐFr   c               óô   — |}t        «        t        | «      st        |«      ry|� || «      }  ||«      }t        | |«      \  } }|xs d}t        | ||«      }t	        | ||¬«      }|r||z  nd}	|�|	|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
    ç      ð?r5   r@   r   r   )r   r   r   r   r<   )
r   r   r   r6   r7   r8   r=   rA   r>   Ú	norm_dists
             r   Únormalized_distancerG   ß   s’   € ð^ 	€AÜ„MÜˆr„{”g˜b”kØàÐÙ�r‹]ˆÙ�r‹]ˆä˜B Ó#�F€BˆØÒ"˜€GÜ" 2 r¨7Ó3€GÜ�B˜ GÔ,€DÙ")��w’¨q€IØ%Ð-°¸lÒ1Jˆ9ÐRÐQRÐRr   c               óÒ   — |}t        «        t        | «      st        |«      ry|� || «      }  ||«      }t        | |«      \  } }|xs d}t        | ||¬«      }d|z
  }|�||k\  r|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        r5   r@   rE   r   )r   r   r   rG   )	r   r   r   r6   r7   r8   r=   rF   Únorm_sims	            r   Únormalized_similarityrJ     s   € ðN 	€AÜ„MÜˆr„{”g˜b”kØàÐÙ�r‹]ˆÙ�r‹]ˆä˜B Ó#�F€BˆØÒ"˜€GÜ# B¨°GÔ<€IØ�Y‰€HØ$Ð,°¸LÒ0Hˆ8ÐPÈqÐPr   c                óð  — | st        |«      g g fS dt        | «      z  dz
  }d}t        | «      }dt        | «      dz
  z  }i }|j                  }d}| D ]  }	 ||	d«      |z  ||	<   |dz  }Œ g }
g }|D ]  } ||d«      }|}||z  |z   |z  |z  |z  }|||z   z  }||z  }|||z  dk7  z  }|||z  dk7  z  }|dz  dz  }|dz  }|||z   z  }||z  }|
j                  |«       |j                  |«       Œ� ||
|fS r&   )r   r'   Úappend)r   r   r(   r)   r*   r+   r,   r-   r#   r.   Ú	matrix_VPÚ	matrix_VNr    r/   r0   r1   r2   r3   s                     r   Ú_matrixrO   v  s]  € ÙÜ�B“˜˜RÐ Ð à
Œs�2‹w‰,˜!Ñ	€BØ	
€BÜ�2‹w€HØ”�R“˜1‘Ñ€Dà€EØ—	‘	€IØ	€AÛˆÙ˜s AÓ&¨Ñ*ˆˆc‰
Ø	ˆa‰‰ð ð €IØ€IÛˆá˜˜aÓ ˆØˆØ�B‘˜"‰} Ñ" aÑ'¨"Ñ,ˆà�B˜‘G�*‰_ˆØ�"‰Wˆà�R˜$‘Y 1Ñ$Ñ$ˆØ�R˜$‘Y 1Ñ$Ñ$ˆà�A‰g˜‰]ˆØ�1‰WˆØ�B˜‘G�*‰_ˆØ�"‰Wˆà×Ñ˜ÔØ×Ñ˜Õð% ð( �i Ð+Ð+r   ©r6   r8   c               óŒ  — |}|� || «      }  ||«      }t        | |«      \  } }t        | |«      \  }}| |t        | «      |z
   } ||t        |«      |z
   }t        | |«      \  }}}	t	        g dd«      }
t        | «      |z   |z   |
_        t        |«      |z   |z   |
_        |dk(  r|
S dg|z  }t        | «      }t        |«      }|dk7  r¡|dk7  rœ||dz
     d|dz
  z  z  r!|dz  }|dz  }t        d||z   ||z   «      ||<   n_|dz  }|r-|	|dz
     d|dz
  z  z  r|dz  }t        d||z   ||z   «      ||<   n+|dz  }| |   ||   k7  r|dz  }t        d||z   ||z   «      ||<   |dk7  r|dk7  rŒœ|dk7  r&|dz  }|dz  }t        d||z   ||z   «      ||<   |dk7  rŒ&|dk7  r&|dz  }|dz  }t        d||z   ||z   «      ||<   |dk7  rŒ&||
_        |
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   rO   r
   Ú_src_lenÚ	_dest_lenr	   Ú_editops)r   r   r6   r8   r=   Ú
prefix_lenÚ
suffix_lenr>   r(   r)   ÚeditopsÚeditop_listÚcolÚrows                 r   rW   rW   Ÿ  sb  € ð^ 	€AØÐÙ�r‹]ˆÙ�r‹]ˆä˜B Ó#�F€BˆÜ)¨"¨bÓ1Ñ€J�
Ø	ˆJœ˜R› :Ñ-Ð	.€BØ	ˆJœ˜R› :Ñ-Ð	.€BÜ˜2˜r“?�L€Dˆ"ˆbä�b˜!˜QÓ€GÜ˜2“w Ñ+¨jÑ8€GÔÜ˜B› *Ñ,¨zÑ9€GÔàˆq‚yØˆà�&˜4‘-€KÜ
ˆb‹'€CÜ
ˆb‹'€CØ
�Š(�s˜a’xàˆc�A‰g‰;˜!  a¡™.Ò)Ø�A‰IˆDØ�1‰HˆCÜ & x°°zÑ1AÀ3ÈÑCSÓ TˆK˜Òà�1‰HˆCñ ˜˜3 ™7™ q¨S°1©W¡~Ò6Ø˜‘	�Ü$*¨8°S¸:Ñ5EÀsÈZÑGWÓ$X�˜DÒ!à�q‘�ð �c‘7˜b ™gÒ%Ø˜A‘I�DÜ(.¨y¸#À
Ñ:JÈCÐR\ÑL\Ó(]�K Ñ%ð' �Š(�s˜a“xð* �Š(Ø�‰	ˆØˆq‰ˆÜ" 8¨S°:Ñ-=¸sÀZÑ?OÓPˆ�DÑð �‹(ð
 �Š(Ø�‰	ˆØˆq‰ˆÜ" 8¨S°:Ñ-=¸sÀZÑ?OÓPˆ�DÑð �‹(ð
 #€GÔØ€Nr   c               ó<   — t        | |||¬«      j                  «       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)
    rP   )rW   Ú
as_opcodes)r   r   r6   r8   s       r   Úopcodesr]     s   € ôj �2�r Y¸:ÔF×QÑQÓSÐSr   )Ú
__future__r   Úrapidfuzz._common_pyr   r   Úrapidfuzz._utilsr   r   Úrapidfuzz.distancer   r;   Ú!rapidfuzz.distance._initialize_pyr	   r
   r   r$   r4   r<   rC   rG   rJ   rO   rW   r]   © r   r   Ú<module>rd      sŸ   ðõ #ç =ß 1Ý 0ß =òòò$!ðP ØØØôOXðl ØØØô:GðB ØØØô=SðH ØØØôTQòn&,ðZ ØôdðV Øõ5Tr   