4 "github.com/agext/levenshtein"
7 var keywords = []string{"false", "true", "null"}
9 // keywordSuggestion tries to find a valid JSON keyword that is close to the
10 // given string and returns it if found. If no keyword is close enough, returns
12 func keywordSuggestion(given string) string {
13 return nameSuggestion(given, keywords)
16 // nameSuggestion tries to find a name from the given slice of suggested names
17 // that is close to the given name and returns it if found. If no suggestion
18 // is close enough, returns the empty string.
20 // The suggestions are tried in order, so earlier suggestions take precedence
21 // if the given string is similar to two or more suggestions.
23 // This function is intended to be used with a relatively-small number of
24 // suggestions. It's not optimized for hundreds or thousands of them.
25 func nameSuggestion(given string, suggestions []string) string {
26 for _, suggestion := range suggestions {
27 dist := levenshtein.Distance(given, suggestion, nil)
28 if dist < 3 { // threshold determined experimentally