Coverage for agentlib/utils/fuzzy_matching.py: 90%

10 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2025-04-07 16:27 +0000

1from typing import Iterable, Union, List 

2 

3try: 

4 from rapidfuzz import process as fuz_process 

5 from rapidfuzz import fuzz 

6 

7 RAPIDFUZZ_IS_INSTALLED = True 

8except ImportError: 

9 RAPIDFUZZ_IS_INSTALLED = False 

10 

11 

12def fuzzy_match(target: str, choices: Iterable[str]) -> Union[None, List[str]]: 

13 if not RAPIDFUZZ_IS_INSTALLED: 

14 return None 

15 matches = fuz_process.extract(query=target, choices=choices, scorer=fuzz.WRatio) 

16 return [m[0] for m in matches]