### Install strutil Package Source: https://pkg.go.dev/github.com/adrg/strutil Use 'go get' to install the strutil package. This command fetches and installs the package and its dependencies. ```go go get github.com/adrg/strutil ``` -------------------------------- ### Jaro Metric Example Source: https://pkg.go.dev/github.com/adrg/strutil/metrics Shows how to use the Jaro metric for string similarity. Case sensitivity can be adjusted. ```go package main import ( "fmt" "github.com/adrg/strutil" ) func main() { // Example with Jaro metric metric := strutil.NewJaro() s// Calculate similarity ssimilarity := metric.Compare("sort", "shirt") fmt.Printf("(sort, shirt) similarity: %.2f\n", similarity) } ``` -------------------------------- ### Hamming Metric Example Source: https://pkg.go.dev/github.com/adrg/strutil/metrics Demonstrates the usage of the Hamming metric for calculating similarity and distance. Ensure strings are of equal length for accurate Hamming distance. ```go package main import ( "fmt" "github.com/adrg/strutil" ) func main() { // Example with Hamming metric metric := strutil.NewHamming() s// Calculate similarity ssimilarity := metric.Compare("text", "test") fmt.Printf("(text, test) similarity: %.2f\n", similarity) // Calculate distance distance := metric.Distance("text", "test") fmt.Printf("(text, test) distance: %d\n", distance) s// Example with different strings and case sensitivity smetric.CaseSensitive = false ssimilarity = metric.Compare("ONE", "once") fmt.Printf("(ONE, once) similarity: %.2f\n", similarity) distance = metric.Distance("ONE", "once") fmt.Printf("(ONE, once) distance: %d\n", distance) } ``` -------------------------------- ### Jaccard Metric Example Source: https://pkg.go.dev/github.com/adrg/strutil/metrics Illustrates the Jaccard index for string similarity. The NgramSize parameter affects tokenization for comparison. A size of 2 is default if not specified. ```go package main import ( "fmt" "github.com/adrg/strutil" ) func main() { // Example with Jaccard metric metric := strutil.NewJaccard() metric.NgramSize = 2 // Use 2-grams s// Calculate similarity ssimilarity := metric.Compare("night", "alright") fmt.Printf("(night, alright) similarity: %.2f\n", similarity) // Example with different NgramSize metric.NgramSize = 3 similarity = metric.Compare("night", "alright") fmt.Printf("(night, alright) similarity: %.2f\n", similarity) } ``` -------------------------------- ### Get Common Prefix of Two Strings Source: https://pkg.go.dev/github.com/adrg/strutil/internal/stringutil Returns the common prefix of two strings. Returns an empty string if there is no common prefix. ```go func CommonPrefix(first, second string) string ``` -------------------------------- ### Count n-grams in Go Source: https://pkg.go.dev/github.com/adrg/strutil/internal/ngram Use this function to get the count of n-grams of a specified size for a given term. If size is non-positive, it defaults to 1. ```go func Count(runes []rune, size int) int ``` -------------------------------- ### Get Unique Elements from String Slice Source: https://pkg.go.dev/github.com/adrg/strutil/internal/stringutil Returns a new slice containing only the unique elements from the input slice. The order of elements is preserved from the input slice. ```go func UniqueSlice(items []string) []string ``` -------------------------------- ### Get Unique Items from String Slice (Go) Source: https://pkg.go.dev/github.com/adrg/strutil Use UniqueSlice to create a new slice containing only the unique elements from an input string slice. Maintains the original order of appearance. ```go func UniqueSlice(items []string) []string ``` ```text Output: [a b a b b c]: [a b c] ``` -------------------------------- ### Create New Levenshtein Metric Source: https://pkg.go.dev/github.com/adrg/strutil/metrics Initializes a new Levenshtein string metric with default costs for case-sensitive comparison. ```go func NewLevenshtein() *Levenshtein ``` -------------------------------- ### Customize Smith-Waterman-Gotoh Parameters Source: https://pkg.go.dev/github.com/adrg/strutil Customizes the gap penalty and substitution function for the Smith-Waterman-Gotoh algorithm. Case sensitivity can also be adjusted. ```go swg := metrics.NewSmithWatermanGotoh() swg.CaseSensitive = false swg.GapPenalty = -0.1 swg.Substitution = metrics.MatchMismatch { Match: 1, Mismatch: -0.5, } similarity := strutil.Similarity("Times Roman", "times new roman", swg) fmt.Printf("%.2f\n", similarity) // Output: 0.96 ``` -------------------------------- ### Create New Jaro-Winkler Metric Source: https://pkg.go.dev/github.com/adrg/strutil/metrics Initializes a new Jaro-Winkler string metric. The default setting is case-sensitive. ```go func NewJaroWinkler() *JaroWinkler ``` -------------------------------- ### SmithWatermanGotoh Struct Definition Source: https://pkg.go.dev/github.com/adrg/strutil/metrics Defines the configuration options for the Smith-Waterman-Gotoh algorithm, including case sensitivity, gap penalty, and substitution scoring. ```go type SmithWatermanGotoh struct { // CaseSensitive specifies if the string comparison is case sensitive. CaseSensitive bool // GapPenalty defines a score penalty for character insertions or deletions. // For relevant results, the gap penalty should be a non-positive number. GapPenalty float64 // Substitution represents a substitution function which is used to // calculate a score for character substitutions. Substitution Substitution } ``` -------------------------------- ### NewSmithWatermanGotoh Function Source: https://pkg.go.dev/github.com/adrg/strutil/metrics Creates and returns a new Smith-Waterman-Gotoh metric with default settings. Default options include case-sensitive comparison, a gap penalty of -0.5, and a MatchMismatch substitution. ```go func NewSmithWatermanGotoh() *SmithWatermanGotoh ``` -------------------------------- ### SmithWatermanGotoh Compare Method Source: https://pkg.go.dev/github.com/adrg/strutil/metrics Calculates the similarity between two strings using the Smith-Waterman-Gotoh algorithm. The result is a float64 between 0 and 1, where higher values indicate greater similarity. ```go func (m *SmithWatermanGotoh) Compare(a, b string) float64 ``` -------------------------------- ### Map n-grams to frequencies in Go Source: https://pkg.go.dev/github.com/adrg/strutil/internal/ngram Use this function to create a map of all n-grams of a specified size for a term, along with their frequencies. It also returns the total number of n-grams. Defaults to size 1 if size is non-positive. ```go func Map(runes []rune, size int) (map[string]int, int) ``` -------------------------------- ### NewLevenshtein Source: https://pkg.go.dev/github.com/adrg/strutil/metrics Initializes and returns a new Levenshtein string metric with default costs. ```APIDOC ## NewLevenshtein ### Description NewLevenshtein returns a new Levenshtein string metric. ### Method func NewLevenshtein() *Levenshtein ### Parameters None ### Request Example None ### Response #### Success Response (200) - **levenshteinMetric** (*Levenshtein) - A new instance of the Levenshtein metric with default costs (CaseSensitive: true, InsertCost: 1, DeleteCost: 1, ReplaceCost: 1). ``` -------------------------------- ### Find Common Prefix of Two Strings Source: https://pkg.go.dev/github.com/adrg/strutil Returns the common prefix of two strings. If there is no common prefix, an empty string is returned. ```go func CommonPrefix(a, b string) string { return "" } ``` -------------------------------- ### StringMetric Interface and Similarity Function Source: https://pkg.go.dev/github.com/adrg/strutil Defines the StringMetric interface and the Similarity function. The Similarity function calculates the similarity between two strings using a provided metric. ```go type StringMetric interface { Compare(a, b string) float64 } func Similarity(a, b string, metric StringMetric) float64 { } ``` -------------------------------- ### NewSmithWatermanGotoh Source: https://pkg.go.dev/github.com/adrg/strutil/metrics Returns a new Smith-Waterman-Gotoh string metric with default options. ```APIDOC ## NewSmithWatermanGotoh ### Description NewSmithWatermanGotoh returns a new Smith-Waterman-Gotoh string metric. ### Default options ``` CaseSensitive: true GapPenalty: -0.5 Substitution: MatchMismatch{ Match: 1, Mismatch: -2, }, ``` ``` -------------------------------- ### Define String Similarity Metric Interface (Go) Source: https://pkg.go.dev/github.com/adrg/strutil The StringMetric interface defines a Compare method for measuring the similarity between two strings. Various implementations are available in the metrics package. ```go type StringMetric interface { Compare(a, b string) float64 } ``` -------------------------------- ### Jaro.Compare Source: https://pkg.go.dev/github.com/adrg/strutil/metrics Calculates the Jaro similarity between two strings. The result is a float between 0 and 1, where higher values indicate greater similarity. ```APIDOC ## Jaro.Compare ### Description Compare returns the Jaro similarity of a and b. The returned similarity is a number between 0 and 1. Larger similarity numbers indicate closer matches. ### Method func (m *Jaro) Compare(a, b string) float64 ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example None ### Response #### Success Response (200) - **similarity** (float64) - The Jaro similarity score between the two input strings. ``` -------------------------------- ### Jaro Similarity Comparison Source: https://pkg.go.dev/github.com/adrg/strutil/metrics Compares two strings using the Jaro similarity algorithm. The result is a float64 between 0 and 1, where higher values indicate greater similarity. ```go func (m *Jaro) Compare(a, b string) float64 ``` -------------------------------- ### Min Source: https://pkg.go.dev/github.com/adrg/strutil/internal/mathutil Min returns the value of the smallest argument, or 0 if no arguments are provided. ```APIDOC ## Min ### Description Min returns the value of the smallest argument, or 0 if no arguments are provided. ### Signature ```go func Min(args ...int) int ``` ``` -------------------------------- ### CommonPrefix Source: https://pkg.go.dev/github.com/adrg/strutil/internal/stringutil CommonPrefix returns the common prefix of the specified strings. An empty string is returned if the parameters have no prefix in common. ```APIDOC ## CommonPrefix ### Description Returns the common prefix of two strings. Returns an empty string if there is no common prefix. ### Signature ```go func CommonPrefix(first, second string) string ``` ### Parameters - **first** (string) - The first string. - **second** (string) - The second string. ### Returns - (string) - The common prefix of the two strings. ``` -------------------------------- ### NewJaroWinkler Source: https://pkg.go.dev/github.com/adrg/strutil/metrics Initializes and returns a new Jaro-Winkler string metric. ```APIDOC ## NewJaroWinkler ### Description NewJaroWinkler returns a new Jaro-Winkler string metric. ### Method func NewJaroWinkler() *JaroWinkler ### Parameters None ### Request Example None ### Response #### Success Response (200) - **jaroWinklerMetric** (*JaroWinkler) - A new instance of the JaroWinkler metric. ``` -------------------------------- ### Calculate General String Similarity Source: https://pkg.go.dev/github.com/adrg/strutil Calculates the similarity between two strings using a provided string metric. The result is a float between 0 and 1, where higher values indicate greater similarity. ```go func Similarity(a, b string, metric StringMetric) float64 { return 0.0 } ``` -------------------------------- ### Minf Source: https://pkg.go.dev/github.com/adrg/strutil/internal/mathutil Minf returns the value of the smallest argument, or 0 if no arguments are provided. ```APIDOC ## Minf ### Description Minf returns the value of the smallest argument, or 0 if no arguments are provided. ### Signature ```go func Minf(args ...float64) float64 ``` ``` -------------------------------- ### Min Function Signature Source: https://pkg.go.dev/github.com/adrg/strutil/internal/mathutil Returns the value of the smallest integer argument. Returns 0 if no arguments are provided. ```go func Min(args ...int) int ``` -------------------------------- ### Configure Levenshtein Costs and Calculate Similarity Source: https://pkg.go.dev/github.com/adrg/strutil Configures custom costs for edit operations (insertion, replacement, deletion) in the Levenshtein metric and calculates the similarity between two strings. Case sensitivity can also be adjusted. ```go lev := metrics.NewLevenshtein() lev.CaseSensitive = false lev.InsertCost = 1 lev.ReplaceCost = 2 lev.DeleteCost = 1 similarity := strutil.Similarity("make", "Cake", lev) fmt.Printf("%.2f\n", similarity) // Output: 0.50 ``` -------------------------------- ### Jaro Metric Source: https://pkg.go.dev/github.com/adrg/strutil Calculates string similarity using the Jaro metric. ```APIDOC ## Jaro Metric ### Description Calculates similarity using the Jaro metric. ### Usage ```go import "github.com/adrg/strutil" import "github.com/adrg/strutil/metrics" similarity := strutil.Similarity("think", "tank", metrics.NewJaro()) fmt.Printf("%.2f\n", similarity) // Output: 0.78 ``` ``` -------------------------------- ### Minf Function Signature Source: https://pkg.go.dev/github.com/adrg/strutil/internal/mathutil Returns the value of the smallest float64 argument. Returns 0 if no arguments are provided. ```go func Minf(args ...float64) float64 ``` -------------------------------- ### Calculate Smith-Waterman-Gotoh Similarity (Default) Source: https://pkg.go.dev/github.com/adrg/strutil Calculates similarity using the Smith-Waterman-Gotoh algorithm with default options. This is a local sequence alignment algorithm. ```go swg := metrics.NewSmithWatermanGotoh() similarity := strutil.Similarity("times roman", "times new roman", swg) fmt.Printf("%.2f\n", similarity) // Output: 0.82 ``` -------------------------------- ### MatchMismatch Min Score Source: https://pkg.go.dev/github.com/adrg/strutil/metrics Returns the minimum possible score, which is the mismatch score. ```go func (m MatchMismatch) Min() float64 ``` -------------------------------- ### MatchMismatch Compare Function Source: https://pkg.go.dev/github.com/adrg/strutil/metrics Compares characters at specified indices in two rune slices, returning the match score if they are equal or the mismatch score otherwise. ```go func (m MatchMismatch) Compare(a []rune, idxA int, b []rune, idxB int) float64 ``` -------------------------------- ### Create New OverlapCoefficient Metric Source: https://pkg.go.dev/github.com/adrg/strutil/metrics Initializes a new OverlapCoefficient string metric. Defaults to case-sensitive comparison with an n-gram size of 2. ```go func NewOverlapCoefficient() *OverlapCoefficient ``` -------------------------------- ### Customize Overlap Coefficient with N-gram Size and Case Sensitivity Source: https://pkg.go.dev/github.com/adrg/strutil Customizes the Overlap Coefficient metric by setting n-gram size and case sensitivity before calculating string similarity. This allows for more nuanced comparisons. ```go oc := metrics.NewOverlapCoefficient() oc.CaseSensitive = false oc.NgramSize = 3 similarity := strutil.Similarity("Time to make haste", "no time to waste", oc) fmt.Printf("%.2f\n", similarity) // Output: 0.57 ``` -------------------------------- ### SmithWatermanGotoh.Compare Source: https://pkg.go.dev/github.com/adrg/strutil/metrics Compares two strings using the Smith-Waterman-Gotoh algorithm and returns their similarity score. ```APIDOC ## (*SmithWatermanGotoh) Compare ### Description Compare returns the Smith-Waterman-Gotoh similarity of a and b. The returned similarity is a number between 0 and 1. Larger similarity numbers indicate closer matches. ### Method func (m *SmithWatermanGotoh) Compare(a, b string) float64 ``` -------------------------------- ### Substitution Interface Definition Source: https://pkg.go.dev/github.com/adrg/strutil/metrics Defines the interface for substitution functions used in sequence comparison algorithms. It includes methods for comparing characters, and retrieving maximum and minimum substitution scores. ```go type Substitution interface { // Compare returns the substitution score of characters a[idxA] and b[idxB]. Compare(a []rune, idxA int, b []rune, idxB int) float64 // Returns the maximum score of a character substitution operation. Max() float64 // Returns the minimum score of a character substitution operation. Min() float64 } ``` -------------------------------- ### Calculate Levenshtein Similarity (Default) Source: https://pkg.go.dev/github.com/adrg/strutil Calculates the Levenshtein similarity between two strings using default options. The Levenshtein distance considers insertions, deletions, and substitutions. ```go similarity := strutil.Similarity("graph", "giraffe", metrics.NewLevenshtein()) fmt.Printf("%.2f\n", similarity) // Output: 0.43 ``` -------------------------------- ### NewOverlapCoefficient Source: https://pkg.go.dev/github.com/adrg/strutil/metrics Initializes and returns a new OverlapCoefficient string metric with default settings. ```APIDOC ## NewOverlapCoefficient ### Description NewOverlapCoefficient returns a new overlap coefficient string metric. ### Method func NewOverlapCoefficient() *OverlapCoefficient ### Parameters None ### Request Example None ### Response #### Success Response (200) - **overlapCoefficientMetric** (*OverlapCoefficient) - A new instance of the OverlapCoefficient metric with default options (CaseSensitive: true, NgramSize: 2). ``` -------------------------------- ### MatchMismatch.Min Source: https://pkg.go.dev/github.com/adrg/strutil/metrics Returns the mismatch score defined for this MatchMismatch instance. ```APIDOC ## MatchMismatch.Min ### Description Min returns the mismatch value. ### Method func (m MatchMismatch) Min() float64 ### Parameters None ### Request Example None ### Response #### Success Response (200) - **mismatchScore** (float64) - The defined mismatch score. ``` -------------------------------- ### SorensenDice Compare Method Source: https://pkg.go.dev/github.com/adrg/strutil/metrics Computes the Sorensen-Dice similarity coefficient between two strings. It returns a value between 0 and 1, with higher scores indicating more similarity. If n-gram size is not positive, it defaults to 2. ```go func (m *SorensenDice) Compare(a, b string) float64 ``` -------------------------------- ### Levenshtein Similarity Comparison Source: https://pkg.go.dev/github.com/adrg/strutil/metrics Compares two strings using the Levenshtein similarity algorithm, returning a score between 0 and 1. ```go func (m *Levenshtein) Compare(a, b string) float64 ``` -------------------------------- ### Map Function Source: https://pkg.go.dev/github.com/adrg/strutil/internal/ngram Creates a frequency map of n-grams for a given rune slice. ```APIDOC ## func Map ### Description Map returns a map of all n-grams of the specified size for the provided term, along with their frequency. The function also returns the total number of n-grams, which is the sum of all the values in the output map. An n-gram size of 1 is used if the provided size is less than or equal to 0. ### Signature ```go func Map(runes []rune, size int) (map[string]int, int) ``` ### Parameters #### Path Parameters - **runes** ([]rune) - The input text as a slice of runes. - **size** (int) - The size of the n-grams to map. Defaults to 1 if less than or equal to 0. ### Returns - **map[string]int** - A map where keys are n-grams and values are their frequencies. - **int** - The total number of n-grams. ``` -------------------------------- ### Calculate Jaro Similarity Source: https://pkg.go.dev/github.com/adrg/strutil Calculates the Jaro similarity between two strings. This metric is particularly suited for short strings like names. ```go similarity := strutil.Similarity("think", "tank", metrics.NewJaro()) fmt.Printf("%.2f\n", similarity) // Output: 0.78 ``` -------------------------------- ### MatchMismatch Max Score Source: https://pkg.go.dev/github.com/adrg/strutil/metrics Returns the maximum possible score, which is the match score. ```go func (m MatchMismatch) Max() float64 ``` -------------------------------- ### Calculate Overlap Coefficient Similarity Source: https://pkg.go.dev/github.com/adrg/strutil Calculates the similarity between two strings using the Overlap Coefficient metric with default options. The result is a float between 0 and 1. ```go oc := metrics.NewOverlapCoefficient() similarity := strutil.Similarity("time to make haste", "no time to waste", oc) fmt.Printf("%.2f\n", similarity) // Output: 0.67 ``` -------------------------------- ### Substitution Interface Source: https://pkg.go.dev/github.com/adrg/strutil/metrics Represents a substitution function used to calculate scores for character substitutions in string metrics. ```APIDOC ## Substitution Interface ### Description Substitution represents a substitution function which is used to calculate a score for character substitutions. ### Methods - **Compare**(a []rune, idxA int, b []rune, idxB int) float64 - Returns the substitution score of characters a[idxA] and b[idxB]. - **Max**() float64 - Returns the maximum score of a character substitution operation. - **Min**() float64 - Returns the minimum score of a character substitution operation. ``` -------------------------------- ### Similarity Source: https://pkg.go.dev/github.com/adrg/strutil Calculates the similarity between two strings using a specified string metric. The result is a float64 between 0 and 1, where higher values indicate a closer match. ```APIDOC ## Similarity ### Description 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. ### Function Signature ```go func Similarity(a, b string, metric StringMetric) float64 ``` ### Example ```go // Assuming 'oc' is an initialized StringMetric implementation similarity := strutil.Similarity("riddle", "needle", oc) // similarity will be approximately 0.56 ``` ``` -------------------------------- ### NgramMap Source: https://pkg.go.dev/github.com/adrg/strutil Generates a map of n-grams and their frequencies for a given term and size. It also returns the total number of n-grams. If the size is non-positive, a default size of 1 is used. ```APIDOC ## NgramMap ### Description NgramMap returns a map of all n-grams of the specified size for the provided term, along with their frequency. The function also returns the total number of n-grams, which is the sum of all the values in the output map. An n-gram size of 1 is used if the provided size is less than or equal to 0. ### Function Signature ```go func NgramMap(term string, size int) (map[string]int, int) ``` ### Example ```go ngramMap, totalNgrams := strutil.NgramMap("abbcabb", 2) // ngramMap will be map[ab:2 bb:2 bc:1 ca:1], totalNgrams will be 6 ``` ``` -------------------------------- ### Calculate Hamming Similarity Source: https://pkg.go.dev/github.com/adrg/strutil Calculates the similarity between two strings using the Hamming metric. The output is a float64 representing the similarity score. ```go similarity := strutil.Similarity("text", "test", metrics.NewHamming()) fmt.Printf("%.2f\n", similarity) // Output: 0.75 ``` -------------------------------- ### Jaro-Winkler Similarity Comparison Source: https://pkg.go.dev/github.com/adrg/strutil/metrics Calculates the Jaro-Winkler similarity between two strings. The output is a float64 between 0 and 1. ```go func (m *JaroWinkler) Compare(a, b string) float64 ``` -------------------------------- ### Create a Unique Slice of Strings Source: https://pkg.go.dev/github.com/adrg/strutil Removes duplicate strings from a slice, returning a new slice with only unique elements. ```go func UniqueSlice(items []string) []string { return nil } ``` -------------------------------- ### CommonPrefix Source: https://pkg.go.dev/github.com/adrg/strutil Finds and returns the longest common prefix between two strings. If no common prefix exists, an empty string is returned. ```APIDOC ## CommonPrefix ### Description CommonPrefix returns the common prefix of the specified strings. An empty string is returned if the parameters have no prefix in common. ### Function Signature ```go func CommonPrefix(a, b string) string ``` ### Example ```go prefix := strutil.CommonPrefix("answer", "anvil") // prefix will be "an" ``` ``` -------------------------------- ### Calculate Jaccard Similarity (Default) Source: https://pkg.go.dev/github.com/adrg/strutil Calculates similarity using the Jaccard index with default options. This metric measures the similarity between finite sample sets. ```go j := metrics.NewJaccard() similarity := strutil.Similarity("time to make haste", "no time to waste", j) fmt.Printf("%.2f\n", similarity) // Output: 0.45 ``` -------------------------------- ### Smith-Waterman-Gotoh Metric Source: https://pkg.go.dev/github.com/adrg/strutil Implements the Smith-Waterman-Gotoh algorithm for local sequence alignment, allowing customization of gap penalties and substitution functions. ```APIDOC ## Smith-Waterman-Gotoh Metric ### Description Calculates similarity using the Smith-Waterman-Gotoh algorithm with configurable options. ### Usage #### Similarity (Default Options) ```go import "github.com/adrg/strutil" import "github.com/adrg/strutil/metrics" swg := metrics.NewSmithWatermanGotoh() similarity := strutil.Similarity("times roman", "times new roman", swg) fmt.Printf("%.2f\n", similarity) // Output: 0.82 ``` #### Similarity (Custom Options) ```go import "github.com/adrg/strutil" import "github.com/adrg/strutil/metrics" swg := metrics.NewSmithWatermanGotoh() swg.CaseSensitive = false swg.GapPenalty = -0.1 swg.Substitution = metrics.MatchMismatch{ Match: 1, Mismatch: -0.5, } similarity := strutil.Similarity("Times Roman", "times new roman", swg) fmt.Printf("%.2f\n", similarity) // Output: 0.96 ``` ``` -------------------------------- ### Count Function Source: https://pkg.go.dev/github.com/adrg/strutil/internal/ngram Counts the n-grams of a specified size within a given rune slice. If the size is non-positive, it defaults to 1. ```APIDOC ## func Count ### Description Count returns the n-gram count of the specified size for the provided term. An n-gram size of 1 is used if the provided size is less than or equal to 0. ### Signature ```go func Count(runes []rune, size int) int ``` ### Parameters #### Path Parameters - **runes** ([]rune) - The input text as a slice of runes. - **size** (int) - The size of the n-grams to count. Defaults to 1 if less than or equal to 0. ### Returns - **int** - The total count of n-grams of the specified size. ``` -------------------------------- ### StringMetric Interface Source: https://pkg.go.dev/github.com/adrg/strutil StringMetric represents an interface for defining metrics used to measure the similarity between two strings. ```APIDOC ## type StringMetric interface ### Description StringMetric represents a metric for measuring the similarity between strings. The metrics package implements the following string metrics: Hamming, Jaro, Jaro-Winkler, Levenshtein, Smith-Waterman-Gotoh, Sorensen-Dice, Jaccard, Overlap coefficient. ### Interface Definition ```go interface { Compare(a, b string) float64 } ``` ### See Also For more information see https://pkg.go.dev/github.com/adrg/strutil/metrics. ``` -------------------------------- ### Levenshtein Metric Definition Source: https://pkg.go.dev/github.com/adrg/strutil/metrics Defines the Levenshtein metric, including options for case sensitivity and costs for insertion, deletion, and substitution. ```go type Levenshtein struct { // CaseSensitive specifies if the string comparison is case sensitive. CaseSensitive bool // InsertCost represents the Levenshtein cost of a character insertion. InsertCost int // InsertCost represents the Levenshtein cost of a character deletion. DeleteCost int // InsertCost represents the Levenshtein cost of a character substitution. ReplaceCost int } ``` -------------------------------- ### Generate N-gram Map Source: https://pkg.go.dev/github.com/adrg/strutil Generates a map of all n-grams for a term and their frequencies, along with the total n-gram count. Uses n-gram size 1 if size is non-positive. ```go func NgramMap(term string, size int) (map[string]int, int) { return nil, 0 } ``` -------------------------------- ### JaroWinkler.Compare Source: https://pkg.go.dev/github.com/adrg/strutil/metrics Calculates the Jaro-Winkler similarity between two strings. The result is a float between 0 and 1, where higher values indicate greater similarity. ```APIDOC ## JaroWinkler.Compare ### Description Compare returns the Jaro-Winkler similarity of a and b. The returned similarity is a number between 0 and 1. Larger similarity numbers indicate closer matches. ### Method func (m *JaroWinkler) Compare(a, b string) float64 ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example None ### Response #### Success Response (200) - **similarity** (float64) - The Jaro-Winkler similarity score between the two input strings. ``` -------------------------------- ### Jaro-Winkler Metric Definition Source: https://pkg.go.dev/github.com/adrg/strutil/metrics Defines the Jaro-Winkler metric for string comparison. It includes an option to specify case sensitivity. ```go type JaroWinkler struct { // CaseSensitive specifies if the string comparison is case sensitive. CaseSensitive bool } ``` -------------------------------- ### Extract N-grams from a String Source: https://pkg.go.dev/github.com/adrg/strutil Returns a slice of all n-grams of a specified size from a term, in the order they appear. Uses n-gram size 1 if size is non-positive. ```go func Ngrams(term string, size int) []string { return nil } ``` -------------------------------- ### Calculate Jaro-Winkler Similarity Source: https://pkg.go.dev/github.com/adrg/strutil Calculates the Jaro-Winkler similarity, an extension of the Jaro metric that gives more favorable ratings to strings that match from the beginning. ```go similarity := strutil.Similarity("think", "tank", metrics.NewJaroWinkler()) fmt.Printf("%.2f\n", similarity) // Output: 0.80 ``` -------------------------------- ### MatchMismatch.Compare Source: https://pkg.go.dev/github.com/adrg/strutil/metrics Compares two characters at specified indices and returns a score based on whether they match or mismatch. ```APIDOC ## MatchMismatch.Compare ### Description Compare returns the match value if a[idxA] is equal to b[idxB] or the mismatch value otherwise. ### Method func (m MatchMismatch) Compare(a []rune, idxA int, b []rune, idxB int) float64 ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example None ### Response #### Success Response (200) - **score** (float64) - The match score if characters are equal, or the mismatch score otherwise. ``` -------------------------------- ### Hamming Metric Source: https://pkg.go.dev/github.com/adrg/strutil Provides the Hamming distance and similarity calculation for strings of equal length. ```APIDOC ## Hamming Metric ### Description Calculates similarity and distance using the Hamming metric. ### Usage #### Similarity ```go import "github.com/adrg/strutil" import "github.com/adrg/strutil/metrics" similarity := strutil.Similarity("text", "test", metrics.NewHamming()) fmt.Printf("%.2f\n", similarity) // Output: 0.75 ``` #### Distance ```go import "github.com/adrg/strutil/metrics" ham := metrics.NewHamming() fmt.Printf("%d\n", ham.Distance("one", "once")) // Output: 2 ``` ``` -------------------------------- ### Jaro Metric Source: https://pkg.go.dev/github.com/adrg/strutil/metrics The Jaro metric is a measure of similarity between two strings. It is a weighted comparison that considers matching characters and transpositions. ```APIDOC ## Jaro Metric ### Description Provides methods for calculating the Jaro similarity between strings. ### Type `Jaro` ### Fields - `CaseSensitive` (bool): Specifies if the string comparison is case sensitive. ### Functions #### `NewJaro()` - **Description**: Returns a new Jaro string metric with default options (CaseSensitive: true). - **Returns**: `*Jaro` #### `(*Jaro) Compare(a, b string)` - **Description**: Calculates the Jaro similarity between two strings `a` and `b`. Returns a float64 representing the similarity score. - **Parameters**: - `a` (string) - The first string. - `b` (string) - The second string. - **Returns**: `float64` - The similarity score. ``` -------------------------------- ### Levenshtein.Compare Source: https://pkg.go.dev/github.com/adrg/strutil/metrics Calculates the Levenshtein similarity between two strings. The result is a float between 0 and 1, where higher values indicate greater similarity. ```APIDOC ## Levenshtein.Compare ### Description Compare returns the Levenshtein similarity of a and b. The returned similarity is a number between 0 and 1. Larger similarity numbers indicate closer matches. ### Method func (m *Levenshtein) Compare(a, b string) float64 ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example None ### Response #### Success Response (200) - **similarity** (float64) - The Levenshtein similarity score between the two input strings. ``` -------------------------------- ### MatchMismatch.Max Source: https://pkg.go.dev/github.com/adrg/strutil/metrics Returns the match score defined for this MatchMismatch instance. ```APIDOC ## MatchMismatch.Max ### Description Max returns the match value. ### Method func (m MatchMismatch) Max() float64 ### Parameters None ### Request Example None ### Response #### Success Response (200) - **matchScore** (float64) - The defined match score. ``` -------------------------------- ### NewSorensenDice Function Source: https://pkg.go.dev/github.com/adrg/strutil/metrics Initializes and returns a new Sorensen-Dice string metric. By default, it performs case-sensitive comparisons and uses an n-gram size of 2. ```go func NewSorensenDice() *SorensenDice ``` -------------------------------- ### Maxf Function Signature Source: https://pkg.go.dev/github.com/adrg/strutil/internal/mathutil Returns the value of the largest float64 argument. Returns 0 if no arguments are provided. ```go func Maxf(args ...float64) float64 ``` -------------------------------- ### Calculate N-gram Count Source: https://pkg.go.dev/github.com/adrg/strutil Calculates the n-gram count for a given term and size. If the provided size is less than or equal to 0, an n-gram size of 1 is used. ```go func NgramCount(term string, size int) int { return 0 } ``` -------------------------------- ### Max Function Signature Source: https://pkg.go.dev/github.com/adrg/strutil/internal/mathutil Returns the value of the largest integer argument. Returns 0 if no arguments are provided. ```go func Max(args ...int) int ``` -------------------------------- ### Find n-gram intersection in Go Source: https://pkg.go.dev/github.com/adrg/strutil/internal/ngram This function returns a map of common n-grams between two terms, their frequencies, the count of common n-grams, and the total n-grams in each term. Defaults to size 1 if size is non-positive. ```go func Intersection(a, b []rune, size int) (map[string]int, int, int, int) ``` -------------------------------- ### Max Source: https://pkg.go.dev/github.com/adrg/strutil/internal/mathutil Max returns the value of the largest argument, or 0 if no arguments are provided. ```APIDOC ## Max ### Description Max returns the value of the largest argument, or 0 if no arguments are provided. ### Signature ```go func Max(args ...int) int ``` ``` -------------------------------- ### SmithWatermanGotoh Source: https://pkg.go.dev/github.com/adrg/strutil/metrics Represents the Smith-Waterman-Gotoh metric for measuring the similarity between sequences. It considers case sensitivity, gap penalties, and substitutions. ```APIDOC ## SmithWatermanGotoh ### Description Represents the Smith-Waterman-Gotoh metric for measuring the similarity between sequences. For more information see https://en.wikipedia.org/wiki/Smith-Waterman_algorithm. ### Fields - **CaseSensitive** (bool) - Specifies if the string comparison is case sensitive. - **GapPenalty** (float64) - Defines a score penalty for character insertions or deletions. For relevant results, the gap penalty should be a non-positive number. - **Substitution** (Substitution) - Represents a substitution function which is used to calculate a score for character substitutions. ### Example ``` (a pink kitten, a kitten) similarity: 0.88 (a pink kitten, A KITTEN) similarity: 0.94 ``` ``` -------------------------------- ### Customize Sorensen-Dice N-gram Size Source: https://pkg.go.dev/github.com/adrg/strutil Customizes the n-gram size for the Sorensen-Dice similarity calculation. Case sensitivity can also be adjusted. ```go sd := metrics.NewSorensenDice() sd.CaseSensitive = false sd.NgramSize = 3 similarity := strutil.Similarity("Time to make haste", "no time to waste", sd) fmt.Printf("%.2f\n", similarity) // Output: 0.53 ``` -------------------------------- ### Levenshtein Metric Source: https://pkg.go.dev/github.com/adrg/strutil Implements the Levenshtein distance and similarity calculation, allowing customization of edit operation costs. ```APIDOC ## Levenshtein Metric ### Description Calculates similarity and distance using the Levenshtein metric with configurable costs. ### Usage #### Similarity (Default Costs) ```go import "github.com/adrg/strutil" import "github.com/adrg/strutil/metrics" similarity := strutil.Similarity("graph", "giraffe", metrics.NewLevenshtein()) fmt.Printf("%.2f\n", similarity) // Output: 0.43 ``` #### Similarity (Custom Costs) ```go import "github.com/adrg/strutil" import "github.com/adrg/strutil/metrics" lev := metrics.NewLevenshtein() lev.CaseSensitive = false lev.InsertCost = 1 lev.ReplaceCost = 2 lev.DeleteCost = 1 similarity := strutil.Similarity("make", "Cake", lev) fmt.Printf("%.2f\n", similarity) // Output: 0.50 ``` #### Distance ```go import "github.com/adrg/strutil/metrics" lev := metrics.NewLevenshtein() fmt.Printf("%d\n", lev.Distance("graph", "giraffe")) // Output: 4 ``` ``` -------------------------------- ### Similarity Function Source: https://pkg.go.dev/github.com/adrg/strutil Calculates the similarity between two strings using a specified string metric. The StringMetric interface is implemented by all provided metrics. ```APIDOC ## Similarity Function ### Description Calculates the similarity between two strings using the provided string metric. ### Signature ```go func Similarity(a, b string, metric StringMetric) float64 ``` ### Parameters - **a** (string) - The first string to compare. - **b** (string) - The second string to compare. - **metric** (StringMetric) - An implementation of the StringMetric interface. ### Return Value - **float64** - The similarity score between the two strings. ``` -------------------------------- ### Slice Function Source: https://pkg.go.dev/github.com/adrg/strutil/internal/ngram Extracts all n-grams of a specified size from a rune slice in their order of occurrence. ```APIDOC ## func Slice ### Description Slice returns all the n-grams of the specified size for the provided term. The n-grams in the output slice are in the order in which they occur in the input term. An n-gram size of 1 is used if the provided size is less than or equal to 0. ### Signature ```go func Slice(runes []rune, size int) []string ``` ### Parameters #### Path Parameters - **runes** ([]rune) - The input text as a slice of runes. - **size** (int) - The size of the n-grams to extract. Defaults to 1 if less than or equal to 0. ### Returns - **[]string** - A slice of strings, where each string is an n-gram in its order of occurrence. ``` -------------------------------- ### Jaro-Winkler Metric Source: https://pkg.go.dev/github.com/adrg/strutil Calculates string similarity using the Jaro-Winkler metric, an extension of the Jaro metric. ```APIDOC ## Jaro-Winkler Metric ### Description Calculates similarity using the Jaro-Winkler metric. ### Usage ```go import "github.com/adrg/strutil" import "github.com/adrg/strutil/metrics" similarity := strutil.Similarity("think", "tank", metrics.NewJaroWinkler()) fmt.Printf("%.2f\n", similarity) // Output: 0.80 ``` ``` -------------------------------- ### Customize Jaccard N-gram Size Source: https://pkg.go.dev/github.com/adrg/strutil Customizes the n-gram size for the Jaccard index calculation. Case sensitivity can also be adjusted. ```go j := metrics.NewJaccard() j.CaseSensitive = false j.NgramSize = 3 similarity := strutil.Similarity("Time to make haste", "no time to waste", j) fmt.Printf("%.2f\n", similarity) // Output: 0.36 ``` -------------------------------- ### Slice n-grams in Go Source: https://pkg.go.dev/github.com/adrg/strutil/internal/ngram This function returns a slice of all n-grams of a specified size for a term, maintaining their order of occurrence. Defaults to size 1 if size is non-positive. ```go func Slice(runes []rune, size int) []string ``` -------------------------------- ### SorensenDice.Compare Source: https://pkg.go.dev/github.com/adrg/strutil/metrics Calculates the Sorensen-Dice similarity coefficient between two strings. ```APIDOC ## (*SorensenDice) Compare ### Description Compare returns the Sorensen-Dice similarity coefficient of a and b. The returned similarity is a number between 0 and 1. Larger similarity numbers indicate closer matches. An n-gram size of 2 is used if the provided size is less than or equal to 0. ### Method func (m *SorensenDice) Compare(a, b string) float64 ``` -------------------------------- ### Find N-gram Intersection Source: https://pkg.go.dev/github.com/adrg/strutil Returns a map of n-grams found in both terms, their frequency, the count of common n-grams, and the total n-grams in each term. Uses n-gram size 1 if size is non-positive. ```go func NgramIntersection(a, b string, size int) (map[string]int, int, int, int) { return nil, 0, 0, 0 } ``` -------------------------------- ### Maxf Source: https://pkg.go.dev/github.com/adrg/strutil/internal/mathutil Maxf returns the value of the largest argument, or 0 if no arguments are provided. ```APIDOC ## Maxf ### Description Maxf returns the value of the largest argument, or 0 if no arguments are provided. ### Signature ```go func Maxf(args ...float64) float64 ``` ``` -------------------------------- ### OverlapCoefficient Similarity Calculation Source: https://pkg.go.dev/github.com/adrg/strutil/metrics Calculates the Overlap Coefficient similarity between two strings. A value between 0 and 1 is returned, with higher values indicating closer matches. If n-gram size is not positive, it defaults to 2. ```go func (m *OverlapCoefficient) Compare(a, b string) float64 ``` -------------------------------- ### Levenshtein Distance Calculation Source: https://pkg.go.dev/github.com/adrg/strutil/metrics Calculates the Levenshtein distance between two strings. Lower values indicate closer matches, with 0 meaning identical strings. ```go func (m *Levenshtein) Distance(a, b string) int ``` -------------------------------- ### Jaccard Metric Source: https://pkg.go.dev/github.com/adrg/strutil Calculates string similarity using the Jaccard index, with options to customize n-gram size and case sensitivity. ```APIDOC ## Jaccard Metric ### Description Calculates similarity using the Jaccard index with configurable n-gram size. ### Usage #### Similarity (Default N-gram Size) ```go import "github.com/adrg/strutil" import "github.com/adrg/strutil/metrics" j := metrics.NewJaccard() similarity := strutil.Similarity("time to make haste", "no time to waste", j) fmt.Printf("%.2f\n", similarity) // Output: 0.45 ``` #### Similarity (Custom N-gram Size) ```go import "github.com/adrg/strutil" import "github.com/adrg/strutil/metrics" j := metrics.NewJaccard() j.CaseSensitive = false j.NgramSize = 3 similarity := strutil.Similarity("Time to make haste", "no time to waste", j) fmt.Printf("%.2f\n", similarity) // Output: 0.36 ``` ### Relationship with Sorensen-Dice - Jaccard Index from Sorensen-Dice: `J = SD / (2 - SD)` - Sorensen-Dice from Jaccard Index: `SD = 2 * J / (1 + J)` ``` -------------------------------- ### Calculate Sorensen-Dice Similarity (Default) Source: https://pkg.go.dev/github.com/adrg/strutil Calculates similarity using the Sorensen-Dice coefficient with default options. This metric is often used for comparing sets or strings. ```go sd := metrics.NewSorensenDice() similarity := strutil.Similarity("time to make haste", "no time to waste", sd) fmt.Printf("%.2f\n", similarity) // Output: 0.62 ``` -------------------------------- ### Ngrams Source: https://pkg.go.dev/github.com/adrg/strutil Extracts all n-grams of a specified size from a term and returns them as a slice of strings. The order of n-grams in the slice corresponds to their occurrence in the term. A size of 1 is used if the provided size is non-positive. ```APIDOC ## Ngrams ### Description Ngrams returns all the n-grams of the specified size for the provided term. The n-grams in the output slice are in the order in which they occur in the input term. An n-gram size of 1 is used if the provided size is less than or equal to 0. ### Function Signature ```go func Ngrams(term string, size int) []string ``` ### Example ```go grams := strutil.Ngrams("abbcd", 2) // grams will be ["ab", "bb", "bc", "cd"] ``` ``` -------------------------------- ### NewSorensenDice Source: https://pkg.go.dev/github.com/adrg/strutil/metrics Returns a new Sorensen-Dice string metric with default options. ```APIDOC ## NewSorensenDice ### Description NewSorensenDice returns a new Sorensen-Dice string metric. ### Default options ``` CaseSensitive: true NGramSize: 2 ``` ``` -------------------------------- ### Calculate Levenshtein Distance Source: https://pkg.go.dev/github.com/adrg/strutil Calculates the Levenshtein distance between two strings. This metric is useful for measuring the difference between sequences. ```go lev := metrics.NewLevenshtein() fmt.Printf("%d\n", lev.Distance("graph", "giraffe")) // Output: 4 ``` -------------------------------- ### Calculate Hamming Distance Source: https://pkg.go.dev/github.com/adrg/strutil Calculates the Hamming distance between two strings. The distance is the number of positions at which the corresponding symbols are different. ```go ham := metrics.NewHamming() fmt.Printf("%d\n", ham.Distance("one", "once")) // Output: 2 ``` -------------------------------- ### SorensenDice Struct Definition Source: https://pkg.go.dev/github.com/adrg/strutil/metrics Defines the configuration for the Sorensen-Dice similarity metric, including case sensitivity and the size of n-grams to be used for comparison. ```go type SorensenDice struct { // CaseSensitive specifies if the string comparison is case sensitive. CaseSensitive bool // NgramSize represents the size (in characters) of the tokens generated // when comparing the input sequences. NgramSize int } ```