nameSuggestion tries to find a name from the given slice of suggested names that is close to the given name and returns it if found. If no suggestion is close enough, returns the empty string. The suggestions are tried in order, so earlier suggestions take precedence if the given string is similar
(given string, suggestions []string)
| 17 | // This function is intended to be used with a relatively-small number of |
| 18 | // suggestions. It's not optimized for hundreds or thousands of them. |
| 19 | func nameSuggestion(given string, suggestions []string) string { |
| 20 | for _, suggestion := range suggestions { |
| 21 | dist := levenshtein.Distance(given, suggestion, nil) |
| 22 | if dist < 3 { // threshold determined experimentally |
| 23 | return suggestion |
| 24 | } |
| 25 | } |
| 26 | return "" |
| 27 | } |