a
    yh                     @  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   V/var/www/html/assistant/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rt| 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qt|||	|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$   	^$/