
    Shw%                        d dl mZ d dlZd dlZ	 ddlmZ n4# e$ r,  ej                    dk    r ej        d           d dl	mZ Y nw xY wddl
mZ ej        ej        ej        d	                                     Zej        ej        ej        d
                                     ZddZej        dd            ZddZddZej        dd            ZddZddZddZddZddZddZdS )    )unicode_literalsN   )StringMatcherPyPyzYUsing slow pure-python SequenceMatcher. Install python-Levenshtein to remove this warning)SequenceMatcher)utilsc                     t          j        | |          \  } }t          d | |          }t          j        d|                                z            S )Nd   )r   make_type_consistentr   intrratio)s1s2ms      U/var/www/html/movieo_spanner_bot/venv/lib/python3.11/site-packages/fuzzywuzzy/fuzz.pyr   r      sG     'B//FBb"%%A:cAGGIIo&&&    c                 2   t          j        | |          \  } }t          |           t          |          k    r| }|}n|}| }t          d||          }|                                }g }|D ]}|d         |d         z
  dk    r|d         |d         z
  nd}|t          |          z   }	|||	         }
t          d||
          }|                                }|dk    r dS |                    |           t          j        dt          |          z            S )zR"Return the ratio of the most similar substring
    as a number between 0 and 100.Nr   r   gףp=
?r
   )	r   r   lenr   get_matching_blocksr   appendr   max)r   r   shorterlongerr   blocksscoresblock
long_startlong_endlong_substrm2rs                r   partial_ratior"      s*    'B//FB
2ww#b''gv..A""$$F F 
 
-21Xa-@A,E,EU1Xa((1
G,Z01T7K88HHJJt8833MM!:cCKK'(((r   Tc                     |rt          j        | |          n| }|                                }d                    t	          |                    }|                                S )z*Return a cleaned string with token sorted.force_ascii )r   full_processsplitjoinsortedstrip)sr%   r'   tstokenssorted_strings         r   _process_and_sortr0   K   s\     <H	N	A;	7	7	7	7QBXXZZF IIfVnn--M   r   c                     t          | ||          }t          |||          }|rt          ||          S t          ||          S )Nr'   )r0   r"   r   )r   r   partialr%   r'   sorted1sorted2s          r   _token_sortr6   Z   sR    KlKKKGKlKKKG 'Wg...Wg&&&r   c                 *    t          | |d||          S )zpReturn a measure of the sequences' similarity between 0 and 100
    but sorting the token before comparing.
    Fr3   r%   r'   r6   r   r   r%   r'   s       r   token_sort_ratior;   e   s     r2u+T`aaaar   c                 *    t          | |d||          S )z}Return the ratio of the most similar substring as a number between
    0 and 100 but sorting the token before comparing.
    Tr8   r9   r:   s       r   partial_token_sort_ratior=   l   s     r2tS_````r   c                    |s| |k    rdS |rt          j        | |          n| }|rt          j        ||          n|}t          j        |          sdS t          j        |          sdS t          |                                          }t          |                                          }|                    |          }	|                    |          }
|                    |          }d                    t          |	                    }d                    t          |
                    }d                    t          |                    }|dz   |z   }|dz   |z   }|	                                }|	                                }|	                                }|rt          }nt          } |||           |||           |||          g}t          |          S )a	  Find all alphanumeric tokens in each string...
        - treat them as a set
        - construct two strings of the form:
            <sorted_intersection><sorted_remainder>
        - take ratios of those two strings
        - controls for unordered partial matchesr
   r$   r   r&   )r   r'   validate_stringsetr(   intersection
differencer)   r*   r+   r"   r   r   )r   r   r3   r%   r'   p1p2tokens1tokens2rA   diff1to2diff2to1sorted_sectsorted_1to2sorted_2to1combined_1to2combined_2to1
ratio_funcpairwises                      r   
_token_setrP   s   s     B"HHs<H	P	BK	8	8	8	8bB<H	P	BK	8	8	8	8bB $$ q $$ q "((**ooG"((**ooG''00L!!'**H!!'**H((6,//00K((6(++,,K((6(++,,K#%3M#%3M ##%%K!''))M!''))M "


 	
;..
;..
=-00H
 x==r   c                 *    t          | |d||          S )NFr8   rP   r:   s       r   token_set_ratiorS      s    b"eS_````r   c                 *    t          | |d||          S )NTr8   rR   r:   s       r   partial_token_set_ratiorU      s    b"dR^____r   c                     |r-t          j        | |          }t          j        ||          }n| }|}t          j        |          sdS t          j        |          sdS t          ||          S )a  
    Quick ratio comparison between two strings.

    Runs full_process from utils on both strings
    Short circuits if either of the strings is empty after processing.

    :param s1:
    :param s2:
    :param force_ascii: Allow only ASCII characters (Default: True)
    :full_process: Process inputs, used here to avoid double processing in extract functions (Default: True)
    :return: similarity ratio
    r$   r   )r   r'   r?   r   )r   r   r%   r'   rC   rD   s         r   QRatiorW      s      <<<<<< $$ q $$ qR==r   c                 (    t          | |d|          S )z
    Unicode quick ratio

    Calls QRatio with force_ascii set to False

    :param s1:
    :param s2:
    :return: similarity ratio
    Fr%   r'   )rW   r   r   r'   s      r   UQRatior[      s     "be,GGGGr   c                 <   |r-t          j        | |          }t          j        ||          }n| }|}t          j        |          sdS t          j        |          sdS d}d}d}t          ||          }	t	          t          t          |          t          |                              t          t          |          t          |                    z  }
|
dk     rd}|
dk    rd	}|rgt          ||          |z  }t          ||d
          |z  |z  }t          ||d
          |z  |z  }t          j        t          |	|||                    S t          ||d
          |z  }t          ||d
          |z  }t          j        t          |	||                    S )aj  
    Return a measure of the sequences' similarity between 0 and 100, using different algorithms.

    **Steps in the order they occur**

    #. Run full_process from utils on both strings
    #. Short circuit if this makes either string empty
    #. Take the ratio of the two processed strings (fuzz.ratio)
    #. Run checks to compare the length of the strings
        * If one of the strings is more than 1.5 times as long as the other
          use partial_ratio comparisons - scale partial results by 0.9
          (this makes sure only full results can return 100)
        * If one of the strings is over 8 times as long as the other
          instead scale by 0.6

    #. Run the other ratio functions
        * if using partial ratio functions call partial_ratio,
          partial_token_sort_ratio and partial_token_set_ratio
          scale all of these by the ratio based on length
        * otherwise call token_sort_ratio and token_set_ratio
        * all token based comparisons are scaled by 0.95
          (on top of any partial scalars)

    #. Take the highest value from these results
       round it and return it as an integer.

    :param s1:
    :param s2:
    :param force_ascii: Allow only ascii characters
    :type force_ascii: bool
    :full_process: Process inputs, used here to avoid double processing in extract functions (Default: True)
    :return:
    r$   r   Tgffffff?g?g      ?F   g333333?r2   )r   r'   r?   r   floatr   r   minr"   r=   rU   r   r;   rS   )r   r   r%   r'   rC   rD   try_partialunbase_scalepartial_scalebase	len_ratior3   ptsorptsertsortsers                   r   WRatiori      s   F  <<<<<< $$ q $$ q KLMR==Dc#b''3r77++,,s3r77CGG/D/DDI 3 1}} 1B''-7(ReDDD*+'BUCCC*+ z#dGUE::;;;BU;;;lJr2E:::\Iz#dD$//000r   c                 (    t          | |d|          S )zReturn a measure of the sequences' similarity between 0 and 100,
    using different algorithms. Same as WRatio but preserving unicode.
    FrY   )ri   rZ   s      r   UWRatiork   .  s     "be,GGGGr   )T)TTT)TT)
__future__r   platformwarningsr   r   ImportErrorpython_implementationwarndifflib r   check_for_nonecheck_for_equivalencecheck_empty_stringr   r"   r0   r6   r;   r=   rP   rS   rU   rW   r[   ri   rk    r   r   <module>rx      sR   ( ' ' ' ' '  (??????? ( ( (%x%''611qrrr''''''''(
       ' '   ' ") ")   ")R! ! ! ! ' ' ' 'b b b ba a a a 1 1 1 1ha a a a` ` ` `   :
H 
H 
H 
HK1 K1 K1 K1\H H H H H Hs    .AA