Documentation
¶
Overview ¶
Package strutil provides string metrics for calculating string similarity as well as other string utility functions.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CommonPrefix ¶
CommonPrefix returns the common prefix of the specified strings. An empty string is returned if the parameters have no prefix in common.
Example ¶
package main import ( "fmt" "github.com/adrg/strutil" ) func main() { fmt.Println("(answer, anvil):", strutil.CommonPrefix("answer", "anvil")) }
Output: (answer, anvil): an
func Similarity ¶
func Similarity(a, b string, metric StringMetric) float64
Similarity returns the similarity of a and b, computed using the specified string metric. The returned similarity is a number between 0 and 1. Larger similarity numbers indicate closer matches.
Example ¶
package main import ( "fmt" "github.com/adrg/strutil" "github.com/adrg/strutil/metrics" ) func main() { sim := strutil.Similarity("riddle", "needle", metrics.NewJaroWinkler()) fmt.Printf("(riddle, needle) similarity: %.2f\n", sim) }
Output: (riddle, needle) similarity: 0.56
func SliceContains ¶
SliceContains returns true if terms contains q, or false otherwise.
Example ¶
package main import ( "fmt" "github.com/adrg/strutil" ) func main() { terms := []string{"a", "b", "c"} fmt.Println("([a b c], b):", strutil.SliceContains(terms, "b")) fmt.Println("([a b c], d):", strutil.SliceContains(terms, "d")) }
Output: ([a b c], b): true ([a b c], d): false
func UniqueSlice ¶
UniqueSlice returns a slice containing the unique items from the specified string slice. The items in the output slice are in the order in which they occur in the input slice.
Example ¶
package main import ( "fmt" "github.com/adrg/strutil" ) func main() { sample := []string{"a", "b", "a", "b", "b", "c"} fmt.Println("[a b a b b c]:", strutil.UniqueSlice(sample)) }
Output: [a b a b b c]: [a b c]
Types ¶
type StringMetric ¶
StringMetric represents a metric for measuring the similarity between strings. The metrics package implements the following string metrics:
- Jaro
- Jaro-Winkler
- Levenshtein
- Smith-Waterman-Gotoh
- Sorensen-Dice
For more information see https://godoc.org/github.com/adrg/strutil/metrics.