a
     ˜ëj±  ã                   @  sŒ   d dl 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„ Z
dddœdd„Zdddœdd„Zdddœdd„Zdddœdd„ZdS )é    )Úannotations)Úconv_sequences)Úis_noneÚsetupPandasc                 C  s<   |d }d}|||  7 }||| 7 }||| | 7 }|d S )Né   ç        g      @© )Úpattern_lenÚtext_lenÚcommon_charsZtranspositionsÚsimr   r   úY/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/rapidfuzz/distance/Jaro_py.pyÚ_jaro_calculate_similarity	   s    r   c                 C  s(   | r|sdS t | |t| |ƒdƒ}||kS )zC
    filter matches below score_cutoff based on string lengths
    Fr   )r   Úmin)r	   r
   Úscore_cutoffr   r   r   r   Ú_jaro_length_filter   s    r   c                 C  s   |sdS t | ||dƒ}||kS )zY
    filter matches below score_cutoff based on string lengths and common characters
    Fr   )r   )r	   r
   r   r   r   r   r   r   Ú_jaro_common_char_filter   s    r   c                 C  sx   t | ƒ}t |ƒ}d}||krF|d d }||| krn|d|| … }n(|d d }||| krn| d|| … } | ||fS )zB
    find bounds and skip out of bound parts of the sequences
    r   r   é   N)Úlen)Ús1Ús2r	   r
   Úboundr   r   r   Ú_jaro_bounds(   s    r   N©Ú	processorr   c                C  sÈ  t ƒ  t| ƒst|ƒrdS |dur2|| ƒ} ||ƒ}| s>|s>dS |du rJd}t| |ƒ\} }t| ƒ}t|ƒ}t|||ƒsxdS |dkrœ|dkrœt| d |d kƒS t| |ƒ\} }}dg| }dg| }d}	t| ƒD ]r\}
}td|
| ƒ}t	|
| |d ƒ}t
||d ƒD ]:}|| �s|| |k�rd ||
< ||< |	d7 }	 qÌ�qqÌt|||	|ƒ�sTdS d }}t|ƒD ]T\}
}|�rdt
||ƒD ]}|| �r||d } �qœ�q|| |
 || k�rd|d7 }�qdt|||	|ƒS )aÍ  
    Calculates the jaro 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 : float, optional
        Optional argument for a score threshold as a float between 0 and 1.0.
        For ratio < score_cutoff 0 is returned instead. Default is None,
        which deactivates this behaviour.

    Returns
    -------
    similarity : float
        similarity between s1 and s2 as a float between 0 and 1.0
    r   Nç      ð?r   r   FT)r   r   r   r   r   Úfloatr   Ú	enumerateÚmaxr   Úranger   r   )r   r   r   r   r	   r
   r   Zs1_flagsZs2_flagsr   ÚiZs1_chÚlowÚhiÚjÚkZtrans_countZs1_fr   r   r   Ú
similarity=   sR    




r%   c                C  s   t | |||d�S )aî  
    Calculates the normalized jaro 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 : float, optional
        Optional argument for a score threshold as a float between 0 and 1.0.
        For ratio < score_cutoff 0 is returned instead. Default is None,
        which deactivates this behaviour.

    Returns
    -------
    normalized similarity : float
        normalized similarity between s1 and s2 as a float between 0 and 1.0
    r   )r%   ©r   r   r   r   r   r   r   Únormalized_similarity—   s    r'   c                C  s|   t ƒ  t| ƒst|ƒrdS |dur2|| ƒ} ||ƒ}|du sB|dkrFdnd| }t| ||d�}d| }|du st||krx|S dS )aÉ  
    Calculates the jaro 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 ratio < score_cutoff 0 is returned instead. Default is None,
        which deactivates this behaviour.

    Returns
    -------
    distance : float
        distance between s1 and s2 as a float between 1.0 and 0.0
    r   N)r   )r   r   r%   )r   r   r   r   Zcutoff_distancer   Údistr   r   r   Údistance·   s    r)   c                C  s   t | |||d�S )aê  
    Calculates the normalized jaro 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 ratio < score_cutoff 0 is returned instead. Default is None,
        which deactivates this behaviour.

    Returns
    -------
    normalized distance : float
        normalized distance between s1 and s2 as a float between 1.0 and 0.0
    r   )r)   r&   r   r   r   Únormalized_distanceâ   s    r*   )Ú
__future__r   Zrapidfuzz._common_pyr   Zrapidfuzz._utilsr   r   r   r   r   r   r%   r'   r)   r*   r   r   r   r   Ú<module>   s$   	û^û$û/û