2023-12-08 21:52:26 +00:00
|
|
|
package projects
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/lithammer/fuzzysearch/fuzzy"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Performs a fuzzy find on the input string, returning the closest
|
|
|
|
// matched based on its Levenshtein distance, along with an integer
|
|
|
|
// indicating number of matches found
|
|
|
|
func (c *Cache) FuzzyFindAlias(name string) []*ProjectAlias {
|
2023-12-20 16:23:11 +00:00
|
|
|
ranks := fuzzy.RankFindFold(name, c.AliasStrings(""))
|
2023-12-08 21:52:26 +00:00
|
|
|
if ranks.Len() == 1 {
|
|
|
|
c.log.Debug("Fuzzy found alias result",
|
|
|
|
c.log.Args(
|
|
|
|
"searchTerm", ranks[0].Source,
|
|
|
|
"foundAlias", ranks[0].Target,
|
|
|
|
"levenshteinDistance", ranks[0].Distance,
|
|
|
|
))
|
|
|
|
} else if ranks.Len() > 1 {
|
|
|
|
found := make([]string, ranks.Len())
|
|
|
|
for i, r := range ranks {
|
|
|
|
found[i] = r.Target
|
|
|
|
}
|
2023-12-10 04:45:30 +00:00
|
|
|
c.log.Debug("Fuzzy found multiple aliases, try being more specific",
|
2023-12-09 04:13:17 +00:00
|
|
|
c.log.Args("foundAliases", strings.Join(found, ", ")))
|
2023-12-08 21:52:26 +00:00
|
|
|
}
|
|
|
|
var aliases []*ProjectAlias
|
|
|
|
if ranks.Len() > 0 {
|
|
|
|
aliases = make([]*ProjectAlias, ranks.Len())
|
|
|
|
for i, r := range ranks {
|
|
|
|
aliases[i] = c.GetAliasByName(r.Target)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return aliases
|
|
|
|
}
|