-
Notifications
You must be signed in to change notification settings - Fork 0
/
simhash.go
85 lines (76 loc) · 1.25 KB
/
simhash.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package simhash
import (
"hash/fnv"
)
// SimHash:
type Vector [64]int
type Features interface {
Len() int // number of features
Bytes(i int) []byte // bytes of i-th feature
Weight(i int) int // weight of i-th feature
}
func Simhash(fs Features) uint64 {
v := Vectorize(fs)
return Fingerprint(v)
}
func Vectorize(fs Features) Vector {
var v Vector
h := fnv.New64()
n := fs.Len()
for i := 0; i < n; i++ {
h.Reset()
h.Write(fs.Bytes(i))
var (
sum = h.Sum64()
weight = fs.Weight(i)
)
for j := range v {
bit := ((sum >> j) & 1)
if bit == 1 {
v[j] += weight
} else {
v[j] -= weight
}
}
}
return v
}
func VectorizeBytes(bs [][]byte) Vector {
var v Vector
h := fnv.New64()
for _, b := range bs {
h.Reset()
h.Write(b)
var (
sum = h.Sum64()
weight = 1
)
for j := range v {
bit := ((sum >> j) & 1)
if bit == 1 {
v[j] += weight
} else {
v[j] -= weight
}
}
}
return v
}
func Fingerprint(v Vector) uint64 {
var fp uint64
for i := range v {
if v[i] >= 0 {
fp |= (1 << i)
}
}
return fp
}
// Compare calculates the Hamming distance between two 64-bit integers.
func Compare(a, b uint64) int {
var n int
v := a ^ b
for n = 0; v != 0; n++ {
v &= v - 1
}
return n
}