Skip to content

Instantly share code, notes, and snippets.

@amjith
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amjith/a9e295e8dbff575c4cde to your computer and use it in GitHub Desktop.
Save amjith/a9e295e8dbff575c4cde to your computer and use it in GitHub Desktop.
>>> import re # regex module from standard library.
>>> def fuzzyfinder(user_input, collection):
suggestions = []
pattern = '.*?'.join(user_input) # Converts 'djm' to 'd.*?j.*?m'
regex = re.compile(pattern) # Compiles a regex.
for item in collection:
match = regex.search(item) # Checks if the current item matches the regex.
if match:
suggestions.append((len(match.group()), match.start(), item))
return [x for _, _, x in sorted(suggestions)]
>>> fuzzyfinder('user', collection)
['user_group.doc', 'api_user.doc']
>>> print fuzzyfinder('mig', collection)
['migrations.py', 'django_migrations.py', 'main_generator.py', 'django_admin_log.py']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment