Install fuzzysearch
masterTo use the fuzzysearch library in your Go project, install the fuzzy package using go get:
go get github.com/lithammer/fuzzysearch/fuzzyrepository·master·Indexed 23 days ago
https://github.com/lithammer/fuzzysearchA Go library for fuzzy string matching that supports pattern matching, ranking via Levenshtein distance, and searching through lists of words. It includes functions for basic matching, ranking, and specialized Unicode normalized or case-insensitive matching.
To use the fuzzysearch library in your Go project, install the fuzzy package using go get:
go get github.com/lithammer/fuzzysearch/fuzzyUse fuzzy.RankMatch(pattern, target) to get a ranking score for a match based on Levenshtein distance. It returns a score (integer) representing the quality of the match, or -1 if no match is found.
import "github.com/lithammer/fuzzysearch/fuzzy"
// Returns a score for the match
fuzzy.RankMatch("cart", "cartwheel") // 5
fuzzy.RankMatch("kitten", "sitting") // -1fuzzy.RankFind(pattern, words) to search through a slice of strings and return detailed match information. Each result includes the pattern, the matched word, a score, and an index. The results can be sorted using the standard library sort package.The library provides specialized matching functions for specific requirements:
fuzzy.MatchNormalized(target, pattern): Performs matching using Unicode normalization (e.g., matching 'e' with 'é').fuzzy.MatchFold(pattern, target): Performs case-insensitive matching using Unicode folding.import "github.com/lithammer/fuzzysearch/fuzzy"
// Unicode normalized matching
fuzzy.MatchNormalized("cartwheel", "cartwhéél") // true
// Case insensitive matching
fuzzy.MatchFold("ArTeeL", "cartwheel") // trueUse fuzzy.Find(pattern, words) to search through a slice of strings. It returns a slice of strings containing only the words that match the pattern.
import "github.com/lithammer/fuzzysearch/fuzzy"
words := []string{"cartwheel", "foobar", "wheel", "baz"}
matches := fuzzy.Find("whl", words) // []string{"cartwheel", "wheel"}Use fuzzy.Match(pattern, target) to check if a pattern exists within a target string. It returns true if the pattern can be found in the target string by skipping characters, and false otherwise.
import "github.com/lithammer/fuzzysearch/fuzzy"
// Returns true if the pattern matches
fuzzy.Match("twl", "cartwheel") // true
fuzzy.Match("eeel", "cartwheel") // false