
    ,jW                         d Z ddlZddlZddlmZmZmZmZmZm	Z	 ddl
mZmZ ddlmZ deeegef   dedeeeeeef   f      d	ed
eeeef      f
dZ	 	 	 ddeded	ededeeegef   d
eeeef      fdZy)zNModule contains the async interface to match needle against haystack in batch.    N)AnyCallableDictListUnioncast)SCORE_INDICES
fzy_scorer)	HAYSTACKSscorerneedle	haystackskeyreturnc                    K   g }|D ]7  } | |t        t        |      |         \  }}|#|j                  |||d       9 |j                  d d       |S w)a  Calculate the score for needle against given list of haystacks and rank them.

    Args:
        scorer: Scorer to be used to do the calculation.
        needle: Substring to search.
        haystacks: List of dictionary containing the haystack to be searched.
        key: The key within the `haystacks` dictionary that contains the actual string value.

    Return:
        Sorted list of haystacks based on the needle score with additional keys for the `score`
        and `indices`.
    )scoreindiceshaystackc                     | d   S Nr    xs    L/home/ubuntu/waydroid_script/venv/lib/python3.12/site-packages/pfzy/match.py<lambda>z_rank_task.<locals>.<lambda>(   s
    aj     Tr   reverse)r   r   appendsort)r   r   r   r   resultr   r   r   s           r   
_rank_taskr"   
   sr     $ F 

T8(<S(ABw?"$	
	

 KK($K7Ms   AA
batch_sizec           	         K   t         t              D ]!  \  }}t        |t              rsd|i|<   # st	        dt
        j                   d      t        j                   fdt        dt                    D          d{   }t        j                  |d dd	}g }	|D ]!  }
|
d
   |
d   d
<   |	j                  |
d          # |	S 7 Ew)a  Fuzzy find the needle within list of haystacks and get matched results with matching index.

    Note:
        The `key` argument is optional when the provided `haystacks` argument is a list of :class:`str`.
        It will be given a default key `value` if not present.

    Warning:
        The `key` argument is required when provided `haystacks` argument is a list of :class:`dict`.
        If not present, :class:`TypeError` will be raised.

    Args:
        needle: String to search within the `haystacks`.
        haystacks: List of haystack/longer strings to be searched.
        key: If `haystacks` is a list of dictionary, provide the key that
            can obtain the haystack value to search.
        batch_size: Number of entry to be processed together.
        scorer (Callable[[str, str], SCORE_indices]): Desired scorer to use. Currently only :func:`~pfzy.score.fzy_scorer` and :func:`~pfzy.score.substr_scorer` is supported.

    Raises:
        TypeError: When the argument `haystacks` is :class:`list` of :class:`dict` and the `key` argument
            is missing, :class:`TypeError` will be raised.

    Returns:
        List of matching `haystacks` with additional key indices and score.

    Examples:
        >>> import asyncio
        >>> asyncio.run(fuzzy_match("ab", ["acb", "acbabc"]))
        [{'value': 'acbabc', 'indices': [3, 4]}, {'value': 'acb', 'indices': [0, 2]}]
    Nvalue$z\ missing 1 required argument: 'key', 'key' is required when haystacks is an instance of dictc           	   3   F   K   | ]  }t        ||z            y w)N)r"   ).0offsetr#   r   r   r   r   s     r   	<genexpr>zfuzzy_match.<locals>.<genexpr>`   s8      

  &6J#67	

s   !r   c                     | d   S r   r   r   s    r   r   zfuzzy_match.<locals>.<lambda>j   s
    !G* r   Tr   r   r   )r
   	enumerate
isinstancedict	TypeErrorfuzzy_match__name__asynciogatherrangelenheapqmerger   )r   r   r   r#   r   indexr   batchesresultschoices	candidates   `````      r   r0   r0   ,   s    J ~$Y/ /x(D) #XIe	/ $$%  &B  C
 	
 NN

  3y>:>


 
G kk7(<dKGG .	+4Y+?	*i(y,-. N!
s   *CA"CCAC) i   N)__doc__r2   r6   typingr   r   r   r   r   r   
pfzy.scorer	   r
   
pfzy.typesr   strr"   intr0   r   r   r   <module>rD      s    T   9 9 0  c3Z./ E#tCH~-./ 
	
 
$sCx.J 26CCC 
C 	C
 c3Z./C 
$sCx.Cr   