o
    ҷh                     @  s   d dl mZ d dlmZmZmZ d dlmZ d dlm	Z	 d'ddZ
d(ddZd)ddZ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ddd+d%d&ZdS ),    )annotations)CallableHashableSequence)conv_sequences)is_nonepattern_leninttext_lencommon_charstranspositionsreturnfloatc                 C  s<   |d }d}|||  7 }||| 7 }||| | 7 }|d S )N           g      @ )r   r
   r   r   simr   r   M/var/www/html/venv/lib/python3.10/site-packages/rapidfuzz/distance/Jaro_py.py_jaro_calculate_similarity   s   r   score_cutoffboolc                 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
   r   r   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   s1Sequence[Hashable]s22tuple[Sequence[Hashable], Sequence[Hashable], int]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)r   r   r   r
   boundr   r   r   _jaro_bounds+   s   
r!   N	processorr   r#   (Callable[..., Sequence[Hashable]] | Nonefloat | Nonec                C  s  t | st |r
dS |dur|| } ||}| s|sdS |du r"d}t| |\} }t| }t|}t|||s9dS |dkrK|dkrKt| 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q~qct
|||	|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   r!   	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@   sV   

r4   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"   )r4   r   r   r#   r   r   r   r   normalized_similarity      r7   c                C  sv   t | st |r
dS |dur|| } ||}|du s|dkr dnd| }t| ||d}d| }|du s7||kr9|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   r4   )r   r   r#   r   cutoff_distancer   distr   r   r   distance   s   r;   c                C  r5   )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;   r6   r   r   r   normalized_distance   r8   r<   )
r   r	   r
   r	   r   r	   r   r	   r   r   )r   r	   r
   r	   r   r   r   r   )
r   r	   r
   r	   r   r	   r   r   r   r   )r   r   r   r   r   r   )
r   r   r   r   r#   r$   r   r%   r   r   )
__future__r   typingr   r   r   rapidfuzz._common_pyr   rapidfuzz._utilsr   r   r   r   r!   r4   r7   r;   r<   r   r   r   r   <module>   s(   

	

]$.