github.com/d-luu/terraform@v0.11.12-beta1/helper/didyoumean/name_suggestion.go (about)

     1  package didyoumean
     2  
     3  import (
     4  	"github.com/agext/levenshtein"
     5  )
     6  
     7  // NameSuggestion tries to find a name from the given slice of suggested names
     8  // that is close to the given name and returns it if found. If no suggestion
     9  // is close enough, returns the empty string.
    10  //
    11  // The suggestions are tried in order, so earlier suggestions take precedence
    12  // if the given string is similar to two or more suggestions.
    13  //
    14  // This function is intended to be used with a relatively-small number of
    15  // suggestions. It's not optimized for hundreds or thousands of them.
    16  func NameSuggestion(given string, suggestions []string) string {
    17  	for _, suggestion := range suggestions {
    18  		dist := levenshtein.Distance(given, suggestion, nil)
    19  		if dist < 3 { // threshold determined experimentally
    20  			return suggestion
    21  		}
    22  	}
    23  	return ""
    24  }