U
    -e                     @  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ddddd	Z
dddd
dddZddddd
dddZddddddZdddddddddddZdddddddddddZddddddddddd Zdddddddddd!d"ZdS )#    )annotations)CallableHashableSequence)conv_sequences)is_noneintfloat)pattern_lentext_lencommon_charstranspositionsreturnc                 C  s<   |d }d}|||  7 }||| 7 }||| | 7 }|d S )N           g      @ )r
   r   r   r   simr   r   [/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/rapidfuzz/distance/Jaro_py.py_jaro_calculate_similarity   s    r   bool)r
   r   score_cutoffr   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   r   r   r   r   r   _jaro_length_filter   s    r   )r
   r   r   r   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   zSequence[Hashable]z2tuple[Sequence[Hashable], Sequence[Hashable], int])s1s2r   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)r   r   r
   r   boundr   r   r   _jaro_bounds+   s    r   N	processorr   z(Callable[..., Sequence[Hashable]] | Nonezfloat | None)r   r   r!   r   r   c                C  s  t | st |rdS |dk	r,|| } ||}| s8|s8dS |dkrDd}t| |\} }t| }t|}t|||srdS |dkr|dkrt| d |d kS t| |\} }}dg| }dg| }d}	t| D ]l\}
}td|
| }t|
| |d }t	||d D ]4}|| s|| |krd ||
< ||< |	d7 }	 qqqt
|||	|sHdS d }}t|D ]T\}
}|rXt	||D ]}|| rp|d } qqp| |
 || krX|d7 }qXt|||	|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   Zs1_flagsZs2_flagsr   iZs1_chlowhijkZtrans_countZs1_fr   r   r   
similarity@   sP    



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  sv   t | st |rdS |dk	r,|| } ||}|dks<|dkr@dnd| }t| ||d}d| }|dksn||krr|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   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    r0   )
__future__r   typingr   r   r   Zrapidfuzz._common_pyr   Zrapidfuzz._utilsr   r   r   r   r   r+   r-   r/   r0   r   r   r   r   <module>   s&   	]$.