-
Notifications
You must be signed in to change notification settings - Fork 0
/
shingling.go
80 lines (67 loc) · 1.57 KB
/
shingling.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
package simhash
import (
"bytes"
"strings"
)
//------------------------------------------------------------------------------
type ShinglingRunes struct {
Runes []rune
K int
}
var _ Features = ShinglingRunes{}
func (p ShinglingRunes) Len() int {
if (p.K <= 0) || (len(p.Runes) < p.K) {
return 0
}
return (len(p.Runes) - p.K + 1)
}
func (p ShinglingRunes) Bytes(i int) []byte {
return encodeRunes(p.Runes[i : i+p.K])
}
func (p ShinglingRunes) Weight(i int) int { return 1 }
//------------------------------------------------------------------------------
type ShinglingStrings struct {
Strings []string
K int
}
var _ Features = ShinglingStrings{}
func (p ShinglingStrings) Len() int {
if (p.K <= 0) || (len(p.Strings) < p.K) {
return 0
}
return (len(p.Strings) - p.K + 1)
}
func (p ShinglingStrings) Bytes(i int) []byte {
s := strings.Join(p.Strings[i:i+p.K], " ")
return []byte(s)
}
func (p ShinglingStrings) Weight(i int) int { return 1 }
//------------------------------------------------------------------------------
func ShingleBytes(a [][]byte, k int) [][]byte {
if k < 1 {
panic("shingle has't elements")
}
if k == 1 {
return a
}
n := len(a) - k + 1
shingles := make([][]byte, n)
for i := range shingles {
shingles[i] = bytes.Join(a[i:i+k], []byte(" "))
}
return shingles
}
func ShingleStrings(a []string, k int) []string {
if k < 1 {
panic("shingle has't elements")
}
if k == 1 {
return a
}
n := len(a) - k + 1
shingles := make([]string, n)
for i := range shingles {
shingles[i] = strings.Join(a[i:i+k], " ")
}
return shingles
}