o
    Uhk                     @  st   d dl mZ d dlmZ d dlmZm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   | st |S dt | > d }d}d}d}t | }dt | d > }i }|j}	d}
| D ]}|	|d|
B ||< |
dK }
q)|D ]R}|	|d}| |@ d> |@ }||@ | |A |B |B }||B }|||B  B }||@ }|||@ dk7 }|||@ dk8 }|d> dB }|d> }|||B  B }||@ }|}q;|S )N   r   )lenget)s1s2VPVND0PM_j_oldcurrDistmaskblock	block_getxch1ch2PM_jTRHPHN r   m/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/rapidfuzz/distance/OSA_py.py_osa_distance_hyrroe2003	   s:   

r   N)	processorscore_cutoffc                C  sL   |dur|| } ||}t | |\} }t| |}|du s ||kr"|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   distance4   s   (
r    c                C  sb   |dur|| } ||}t | |\} }tt| t|}t| |}|| }|du s-||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   
similaritye   s    
r$   c                C  s   t   t| st|rdS |dur|| } ||}t| |\} }tt| t|}t| |}|r4|| nd}|du s>||kr@|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"   r   	norm_distr   r   r   normalized_distance   s   
r'   c                C  sj   t   t| st|rdS |dur|| } ||}t| |\} }t| |}d| }|du s1||kr3|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   r&   norm_simr   r   r   normalized_similarity   s   
r)   )
__future__r   rapidfuzz._common_pyr   rapidfuzz._utilsr   r   r   r    r$   r'   r)   r   r   r   r   <module>   s    /5/2