// SuggestCommands can be used to match a given word // which is very similar to one among the list of available commands. func SuggestCommands(given string, available []string) []string { commandSet := make(map[string]struct{}) for _, command := range available { commandSet[command] = struct{}{} } if len(commandSet) == 0 || given == "" { return nil } if _, ok := commandSet[given]; ok { return nil } var scores cmdScores for command := range commandSet { levenshtein := smetrics.WagnerFischer(command, given, 1, 1, 1) jaroWinkler := smetrics.JaroWinkler(command, given, 0.7, 4) if levenshtein > levenThreshold { continue } if jaroWinkler < jaroThreshold { continue } scores = append( scores, cmdScore{ name: command, levenshtein: levenshtein, jaroWinkler: jaroWinkler, }, ) } if len(scores) == 0 { return nil } sort.Sort(scores) levenshtein := scores[0].levenshtein var matches []string for _, score := range scores { if score.levenshtein != levenshtein { break } matches = append(matches, score.name) } return matches }
// MostSimilarLineTo returns the most similar line in the output to the given // string, if any of them have a JaroWinkler score >0.1. It returns the string // (or empty), the index of that line, and a bool indicating if the score was // greater than 0.1 func (out TestOutput) MostSimilarLineTo(s string) ( winner string, index int, goodMatch bool) { index = -1 if s == "" { return } max := 0.0 for i, l := range out.Lines() { score := smetrics.JaroWinkler(l, s, 0.7, 4) if score > max { winner = l index = i max = score } } return winner, index, max > 0.1 }