-
Notifications
You must be signed in to change notification settings - Fork 8
/
q-gram.go
47 lines (43 loc) · 933 Bytes
/
q-gram.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
package stringosim
import (
"strings"
)
type QGramSimilarityOptions struct {
CaseInsensitive bool
NGramSizes []int
}
var DefaultQGramSimilarityOptions = QGramSimilarityOptions{
CaseInsensitive: false,
NGramSizes: []int{2},
}
func QGram(s []rune, t []rune, options ...QGramSimilarityOptions) int {
opt := DefaultQGramSimilarityOptions
for _, option := range options {
opt = option
break
}
var sGrams, tGrams map[string]int
if opt.CaseInsensitive {
sGrams = GetNGram(strings.ToLower(string(s)), opt.NGramSizes)
tGrams = GetNGram(strings.ToLower(string(t)), opt.NGramSizes)
} else {
sGrams = GetNGram(string(s), opt.NGramSizes)
tGrams = GetNGram(string(t), opt.NGramSizes)
}
dis := 0
for k, vs := range sGrams {
vt, ok := tGrams[k]
if ok {
dis += AbsInt(vs - vt)
} else {
dis += vs
}
}
for k, vt := range tGrams {
_, ok := sGrams[k]
if !ok {
dis += vt
}
}
return dis
}