Skip to content

Instantly share code, notes, and snippets.

@kzinmr
Created May 21, 2020 10:57
Show Gist options
  • Save kzinmr/cf10d4ea6055adad3f51ca11b849c755 to your computer and use it in GitHub Desktop.
Save kzinmr/cf10d4ea6055adad3f51ca11b849c755 to your computer and use it in GitHub Desktop.
fuzzy matching using https://pypi.org/project/regex/
import regex
from typing import List, Tuple
def fuzzy_match_spans(annotation: str, text: str, max_num_errors: int = 5) -> List[Tuple[int, int]]:
r = regex.compile('(%s)' % re.escape(annotation))
matches = [m.span() for m in r.finditer(text)]
num_errors = 1
while not matches and num_errors <= max_num_errors:
r = regex.compile('(%s){e<=%d}' % (re.escape(annotation), num_errors))
matches = [m.span() for m in r.finditer(text)]
num_errors += 1
return matches
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment