
    Ni-                        S SK Jr  S SK Jr  SSKrSSKJr  SSKJr  SSKJ	r	  \R                  " \5      r\R                  r\R                  rS r\R$                  \R$                  \R&                  \R&                  \R(                  \R(                  \R*                  \R*                  \R,                  \R,                  \R.                  \R.                  \R                  \R                  \R0                  \R0                  \R2                  \R                  \R4                  \R0                  0
rS rS	 r\\S4S
 jr\\S4S jr\\SS4S jr \\S4S jr!S\R(                  4S jr"g)   )fuzz)utils    N)process)partialc           	        ^ ^ U[         R                  [         R                  [         R                  [         R                  [         R
                  [         R                  [         R                  [         R                  4;  a  T $ U[         R                  [         R                  4;  n[        [        R                  US9mT (       a  T [        R                  :X  a  T$ UU 4S jnU$ )z
thefuzz runs both the default preprocessing of the function and the preprocessing
function passed into process.* while rapidfuzz only runs the one passed into
process.*. This function wraps the processor to mimic this behavior
)force_asciic                     > T" T" U 5      5      $ N )spre_processor	processors    N/var/www/html/land-tabula/venv/lib/python3.13/site-packages/thefuzz/process.pywrapper_get_processor.<locals>.wrapper!   s    Yq\**    )r   WRatioQRatiotoken_set_ratiotoken_sort_ratiopartial_token_set_ratiopartial_token_sort_ratioUWRatioUQRatior   r   full_process)r   scorerr	   r   r   s   `   @r   _get_processorr      s     dkk4;;**D,A,A22D4Q4QllDLL2 2 t|| <<KE..KHM	U%7%77+ Nr   c                 @   ^  SU 4S jjn[         R                  T U5      $ )zr
rapidfuzz scorers require the score_cutoff argument to be available
This generates a compatible wrapper function
c                    > T" X5      $ r   r   )s1s2score_cutoffr   s      r   r   _get_scorer.<locals>.wrapper@   s    b~r   )r   )_scorer_loweringget)r   r   s   ` r   _get_scorerr'   ;   s    
 00r   c                 x    U(       a  U" U 5      OU n[        U5      S:X  a  [        R                  SU  S35        U$ )Nr   zcApplied processor reduces input query to empty string, all comparisons will have score 0. [Query: 'z'])len_loggerwarning)queryr   processed_querys      r   _preprocess_queryr.   F   sB    *3i&O
?q  %%*G30 	1 r   c              #     #    [        US5      nU[        ;   n[        X5      n [        R                  " X[        X#5      [        U5      US9nU H1  u  pn
U(       a  [        [        U	5      5      n	U(       a  XU
4OX4v   M3     g7f)a  
Select the best match in a list or dictionary of choices.

Find best matches in a list or dictionary of choices, return a
generator of tuples containing the match and its score. If a dictionary
is used, also returns the key for each match.

Arguments:
    query: An object representing the thing we want to find.
    choices: An iterable or dictionary-like object containing choices
        to be matched against the query. Dictionary arguments of
        {key: value} pairs will attempt to match the query against
        each value.
    processor: Optional function of the form f(a) -> b, where a is the query or
        individual choice and b is the choice to be used in matching.

        This can be used to match against, say, the first element of
        a list:

        lambda x: x[0]

        Defaults to thefuzz.utils.full_process().
    scorer: Optional function for scoring matches between the query and
        an individual processed choice. This should be a function
        of the form f(query, choice) -> int.

        By default, fuzz.WRatio() is used and expects both query and
        choice to be strings.
    score_cutoff: Optional argument for score threshold. No matches with
        a score less than this number will be returned. Defaults to 0.

Returns:
    Generator of tuples containing the match and its score.

    If a list is used for choices, then the result will be 2-tuples.
    If a dictionary is used, then the result will be 3-tuples containing
    the key for each match.

    For example, searching for 'bird' in the dictionary

    {'bard': 'train', 'dog': 'man'}

    may return

    ('train', 22, 'bard'), ('man', 0, 'dog')
itemsr   r   r#   N)	hasattrr%   r.   rprocessextract_iterr   r'   intround)r,   choicesr   r   r#   
is_mapping
is_lowereditchoicescorekeys              r   extractWithoutOrderr>   P   s     ^ '*J++Je/E			 36"!	
B !se%E&0vc"voE	 !s   BB   c                     [        XX#US9$ )aF  
Select the best match in a list or dictionary of choices.

Find best matches in a list or dictionary of choices, return a
list of tuples containing the match and its score. If a dictionary
is used, also returns the key for each match.

Arguments:
    query: An object representing the thing we want to find.
    choices: An iterable or dictionary-like object containing choices
        to be matched against the query. Dictionary arguments of
        {key: value} pairs will attempt to match the query against
        each value.
    processor: Optional function of the form f(a) -> b, where a is the query or
        individual choice and b is the choice to be used in matching.

        This can be used to match against, say, the first element of
        a list:

        lambda x: x[0]

        Defaults to thefuzz.utils.full_process().
    scorer: Optional function for scoring matches between the query and
        an individual processed choice. This should be a function
        of the form f(query, choice) -> int.
        By default, fuzz.WRatio() is used and expects both query and
        choice to be strings.
    limit: Optional maximum for the number of elements returned. Defaults
        to 5.

Returns:
    List of tuples containing the match and its score.

    If a list is used for choices, then the result will be 2-tuples.
    If a dictionary is used, then the result will be 3-tuples containing
    the key for each match.

    For example, searching for 'bird' in the dictionary

    {'bard': 'train', 'dog': 'man'}

    may return

    [('train', 22, 'bard'), ('man', 0, 'dog')]
)r   r   limit)extractBests)r,   r7   r   r   rA   s        r   extractrC      s    \ )RWXXr   c           	          [        US5      nU[        ;   n[        X5      n [        R                  " X[        X#5      [        U5      UUS9n[        U5       H4  u  n	u  pnU(       a  [        [        U5      5      nU(       a  XU4OX4X'   M6     U$ )a  
Get a list of the best matches to a collection of choices.

Convenience function for getting the choices with best scores.

Args:
    query: A string to match against
    choices: A list or dictionary of choices, suitable for use with
        extract().
    processor: Optional function for transforming choices before matching.
        See extract().
    scorer: Scoring function for extract().
    score_cutoff: Optional argument for score threshold. No matches with
        a score less than this number will be returned. Defaults to 0.
    limit: Optional maximum for the number of elements returned. Defaults
        to 5.

Returns: A a list of (match, score) tuples.
r0   )r   r   r#   rA   )
r2   r%   r.   r3   rC   r   r'   	enumerater5   r6   )r,   r7   r   r   r#   rA   r8   r9   resultsir;   r<   r=   s                r   rB   rB      s    ( '*J++Je/E 36"!G $-W#5F3e%E-7fS)f_
	 $6 Nr   c                     [        US5      nU[        ;   n[        X5      n [        R                  " X[        X#5      [        U5      US9nUc  U$ Uu  pn
U(       a  [        [        U	5      5      n	U(       a  XU
4$ X4$ )a   
Find the single best match above a score in a list of choices.

This is a convenience method which returns the single best choice.
See extract() for the full arguments list.

Args:
    query: A string to match against
    choices: A list or dictionary of choices, suitable for use with
        extract().
    processor: Optional function for transforming choices before matching.
        See extract().
    scorer: Scoring function for extract().
    score_cutoff: Optional argument for score threshold. If the best
        match is found, but it is not greater than this number, then
        return None anyway ("not a good enough match").  Defaults to 0.

Returns:
    A tuple containing a single match and its score, if a match
    was found that was above score_cutoff. Otherwise, returns None.
r0   r1   )	r2   r%   r.   r3   
extractOner   r'   r5   r6   )r,   r7   r   r   r#   r8   r9   resr;   r<   r=   s              r   rI   rI      s    , '*J++Je/E


 36"!	C {
F3E%L!#-F3BF?Br   F   c           	          [        5       nU  H+  n[        X@X!SS9nUR                  [        US S9S   5        M-     [	        U5      [	        U 5      :w  a  [        U5      $ U $ )a  
This convenience function takes a list of strings containing duplicates and uses fuzzy matching to identify
and remove duplicates. Specifically, it uses process.extract to identify duplicates that
score greater than a user defined threshold. Then, it looks for the longest item in the duplicate list
since we assume this item contains the most entity information and returns that. It breaks string
length ties on an alphabetical sort.

Note: as the threshold DECREASES the number of duplicates that are found INCREASES. This means that the
    returned deduplicated list will likely be shorter. Raise the threshold for dedupe to be less
    sensitive.

Args:
    contains_dupes: A list of strings that we would like to dedupe.
    threshold: the numerical value (0,100) point at which we expect to find duplicates.
        Defaults to 70 out of 100
    scorer: Optional function for scoring matches between the query and
        an individual processed choice. This should be a function
        of the form f(query, choice) -> int.
        By default, fuzz.token_set_ratio() is used and expects both query and
        choice to be strings.

Returns:
    A deduplicated list. For example:

        In: contains_dupes = ['Frodo Baggin', 'Frodo Baggins', 'F. Baggins', 'Samwise G.', 'Gandalf', 'Bilbo Baggins']
        In: dedupe(contains_dupes)
        Out: ['Frodo Baggins', 'Samwise G.', 'Bilbo Baggins', 'Gandalf']
N)r   r#   rA   c                 (    [        U S   5      U S   4$ )Nr   )r)   )xs    r   <lambda>dedupe.<locals>.<lambda>7  s    AaD	1Q4/@r   )r=   r   )setrB   addmaxr)   list)contains_dupes	thresholdr   dedupeditemmatchess         r   deduperZ     s^    : eGtFbfgC%@A!DE   LC,??4=S^Sr   )# r   r   logging	rapidfuzzrfuzzr   r3   	functoolsr   	getLogger__name__r*   r   default_scorerr   default_processorr   ratiopartial_ratior   r   r   r   r   r   r   r%   r'   r.   r>   rC   rB   rI   rZ   r   r   r   <module>rf      sE      # ) 


H
%&& > 	JJ++%//511  %"?"?!!5#A#AKKKKLL%,,LL%,, 1 3DNij >FB '8VW .Yb ,=^bckl &R *;>`a )CX &(0D0D "Tr   