-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.go
86 lines (75 loc) · 1.68 KB
/
index.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
86
package gnlp
import (
"math"
)
// JaccardIndex computes Jaccard index (Jaccard similarity coefficient)
// of two sets.
func JaccardIndex[T comparable](a, b []T) float64 {
if len(a) == 0 && len(b) == 0 {
return 1
}
// union
union := map[T]any{}
for _, x := range a {
union[x] = struct{}{}
}
for _, x := range b {
union[x] = struct{}{}
}
// intersection
mapping := map[T]any{}
for _, x := range a {
mapping[x] = struct{}{}
}
intersection := map[T]any{}
for _, x := range b {
if _, ok := mapping[x]; ok {
intersection[x] = struct{}{}
}
}
return float64(len(intersection)) / float64(len(union))
}
// DiceIndex computes Sørensen-Dice index (Sørensen-Dice similarity coefficient)
// of two sets.
func DiceIndex[T comparable](a, b []T) float64 {
if len(a) == 0 && len(b) == 0 {
return 1
}
aMap := map[T]any{}
for _, x := range a {
aMap[x] = struct{}{}
}
bMap := map[T]any{}
for _, x := range b {
bMap[x] = struct{}{}
}
intersection := map[T]any{}
for _, x := range b {
if _, ok := aMap[x]; ok {
intersection[x] = struct{}{}
}
}
return 2 * float64(len(intersection)) / float64(len(aMap)+len(bMap))
}
// SimpsonIndex computes Szymkiewicz–Simpson index (Szymkiewicz–Simpson similarity coefficient)
// of two sets.
func SimpsonIndex[T comparable](a, b []T) float64 {
if len(a) == 0 || len(b) == 0 {
return 1
}
aMap := map[T]any{}
for _, x := range a {
aMap[x] = struct{}{}
}
bMap := map[T]any{}
for _, x := range b {
bMap[x] = struct{}{}
}
intersection := map[T]any{}
for _, x := range b {
if _, ok := aMap[x]; ok {
intersection[x] = struct{}{}
}
}
return float64(len(intersection)) / math.Min(float64(len(aMap)), float64(len(bMap)))
}