Created
May 21, 2020 10:57
-
-
Save kzinmr/cf10d4ea6055adad3f51ca11b849c755 to your computer and use it in GitHub Desktop.
fuzzy matching using https://pypi.org/project/regex/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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