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	„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_nonezSequence[Hashable]Úint)Ús1Ús2Úreturnc                 C  s   | st |ƒS dt | ƒ> d }d}d}d}t | ƒ}dt | ƒd > }i }|j}	d}
| D ]}|	|dƒ|
B ||< |
dK }
qR|D ]¤}|	|dƒ}| |@ d> |@ }||@ | |A |B |B }||B }|||B  B }||@ }|||@ dk7 }|||@ dk8 }|d> dB }|d> }|||B  B }||@ }|}qv|S )Né   r   )ÚlenÚget)r	   r
   ZVPZVNZD0ZPM_j_oldZcurrDistÚmaskÚblockZ	block_getÚxZch1Zch2ZPM_jZTRZHPZHN© r   úZ/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/rapidfuzz/distance/OSA_py.pyÚ_osa_distance_hyrroe2003   s:    

r   N)Ú	processorÚscore_cutoffz(Callable[..., Sequence[Hashable]] | Nonez
int | None)r	   r
   r   r   r   c                C  sL   |dk	r|| ƒ} ||ƒ}t | |ƒ\} }t| |ƒ}|dks@||krD|S |d S )a¯  
    Calculates the optimal string alignment (OSA) 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 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

    Examples
    --------
    Find the OSA distance between two strings:

    >>> from rapidfuzz.distance import OSA
    >>> OSA.distance("CA", "AC")
    2
    >>> OSA.distance("CA", "ABC")
    3
    Nr   )r   r   )r	   r
   r   r   Údistr   r   r   Údistance7   s    (
r   c                C  sb   |dk	r|| ƒ} ||ƒ}t | |ƒ\} }tt| ƒt|ƒƒ}t| |ƒ}|| }|dksZ||kr^|S dS )a5  
    Calculates the optimal string alignment (OSA) similarity in the range [max, 0].

    This is calculated as ``max(len1, len2) - 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
    -------
    similarity : int
        similarity between s1 and s2
    Nr   )r   Úmaxr   r   )r	   r
   r   r   Úmaximumr   Úsimr   r   r   Ú
similarityh   s     
r   zfloat | NoneÚfloatc                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 )aM  
    Calculates a normalized optimal string alignment (OSA) similarity in the range [1, 0].

    This is calculated as ``distance / max(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
    ç      ð?Nr   r   )r   r   r   r   r   )r	   r
   r   r   r   r   Ú	norm_distr   r   r   Únormalized_distance“   s    
r    c                C  sd   t | ƒst |ƒrdS |dk	r,|| ƒ} ||ƒ}t| |ƒ\} }t| |ƒ}d| }|dks\||kr`|S dS )aE  
    Calculates a normalized optimal string alignment (OSA) 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   Znorm_simr   r   r   Únormalized_similarityÀ   s    
r!   )Ú
__future__r   Útypingr   r   r   Zrapidfuzz._common_pyr   Zrapidfuzz._utilsr   r   r   r   r    r!   r   r   r   r   Ú<module>   s    /û5û/û1û