
    9i\                        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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y)    )annotations)CallableHashableSequence)conv_sequences)is_nonec                   | st        |      S dt        |       z  dz
  }d}d}d}t        |       }dt        |       dz
  z  }i }|j                  }	d}
| D ]  } |	|d      |
z  ||<   |
dz  }
 |D ]n  } |	|d      }| |z  dz  |z  }||z  |z   |z  |z  |z  }||z  }|||z   z  }||z  }|||z  dk7  z  }|||z  dk7  z  }|dz  dz  }|dz  }|||z   z  }||z  }|}p |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HNs                    c/var/www/html/backtest/airagagent/rag_env/lib/python3.12/site-packages/rapidfuzz/distance/OSA_py.py_osa_distance_hyrroe2003r      s`   2w
s2w,!	B	
B	
BH2wHR1D!#E		I	A sA&*c
	a  a t|!X-r	R2%-2"W BG*_"W 	R$Y1$$R$Y1$$ Ag]1WBG*_"W), O    N)	processorscore_cutoffc               x    | ||       }  ||      }t        | |      \  } }t        | |      }|||k  r|S |dz   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
    r
   )r   r   )r   r   r!   r"   dists        r   distancer%   7   sV    P r]r]B#FB#B+D (DL,@4W|VWGWWr    c                   | ||       }  ||      }t        | |      \  } }t        t        |       t        |            }t        | |      }||z
  }|||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
    r   )r   maxr   r%   )r   r   r!   r"   maximumr$   sims          r   
similarityr*   h   sm    @ r]r]B#FB#b'3r7#GBD
D.C'3,+>3FQFr    c                   t        |       st        |      ry| ||       }  ||      }t        | |      \  } }t        t        |       t        |            }t	        | |      }|r||z  nd}|||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
          ?r   r
   )r   r   r'   r   r%   )r   r   r!   r"   r(   r$   	norm_dists          r   normalized_distancer.      s    > r{gbkr]r]B#FB#b'3r7#GBD")wqI%-l1J9RQRRr    c                   t        |       st        |      ry| ||       }  ||      }t        | |      \  } }t        | |      }d|z
  }|||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        r,   r   )r   r   r.   )r   r   r!   r"   r-   norm_sims         r   normalized_similarityr1      sh    > r{gbkr]r]B#FB#B+IYH$,L0H8PqPr    )r   Sequence[Hashable]r   r2   returnint)
r   r2   r   r2   r!   (Callable[..., Sequence[Hashable]] | Noner"   z
int | Noner3   r4   )
r   r2   r   r2   r!   r5   r"   zfloat | Noner3   float)
__future__r   typingr   r   r   rapidfuzz._common_pyr   rapidfuzz._utilsr   r   r%   r*   r.   r1    r    r   <module>r<      s#   # / / / $(^ ;?#.X.X.X 8	.X
 .X 	.Xj ;?#(G(G(G 8	(G
 (G 	(G^ ;?!%*S*S*S 8	*S
 *S *Sb ;?!%)Q)Q)Q 8	)Q
 )Q )Qr    