Coverage for geoparser/db/functions/levenshtein.py: 100%

4 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-20 18:08 +0000

1""" 

2Levenshtein edit distance for fuzzy matching ranking. 

3""" 

4 

5from rapidfuzz import utils 

6from rapidfuzz.distance import Levenshtein 

7 

8 

9def levenshtein(query: str, candidate: str) -> int: 

10 """ 

11 Compute the Levenshtein edit distance between two strings using rapidfuzz. 

12 

13 The distance counts the minimum number of single-character insertions, 

14 deletions, and substitutions required to turn one string into the other. 

15 Lower values indicate better matches, which matches the ascending-order 

16 ranking used for fuzzy search. Comparison is case-insensitive and ignores 

17 non-alphanumeric characters. 

18 

19 Args: 

20 query: Search string. May be None. 

21 candidate: Candidate string to compare against. May be None. 

22 

23 Returns: 

24 Non-negative edit distance, where 0 is an exact match. 

25 """ 

26 return Levenshtein.distance( 

27 query or "", candidate or "", processor=utils.default_process 

28 )