go.fuchsia.dev/infra@v0.0.0-20240507153436-9b593402251b/cmd/autocorrelator/compare/compare.go (about) 1 // Copyright 2021 The Fuchsia Authors. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Package compare implements comparators which can be used to score similarity 6 // between various build attributes. 7 package compare 8 9 import ( 10 "github.com/texttheater/golang-levenshtein/levenshtein" 11 ) 12 13 // TextComparator is an interface which can score similarity between two 14 // strings. 15 type TextComparator interface { 16 Compare(s1, s2 string) float64 17 } 18 19 // LevenshteinComparator implements the TextComparator interface, and scores 20 // similarity between two strings as their Levenshtein ratio. 21 type LevenshteinComparator struct { 22 Opts levenshtein.Options 23 } 24 25 func (c LevenshteinComparator) Compare(s1, s2 string) float64 { 26 return levenshtein.RatioForStrings([]rune(s1), []rune(s2), c.Opts) 27 }