dhp.search package

Module contents

search type utilities

dhp.search.fuzzy_distance(needle, straw)

calculate distance between needle and a straw from the haystack.

Parameters:
  • needle (str) – The thing to match
  • straw (str) – The thing to match against
Returns:

A distance of 0 indicates a search failure on one or more chars in needle. The lower the distance the closer the match, matching earlier and closer together results in a shorter distance.

Return type:

(int)

Return a list of elements from haystack, ranked by distance from needle.

Parameters:
  • needle (str) – The thing to match.
  • haystack (list) – A list of strings to match against.
Returns:

Of strings, ranked by distance, that fuzzy match needle to one

degree or another.

Return type:

(list)

Example:

corpus = ['django_migrations.py',
          'django_admin_log.py',
          'main_generator.py',
          'migrations.py',
          'api_user.doc',
          'user_group.doc',
          'accounts.txt',
          ]
assert fuzzy_search('mig', corpus) == ['migrations.py',
                                       'django_migrations.py',
                                       'main_generator.py',
                                       'django_admin_log.py']