
    k*ih              	          S SK Jr  S SKrS SKJrJrJr  S SKJrJ	r	  / SQr
S r\SSSSS.S jr\SSSSS.S	 jr\SS
SSSS.S jrS rS r\	SSSSSSSS.S jr\	SSSSSSSS.S jrg)    )annotationsN)
ScorerFlagis_nonesetupPandas)WRatioratio)cdistextract
extractOneextract_iterc                N    [        U SS 5      nUb  US   " S0 UD6nUS   US   4$ g)N_RF_ScorerPyget_scorer_flagsworst_scoreoptimal_score)r   d    )getattrscorerscorer_kwargsparamsflagss       T/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/rapidfuzz/process_py.py_get_scorer_flags_pyr      sA    V^T2F)*;];m$eO&<==    )r   	processorscore_cutoff
score_hintr   c             #    #    UnU=(       d    0 n[        X&5      u  pX:  n
[        5         [        U 5      (       a  gUc  UnUb  U" U 5      n [        US5      (       a  UR	                  5       O
[        U5      nU H[  u  p[        U5      (       a  M  Uc  U" X4SU0UD6nOU" U U" U5      4SU0UD6nU
(       a  X:  a  XU4v   ML  MN  X::  d  MU  XU4v   M]     g7f)a  
Find the best match in a list of choices

Parameters
----------
query : Sequence[Hashable]
    string we want to find
choices : Iterable[Sequence[Hashable]] | Mapping[Sequence[Hashable]]
    list of all strings the query should be compared with or dict with a mapping
    {<result>: <string to compare>}
scorer : Callable, optional
    Optional callable that is used to calculate the matching score between
    the query and each choice. This can be any of the scorers included in RapidFuzz
    (both scorers that calculate the edit distance or the normalized edit distance), or
    a custom function, which returns a normalized edit distance.
    fuzz.WRatio is used by default.
processor : Callable, optional
    Optional callable that is used to preprocess the strings before
    comparing them. Default is None, which deactivates this behaviour.
score_cutoff : Any, optional
    Optional argument for a score threshold. When an edit distance is used this represents the maximum
    edit distance and matches with a `distance > score_cutoff` are ignored. When a
    normalized edit distance is used this represents the minimal similarity
    and matches with a `similarity < score_cutoff` are ignored. Default is None, which deactivates this behaviour.
score_hint : Any, optional
    Optional argument for an expected score to be passed to the scorer.
    This is used to select a faster implementation. Default is None,
    which deactivates this behaviour.
scorer_kwargs : dict[str, Any], optional
    any other named parameters are passed to the scorer. This can be used to pass
    e.g. weights to `Levenshtein.distance`

Yields
-------
tuple[Sequence[Hashable], Any, Any]
    Yields similarity between the query and each choice in form of a Tuple with 3 elements.
    The values stored in the tuple depend on the types of the input arguments.

    * The first element is always the current `choice`, which is the value that's compared to the query.

    * The second value represents the similarity calculated by the scorer. This can be:

      * An edit distance (distance is 0 for a perfect match and > 0 for non perfect matches).
        In this case only choices which have a `distance <= score_cutoff` are yielded.
        An example of a scorer with this behavior is `Levenshtein.distance`.
      * A normalized edit distance (similarity is a score between 0 and 100, with 100 being a perfect match).
        In this case only choices which have a `similarity >= score_cutoff` are yielded.
        An example of a scorer with this behavior is `Levenshtein.normalized_similarity`.

      Note, that for all scorers, which are not provided by RapidFuzz, only normalized edit distances are supported.

    * The third parameter depends on the type of the `choices` argument it is:

      * The `index of choice` when choices is a simple iterable like a list
      * The `key of choice` when choices is a mapping like a dict, or a pandas Series

Nitemsr   r   r   r   hasattrr!   	enumerate)querychoicesr   r   r   r   r   _r   r   lowest_score_worstchoices_iterkeychoicescores                  r   r   r      s    F 	A!'RM!5f!LK&4Mu~~" % &-gw&?&?7==?YwEWL#6??5U|U}UE&! *  	E $c** % $c**' $s   C	CCc                  UnU=(       d    0 n[        X&5      u  pX:  n
[        5         [        U 5      (       a  gUc  UnUb  U" U 5      n Sn[        US5      (       a  UR	                  5       O
[        U5      nU Ht  u  p[        U5      (       a  M  Uc  U" X4SU0UD6nOU" U U" U5      4SU0UD6nU
(       a  X:  a  Ub  XS   :  a  UnXU4nOX::  a  Ub  XS   :  a  UnXU4nX:X  d  Ms    U$    U$ )aU  
Find the best match in a list of choices. When multiple elements have the same similarity,
the first element is returned.

Parameters
----------
query : Sequence[Hashable]
    string we want to find
choices : Iterable[Sequence[Hashable]] | Mapping[Sequence[Hashable]]
    list of all strings the query should be compared with or dict with a mapping
    {<result>: <string to compare>}
scorer : Callable, optional
    Optional callable that is used to calculate the matching score between
    the query and each choice. This can be any of the scorers included in RapidFuzz
    (both scorers that calculate the edit distance or the normalized edit distance), or
    a custom function, which returns a normalized edit distance.
    fuzz.WRatio is used by default.
processor : Callable, optional
    Optional callable that is used to preprocess the strings before
    comparing them. Default is None, which deactivates this behaviour.
score_cutoff : Any, optional
    Optional argument for a score threshold. When an edit distance is used this represents the maximum
    edit distance and matches with a `distance > score_cutoff` are ignored. When a
    normalized edit distance is used this represents the minimal similarity
    and matches with a `similarity < score_cutoff` are ignored. Default is None, which deactivates this behaviour.
score_hint : Any, optional
    Optional argument for an expected score to be passed to the scorer.
    This is used to select a faster implementation. Default is None,
    which deactivates this behaviour.
scorer_kwargs : dict[str, Any], optional
    any other named parameters are passed to the scorer. This can be used to pass
    e.g. weights to `Levenshtein.distance`

Returns
-------
tuple[Sequence[Hashable], Any, Any]
    Returns the best match in form of a Tuple with 3 elements. The values stored in the
    tuple depend on the types of the input arguments.

    * The first element is always the `choice`, which is the value that's compared to the query.

    * The second value represents the similarity calculated by the scorer. This can be:

      * An edit distance (distance is 0 for a perfect match and > 0 for non perfect matches).
        In this case only choices which have a `distance <= score_cutoff` are returned.
        An example of a scorer with this behavior is `Levenshtein.distance`.
      * A normalized edit distance (similarity is a score between 0 and 100, with 100 being a perfect match).
        In this case only choices which have a `similarity >= score_cutoff` are returned.
        An example of a scorer with this behavior is `Levenshtein.normalized_similarity`.

      Note, that for all scorers, which are not provided by RapidFuzz, only normalized edit distances are supported.

    * The third parameter depends on the type of the `choices` argument it is:

      * The `index of choice` when choices is a simple iterable like a list
      * The `key of choice` when choices is a mapping like a dict, or a pandas Series

None
    When no choice has a `similarity >= score_cutoff`/`distance <= score_cutoff` None is returned

Examples
--------

>>> from rapidfuzz.process import extractOne
>>> from rapidfuzz.distance import Levenshtein
>>> from rapidfuzz.fuzz import ratio

extractOne can be used with normalized edit distances.

>>> extractOne("abcd", ["abce"], scorer=ratio)
("abcd", 75.0, 1)
>>> extractOne("abcd", ["abce"], scorer=Levenshtein.normalized_similarity)
("abcd", 0.75, 1)

extractOne can be used with edit distances as well.

>>> extractOne("abcd", ["abce"], scorer=Levenshtein.distance)
("abce", 1, 0)

additional settings of the scorer can be passed via the scorer_kwargs argument to extractOne

>>> extractOne("abcd", ["abce"], scorer=Levenshtein.distance, scorer_kwargs={"weights":(1,1,2)})
("abcde", 2, 1)

when a mapping is used for the choices the key of the choice is returned instead of the List index

>>> extractOne("abcd", {"key": "abce"}, scorer=ratio)
("abcd", 75.0, "key")

It is possible to specify a processor function which is used to preprocess the strings before comparing them.

>>> extractOne("abcd", ["abcD"], scorer=ratio)
("abcD", 75.0, 0)
>>> extractOne("abcd", ["abcD"], scorer=ratio, processor=utils.default_process)
("abcD", 100.0, 0)
>>> extractOne("abcd", ["abcD"], scorer=ratio, processor=lambda s: s.upper())
("abcD", 100.0, 0)

When only results with a similarity above a certain threshold are relevant, the parameter score_cutoff can be
used to filter out results with a lower similarity. This threshold is used by some of the scorers to exit early,
when they are sure, that the similarity is below the threshold.
For normalized edit distances all results with a similarity below score_cutoff are filtered out

>>> extractOne("abcd", ["abce"], scorer=ratio)
("abce", 75.0, 0)
>>> extractOne("abcd", ["abce"], scorer=ratio, score_cutoff=80)
None

For edit distances all results with an edit distance above the score_cutoff are filtered out

>>> extractOne("abcd", ["abce"], scorer=Levenshtein.distance, scorer_kwargs={"weights":(1,1,2)})
("abce", 2, 0)
>>> extractOne("abcd", ["abce"], scorer=Levenshtein.distance, scorer_kwargs={"weights":(1,1,2)}, score_cutoff=1)
None

Nr!   r      r"   )r%   r&   r   r   r   r   r   r'   r   r   r(   resultr)   r*   r+   r,   s                   r   r   r      s5   | 	A!'RM!5f!LK&4Mu~~" % F&-gw&?&?7==?YwEWL#6??5U|U}UE&! *  	E $&.E1I<M$ -$&.E1I<M$ -!M5 $4 Mr      )r   r   limitr   r   r   c          
        U=(       d    0 n[        X'5      u  pX:  n
US:X  a  [        U UUUUUUS9nUc  / $ U/$ [        U UUUUUUS9nUc  [        US U
S9$ U
(       a  [        R
                  " XLS S9$ [        R                  " XLS S9$ )a  
Find the best matches in a list of choices. The list is sorted by the similarity.
When multiple choices have the same similarity, they are sorted by their index

Parameters
----------
query : Sequence[Hashable]
    string we want to find
choices : Collection[Sequence[Hashable]] | Mapping[Sequence[Hashable]]
    list of all strings the query should be compared with or dict with a mapping
    {<result>: <string to compare>}
scorer : Callable, optional
    Optional callable that is used to calculate the matching score between
    the query and each choice. This can be any of the scorers included in RapidFuzz
    (both scorers that calculate the edit distance or the normalized edit distance), or
    a custom function, which returns a normalized edit distance.
    fuzz.WRatio is used by default.
processor : Callable, optional
    Optional callable that is used to preprocess the strings before
    comparing them. Default is None, which deactivates this behaviour.
limit : int, optional
    maximum amount of results to return. None can be passed to disable this behavior.
    Default is 5.
score_cutoff : Any, optional
    Optional argument for a score threshold. When an edit distance is used this represents the maximum
    edit distance and matches with a `distance > score_cutoff` are ignored. When a
    normalized edit distance is used this represents the minimal similarity
    and matches with a `similarity < score_cutoff` are ignored. Default is None, which deactivates this behaviour.
score_hint : Any, optional
    Optional argument for an expected score to be passed to the scorer.
    This is used to select a faster implementation. Default is None,
    which deactivates this behaviour.
scorer_kwargs : dict[str, Any], optional
    any other named parameters are passed to the scorer. This can be used to pass
    e.g. weights to `Levenshtein.distance`

Returns
-------
list[tuple[Sequence[Hashable], Any, Any]]
    The return type is always a List of Tuples with 3 elements. However the values stored in the
    tuple depend on the types of the input arguments.

    * The first element is always the `choice`, which is the value that's compared to the query.

    * The second value represents the similarity calculated by the scorer. This can be:

      * An edit distance (distance is 0 for a perfect match and > 0 for non perfect matches).
        In this case only choices which have a `distance <= score_cutoff` are returned.
        An example of a scorer with this behavior is `Levenshtein.distance`.
      * A normalized edit distance (similarity is a score between 0 and 100, with 100 being a perfect match).
        In this case only choices which have a `similarity >= score_cutoff` are returned.
        An example of a scorer with this behavior is `Levenshtein.normalized_similarity`.

      Note, that for all scorers, which are not provided by RapidFuzz, only normalized edit distances are supported.

    * The third parameter depends on the type of the `choices` argument it is:

      * The `index of choice` when choices is a simple iterable like a list
      * The `key of choice` when choices is a mapping like a dict, or a pandas Series

    The list is sorted by similarity or distance depending on the scorer used. The first element in the list
    has the `highest similarity`/`smallest distance`.

r.   )r   r   r   r   r   c                    U S   $ Nr.   r   is    r   <lambda>extract.<locals>.<lambda>  s    1r   )r*   reversec                    U S   $ r4   r   r5   s    r   r7   r8     s    !r   )r*   c                    U S   $ r4   r   r5   s    r   r7   r8     s    QqTr   )r   r   r   sortedheapqnlargest	nsmallest)r%   r&   r   r   r1   r   r   r   r   r   r(   resresult_iters                r   r
   r
   /  s    V "'RM!5f!LK&4z%!'
 ;Iu!#K }k~?QRR~~enEE??5>BBr   c                4   SS K nU b  UR                  U 5      $ [        USS 5      nUbe  US   " S0 UD6nUS   [        R                  -  (       a  UR
                  $ US   [        R                  -  (       a  UR                  $ UR                  $ UR                  $ )Nr   r   r   r   r   )	numpydtyper   r   
RESULT_I64int32RESULT_SIZE_Tuint32float32)rD   r   r   npr   r   s         r   _dtype_to_type_numrK     s    
 xxV^T2F)*;];>J11188O>J44499zz::r   c                r    [        U SS 5      nUb'  US   " S0 UD6nUS   [        R                  -  (       a  gg)Nr   r   r   TFr   )r   r   	SYMMETRICr   s       r   _is_symmetricrN     sA    V^T2F)*;];>J000r   r.   )r   r   r   r   score_multiplierrD   workersr   c                  SSK n
X4nU	=(       d    0 n	[        XrU	5      nU
R                  [        U 5      [        U5      4US9n[	        5         Uc  [        U5      nO*U Vs/ s H  n[        U5      (       a  UOU" U5      PM     nnXL a  [        X)5      (       a  [        U5       H  u  nnU" UU4SU0U	D6U-  nU
R                  XzR                  5      (       a  [        U5      nUXU4'   [        US-   [        U5      5       HM  nU" UUU   4SU0U	D6U-  nU
R                  XzR                  5      (       a  [        U5      nU=XU4'   UUU4'   MO     M     U$ [        U 5       H|  u  nnU(       a  [        U5      (       d  U" U5      OUn[        U5       HF  u  nnU" UU4SU0U	D6U-  nU
R                  XzR                  5      (       a  [        U5      nUXU4'   MH     M~     U$ s  snf )a	  
Compute distance/similarity between each pair of the two collections of inputs.

Parameters
----------
queries : Collection[Sequence[Hashable]]
    list of all strings the queries
choices : Collection[Sequence[Hashable]]
    list of all strings the query should be compared
scorer : Callable, optional
    Optional callable that is used to calculate the matching score between
    the query and each choice. This can be any of the scorers included in RapidFuzz
    (both scorers that calculate the edit distance or the normalized edit distance), or
    a custom function, which returns a normalized edit distance.
    fuzz.ratio is used by default.
processor : Callable, optional
    Optional callable that is used to preprocess the strings before
    comparing them. Default is None, which deactivates this behaviour.
score_cutoff : Any, optional
    Optional argument for a score threshold to be passed to the scorer.
    Default is None, which deactivates this behaviour.
score_hint : Any, optional
    Optional argument for an expected score to be passed to the scorer.
    This is used to select a faster implementation. Default is None,
    which deactivates this behaviour.
score_multiplier: Any, optional
    Optional argument to multiply the calculated score with. This is applied as the final step,
    so e.g. score_cutoff is applied on the unmodified score. This is mostly useful to map from
    a floating point range to an integer to reduce the memory usage. Default is 1,
    which deactivates this behaviour.
dtype : data-type, optional
    The desired data-type for the result array. Depending on the scorer type the following
    dtypes are supported:

    - similarity:
      - np.float32, np.float64
      - np.uint8 -> stores fixed point representation of the result scaled to a range 0-100
    - distance:
      - np.int8, np.int16, np.int32, np.int64

    If not given, then the type will be np.float32 for similarities and np.int32 for distances.
workers : int, optional
    The calculation is subdivided into workers sections and evaluated in parallel.
    Supply -1 to use all available CPU cores.
    This argument is only available for scorers using the RapidFuzz C-API so far, since it
    releases the Python GIL.
scorer_kwargs : dict[str, Any], optional
    any other named parameters are passed to the scorer. This can be used to pass
    e.g. weights to `Levenshtein.distance`

Returns
-------
ndarray
    Returns a matrix of dtype with the distance/similarity between each pair
    of the two collections of inputs.
r   NrD   r   r.   )rC   rK   zeroslenr   listr   rN   r$   
issubdtypeintegerroundrange)queriesr&   r   r   r   r   rO   rD   rP   r   rJ   r'   resultsproc_choicesxr6   
proc_queryr,   jr%   r+   s                        r   r	   r	     s   J A!'RMum<EhhGc'l35hAGMG}CJK7aWQZZYq\97KmFBB&|4MAz:z^^P]^aqqE}}UJJ//e!GqDM1q5#l"34"$Q &2 (	 ''  ==

33!%LE05511 5 5R N% "'*HAu.75)UZJ&|4	6" &2 (	 ''  ==

33!%LE %1 5 +$ NY Ls    $G'c                  SSK n
[        U 5      n[        U5      nX:w  a  Sn[        U5      eX4nU	=(       d    0 n	[        XrU	5      nU
R	                  U4US9n[        5         [        [        X5      5       H  u  nu  nnU(       a  [        U5      (       d  U" U5      OUnU(       a  [        U5      (       d  U" U5      OUnU" UU4SU0U	D6nUU-  nU
R                  XzR                  5      (       a  [        U5      nUUU'   M     U$ )a^
  
Compute the pairwise distance/similarity between corresponding elements of the queries & choices.

Parameters
----------
queries : Collection[Sequence[Hashable]]
    list of strings used to compute the distance/similarity.
choices : Collection[Sequence[Hashable]]
    list of strings the queries should be compared with. Must be the same length as the queries.
scorer : Callable, optional
    Optional callable that is used to calculate the matching score between
    the query and each choice. This can be any of the scorers included in RapidFuzz
    (both scorers that calculate the edit distance or the normalized edit distance), or
    a custom function, which returns a normalized edit distance.
    fuzz.ratio is used by default.
processor : Callable, optional
    Optional callable that is used to preprocess the strings before
    comparing them. Default is None, which deactivates this behaviour.
score_cutoff : Any, optional
    Optional argument for a score threshold to be passed to the scorer.
    Default is None, which deactivates this behaviour.
score_hint : Any, optional
    Optional argument for an expected score to be passed to the scorer.
    This is used to select a faster implementation. Default is None,
    which deactivates this behaviour.
score_multiplier: Any, optional
    Optional argument to multiply the calculated score with. This is applied as the final step,
    so e.g. score_cutoff is applied on the unmodified score. This is mostly useful to map from
    a floating point range to an integer to reduce the memory usage. Default is 1,
    which deactivates this behaviour.
dtype : data-type, optional
    The desired data-type for the result array. Depending on the scorer type the following
    dtypes are supported:

    - similarity:
      - np.float32, np.float64
      - np.uint8 -> stores fixed point representation of the result scaled to a range 0-100
    - distance:
      - np.int8, np.int16, np.int32, np.int64

    If not given, then the type will be np.float32 for similarities and np.int32 for distances.
workers : int, optional
    The calculation is subdivided into workers sections and evaluated in parallel.
    Supply -1 to use all available CPU cores.
    This argument is only available for scorers using the RapidFuzz C-API so far, since it
    releases the Python GIL.
scorer_kwargs : dict[str, Any], optional
    any other named parameters are passed to the scorer. This can be used to pass
    e.g. weights to `Levenshtein.distance`

Returns
-------
ndarray
    Returns a matrix of size (n x 1) of dtype with the distance/similarity between each pair
    of the two collections of inputs.
r   Nz/Length of queries and choices must be the same!rR   r   )rC   rT   
ValueErrorrK   rS   r   r$   zipr   rV   rW   rX   )rZ   r&   r   r   r   r   rO   rD   rP   r   rJ   len_querieslen_choiceserror_messager'   r[   r6   r%   r+   r^   proc_choicer,   s                         r   cpdistrg   >  s   J g,Kg,K!I''A!'RMum<Ehh~Uh3GM'G(=>?E6*3GENNYu%QV
,5gfooi'TZ
 &
 	
 	!! ==

++%LE 
% ?( Nr   )
__future__r   r=   rapidfuzz._utilsr   r   r   rapidfuzz.fuzzr   r   __all__r   r   r   r
   rK   rN   r	   rg   r   r   r   <module>rl      s    #  = = (
< h+^ lf 
lC^, 
}H 
ir   