U
    -eD                     @  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
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_noneN)	processorscore_cutoffzSequence[Hashable]z(Callable[..., Sequence[Hashable]] | Nonez
int | Noneint)s1s2r   r	   returnc                C  sf   |dk	r|| } ||}t | |\} }tt| t|}t| |}|| }|dksZ||kr^|S |d S )a  
    Calculates the Prefix distance between two strings.

    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 : int or None, optional
        Maximum distance between s1 and s2, that is
        considered as a result. If the distance is bigger than score_cutoff,
        score_cutoff + 1 is returned instead. Default is None, which deactivates
        this behaviour.

    Returns
    -------
    distance : int
        distance between s1 and s2
    N   )r   maxlen
similarity)r   r   r   r	   maximumsimdist r   ]/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/rapidfuzz/distance/Prefix_py.pydistance   s    
r   c                C  sj   |dk	r|| } ||}t | |\} }d}t| |D ]\}}||krH qR|d7 }q4|dksb||krf|S dS )a  
    Calculates the prefix similarity between two strings.

    This is calculated as ``len1 - 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 : int, optional
        Maximum distance between s1 and s2, that is
        considered as a result. If the similarity is smaller than score_cutoff,
        0 is returned instead. Default is None, which deactivates
        this behaviour.

    Returns
    -------
    distance : int
        distance between s1 and s2
    Nr   r   )r   zip)r   r   r   r	   r   Zch1Zch2r   r   r   r   6   s     
r   zfloat | Nonefloatc                C  sB   t | st |rdS t| ||d}d| }|dks:||kr>|S dS )a3  
    Calculates a normalized prefix similarity in the range [1, 0].

    This is calculated as ``distance / (len1 + len2)``.

    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 norm_dist > score_cutoff 1.0 is returned instead. Default is 1.0,
        which deactivates this behaviour.

    Returns
    -------
    norm_dist : float
        normalized distance between s1 and s2 as a float between 0 and 1.0
          ?)r   N)r   normalized_similarity)r   r   r   r	   norm_simZ	norm_distr   r   r   normalized_distanced   s
    r   c                C  s~   t | st |rdS |dk	r,|| } ||}t| |\} }tt| t|}t| |}|rb|| nd}|dksv||krz|S dS )a-  
    Calculates a normalized prefix similarity in the range [0, 1].

    This is calculated as ``1 - normalized_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 norm_sim < score_cutoff 0 is returned instead. Default is 0,
        which deactivates this behaviour.

    Returns
    -------
    norm_sim : float
        normalized similarity between s1 and s2 as a float between 0 and 1.0
    g        Nr   )r   r   r   r   r   )r   r   r   r	   r   r   r   r   r   r   r      s    
r   )
__future__r   typingr   r   r   Zrapidfuzz._common_pyr   Zrapidfuzz._utilsr   r   r   r   r   r   r   r   r   <module>   s   .2,