Port cache-`dict` search to new `rapidfuzz` api

distribute_dis
Tyler Goodlet 2023-10-01 17:46:46 -04:00
parent 81a8cd1685
commit e5fdb33e31
1 changed files with 12 additions and 8 deletions

View File

@ -43,7 +43,7 @@ from typing import (
Iterator, Iterator,
) )
import time import time
# from pprint import pformat from pprint import pformat
from rapidfuzz import process as fuzzy from rapidfuzz import process as fuzzy
import trio import trio
@ -1139,21 +1139,25 @@ async def search_simple_dict(
) -> dict[str, Any]: ) -> dict[str, Any]:
tokens = [] tokens: list[str] = []
for key in source: for key in source:
if not isinstance(key, str): match key:
tokens.extend(key) case str():
else:
tokens.append(key) tokens.append(key)
case []:
tokens.extend(key)
# search routine can be specified as a function such # search routine can be specified as a function such
# as in the case of the current app's local symbol cache # as in the case of the current app's local symbol cache
matches = fuzzy.extractBests( matches = fuzzy.extract(
text, text,
tokens, tokens,
score_cutoff=90, score_cutoff=90,
) )
log.info(
'cache search results:\n'
f'{pformat(matches)}'
)
return [item[0] for item in matches] return [item[0] for item in matches]