suggest_similar

bpod_core.misc.suggest_similar(invalid_string, valid_strings, format_string=" - did you mean '{}'?", cutoff=0.6)

Suggest a similar valid string based on the given invalid string.

This function uses a similarity matching algorithm to find the closest match from an iterable of valid strings. If a match is found above the specified cutoff, it returns a formatted suggestion string.

Parameters:
  • invalid_string (str) – The string that is invalid or misspelled.

  • valid_strings (Iterable of str) – An iterable of valid strings to compare against.

  • format_string (str, default: " - did you mean '{}'?") – The format string for the suggestion.

  • cutoff (float, default: 0.6) – The similarity threshold for considering a match.

Returns:

A formatted suggestion string if a match is found, otherwise an empty string.

Return type:

str

Examples

>>> 'no such port' + suggest_similar('Prot1', ['Port1', 'Port2'])
"no such port - did you mean 'Port1'?"
>>> 'no such port' + suggest_similar('xyz', ['Port1', 'Port2'])
'no such port'