diff options
| author | Martin Atkins <mart@degeneration.co.uk> | 2019-09-09 16:08:19 -0700 |
|---|---|---|
| committer | Martin Atkins <mart@degeneration.co.uk> | 2019-09-09 16:08:19 -0700 |
| commit | 6c4344623b6ac528a57f9b80e4622acfab2fde40 (patch) | |
| tree | 83031084d3ab54abbe40e3fd749e439c8b28e9d1 /didyoumean.go | |
| parent | 0f5ab3bd563c111077917020c0cbc8c211e1bff3 (diff) | |
Unfold the "hcl" directory up into the root
The main HCL package is more visible this way, and so it's easier than
having to pick it out from dozens of other package directories.
Diffstat (limited to 'didyoumean.go')
| -rw-r--r-- | didyoumean.go | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/didyoumean.go b/didyoumean.go new file mode 100644 index 0000000..c128334 --- /dev/null +++ b/didyoumean.go @@ -0,0 +1,24 @@ +package hcl + +import ( + "github.com/agext/levenshtein" +) + +// 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 to two or more suggestions. +// +// This function is intended to be used with a relatively-small number of +// suggestions. It's not optimized for hundreds or thousands of them. +func nameSuggestion(given string, suggestions []string) string { + for _, suggestion := range suggestions { + dist := levenshtein.Distance(given, suggestion, nil) + if dist < 3 { // threshold determined experimentally + return suggestion + } + } + return "" +} |
