suggest#

suggest(user_input, valid_inputs, n=1, threshold=None, fulloutput=False, die=False, which='damerau')[source]#

Return suggested item

Returns item with lowest Levenshtein distance, where case substitution and stripping whitespace are not included in the distance. If there are ties, then the additional operations will be included.

Parameters:
  • user_input (str) – User’s input

  • valid_inputs (list) – List/collection of valid strings

  • n (int) – Maximum number of suggestions to return

  • threshold (int) – Maximum number of edits required for an option to be suggested (by default, two-thirds the length of the input; for no threshold, set to -1)

  • fulloutput (bool) – Whether to return suggestions and distances.

  • die (bool) – If True, an informative error will be raised (to avoid having to implement this in the calling code)

  • which (str) – Distance calculation method used; options are “damerau” (default), “levenshtein”, or “jaro”

Returns:

Suggested string. Returns None if no suggestions with edit distance less than threshold were found. This helps to make

suggestions more relevant.

Return type:

suggestions (str or list)

Examples:

>>> sc.suggest('foo', ['Foo','Bar'])
'Foo'
>>> sc.suggest('foo', ['FOO','Foo'])
'Foo'
>>> sc.suggest('foo', ['Foo ','boo'])
'Foo '