-
Notifications
You must be signed in to change notification settings - Fork 0
/
hamming.go
38 lines (29 loc) · 878 Bytes
/
hamming.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package similarity_measure
import (
"math"
"github.com/agtabesh/lsh/interfaces"
"github.com/agtabesh/lsh/types"
)
var _ interfaces.SimilarityMeasure = (*HammingSimilarity)(nil)
const percision = 8
type HammingSimilarity struct{}
func NewHammingSimilarity() *HammingSimilarity {
return &HammingSimilarity{}
}
// Calculate computes the hamming similarity between two signature x and y.
// It returns the similarity as an integer value between 0 and 1.
// The function assumes that x and y contain numeric values.
func (sm *HammingSimilarity) Measure(x, y types.Signature) float64 {
if len(x) != len(y) {
return 0
}
total := len(x)
same := 0
for i := 0; i < total; i += 1 {
if x[i] == y[i] {
same += 1
}
}
similarity := float64(same) / float64(total)
return math.Round(math.Pow(10, float64(percision))*similarity) / math.Pow(10, float64(percision))
}