Created
July 10, 2013 18:13
-
-
Save Grokzen/5968698 to your computer and use it in GitHub Desktop.
Tiny lib that will perform a simple version of fuzzy string matching, similar to what sublime text use.
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 difflib | |
def match(key, search_for): | |
a = [char for char in search_for] | |
index = 0 | |
for char in key: | |
if a[index] == char: | |
index += 1 | |
if index == len(a): | |
break | |
return index == len(a) | |
def fuzzy_match(keys, search_string): | |
return [(match(key, search_string.replace(" ", "")), difflib.SequenceMatcher(None, key, search_string.replace(" ", "")).ratio(), key) for key in keys] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment