-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathshingle.go
34 lines (31 loc) · 864 Bytes
/
shingle.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
package edlib
// Shingle Find the k-gram of a string for a given k
// Takes a string and an integer as parameters and return a map.
// Returns an empty map if the string is empty or if k is 0
func Shingle(s string, k int) map[string]int {
m := make(map[string]int)
if s != "" && k != 0 {
runeS := []rune(s)
for i := 0; i < len(runeS)-k+1; i++ {
m[string(runeS[i:i+k])]++
}
}
return m
}
// ShingleSlice Find the k-gram of a string for a given k
// Takes a string and an integer as parameters and return a slice.
// Returns an empty slice if the string is empty or if k is 0
func ShingleSlice(s string, k int) []string {
var out []string
m := make(map[string]int)
if s != "" && k != 0 {
runeS := []rune(s)
for i := 0; i < len(runeS)-k+1; i++ {
m[string(runeS[i:i+k])]++
}
for k := range m {
out = append(out, k)
}
}
return out
}