Skip to content

Instantly share code, notes, and snippets.

@alediaferia
Last active January 3, 2018 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alediaferia/df59e27501d9c55908752e7d64c5dadf to your computer and use it in GitHub Desktop.
Save alediaferia/df59e27501d9c55908752e7d64c5dadf to your computer and use it in GitHub Desktop.
A simple function to compute the similarity between 2 words
import "math"
// similarity computes the similarity between
// 2 words. w1len and w2len are the lengths of the 2
// words for which to compute the similarity.
// ld is the levenshtein distance between the 2 words.
// It returns a value between 0 and 1 that identifies the similarity
// between the 2 words. 2 equal words will have a similarity of 1.
func similarity(w1Len, w2Len, ld int) float64 {
maxLen := math.Max(float64(w1Len), float64(w2Len))
return 1.0 - float64(ld)/float64(maxLen)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment