o
    Uhy#                     @  s   d dl mZ d dlmZ d dlmZmZ d dlmZm	Z	 dddddd	Z
ddddd
dZddddddZd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setupPandas)EditopEditopsTN)pad	processorscore_cutoffc          	      C  s   |dur|| } ||}t | |\} }|s#t| t|kr#d}t|tt| t|}tt| t|}t|D ]}|| | || k8 }q9|du sN||krP|S |d S )a  
    Calculates the Hamming distance between two strings.
    The hamming distance is defined as the number of positions
    where the two strings differ. It describes the minimum
    amount of substitutions required to transform s1 into s2.

    Parameters
    ----------
    s1 : Sequence[Hashable]
        First string to compare.
    s2 : Sequence[Hashable]
        Second string to compare.
    pad : bool, optional
       should strings be padded if there is a length difference.
       If pad is False and strings have a different length
       a ValueError is thrown instead. Defaults is True.
    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

    Raises
    ------
    ValueError
        If s1 and s2 have a different length
    N"Sequences are not the same length.   )r   len
ValueErrorminmaxrange)	s1s2r   r	   r
   msgmin_lendisti r   q/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/rapidfuzz/distance/Hamming_py.pydistance
   s   +r   c                C  sf   |dur|| } ||}t | |\} }tt| t|}t| ||d}|| }|du s/||kr1|S dS )a.  
    Calculates the Hamming 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.
    pad : bool, optional
       should strings be padded if there is a length difference.
       If pad is False and strings have a different length
       a ValueError is thrown instead. Defaults is True.
    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

    Raises
    ------
    ValueError
        If s1 and s2 have a different length
    Nr   r   )r   r   r   r   )r   r   r   r	   r
   maximumr   simr   r   r   
similarityG   s   *r   c                C  s   t   t| st|rdS |dur|| } ||}t| |\} }tt| t|}t| ||d}|r6|| nd}|du s@||krB|S dS )aU  
    Calculates a normalized Hamming 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.
    pad : bool, optional
       should strings be padded if there is a length difference.
       If pad is False and strings have a different length
       a ValueError is thrown instead. Defaults is True.
    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

    Raises
    ------
    ValueError
        If s1 and s2 have a different length
    g      ?Nr   r   )r   r   r   r   r   r   )r   r   r   r	   r
   r   r   	norm_distr   r   r   normalized_distance}   s   )r    c                C  sX   t   t| st|rdS t| |\} }t| |||d}d| }|du s(||kr*|S dS )aO  
    Calculates a normalized Hamming 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.
    pad : bool, optional
       should strings be padded if there is a length difference.
       If pad is False and strings have a different length
       a ValueError is thrown instead. Defaults is True.
    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

    Raises
    ------
    ValueError
        If s1 and s2 have a different length
    g        r   r	   r   N)r   r   r   r    )r   r   r   r	   r
   r   norm_simr   r   r   normalized_similarity   s   )r#   r!   c          	      C  s  |dur|| } ||}t | |\} }|s#t| t|kr#d}t|g }tt| t|}t|D ]}| | || krE|td|| q2t|t| D ]}|td|t| qMt|t|D ]}|tdt| | qbtt}t| |_	t||_
||_|S )a  
    Return Editops describing how to turn s1 into s2.

    Parameters
    ----------
    s1 : Sequence[Hashable]
        First string to compare.
    s2 : Sequence[Hashable]
        Second string to compare.
    pad : bool, optional
       should strings be padded if there is a length difference.
       If pad is False and strings have a different length
       a ValueError is thrown instead. Defaults is True.
    processor: callable, optional
        Optional callable that is used to preprocess the strings before
        comparing them. Default is None, which deactivates this behaviour.

    Returns
    -------
    editops : Editops
        edit operations required to turn s1 into s2
    Nr   replacedeleteinsert)r   r   r   r   r   appendr   r   __new___src_len	_dest_len_editops)	r   r   r   r	   r   ops_listr   r   opsr   r   r   editops   s,   


r.   c                C  s   t | |||d S )a  
    Return Opcodes describing how to turn s1 into s2.

    Parameters
    ----------
    s1 : Sequence[Hashable]
        First string to compare.
    s2 : Sequence[Hashable]
        Second string to compare.
    pad : bool, optional
       should strings be padded if there is a length difference.
       If pad is False and strings have a different length
       a ValueError is thrown instead. Defaults is True.
    processor: callable, optional
        Optional callable that is used to preprocess the strings before
        comparing them. Default is None, which deactivates this behaviour.

    Returns
    -------
    opcodes : Opcodes
        edit operations required to turn s1 into s2
    r!   )r.   
as_opcodes)r   r   r   r	   r   r   r   opcodes%  s   r0   )
__future__r   rapidfuzz._common_pyr   rapidfuzz._utilsr   r   !rapidfuzz.distance._initialize_pyr   r   r   r   r    r#   r.   r0   r   r   r   r   <module>   s4   A:=8?