o
    wi                     @  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_charstranspositionssimr   r   W/home/ubuntu/sommelier/.venv/lib/python3.10/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r#|d d }||| kr"|d||  }n|d d }||| kr7| 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r|| } ||}| s|sdS |du r%d}t| |\} }t| }t|}t|||s<dS |dkrN|dkrNt| d |d kS t| |\} }}dg| }dg| }d}	t| D ]6\}
}td|
| }t	|
| |d }t
||d D ]}|| s|| |krd ||
< ||< |	d7 }	 nqqft|||	|sdS d }}t|D ]$\}
}|rt
||D ]}|| r|d } nq| |
 || kr|d7 }qt|||	|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   s1_flagss2_flagsr   is1_chlowhijktrans_counts1_fr   r   r   
similarity=   sX   

r+   c                C     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      r.   c                C  s|   t   t| st|rdS |dur|| } ||}|du s!|dkr#dnd| }t| ||d}d| }|du s:||kr<|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   cutoff_distancer   distr   r   r   distance   s   r2   c                C  r,   )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   )r2   r-   r   r   r   normalized_distance   r/   r3   )
__future__r   rapidfuzz._common_pyr   rapidfuzz._utilsr   r   r   r   r   r   r+   r.   r2   r3   r   r   r   r   <module>   s&   	^$/