-
Notifications
You must be signed in to change notification settings - Fork 0
/
symmetricalTversky.go
54 lines (43 loc) · 1.24 KB
/
symmetricalTversky.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
package textdistance
import mapset "github.com/deckarep/golang-set"
// NewSymmetricalTversky returns a SymmetricalTversky structure with the StringToSet set to the default WordsToSet
func NewSymmetricalTversky(alpha, beta float64) SymmetricalTversky {
return SymmetricalTversky{
StringToSet: WordsToSet,
Alpha: alpha,
Beta: beta,
}
}
type SymmetricalTversky struct {
StringToSet func(s string) mapset.Set
Alpha float64
Beta float64
}
func (SymmetricalTversky) Maximum(_, _ string) (float64, error) {
return 1, nil
}
func (t SymmetricalTversky) Similarity(s1, s2 string) (float64, error) {
if s1 == s2 {
return 1.0, nil
}
set1 := t.StringToSet(s1)
set2 := t.StringToSet(s2)
set1MinusSet2Cardinality := float64(set1.Difference(set2).Cardinality())
set2MinusSet1Cardinality := float64(set2.Difference(set1).Cardinality())
a := min(set1MinusSet2Cardinality, set2MinusSet1Cardinality)
b := max(set1MinusSet2Cardinality, set2MinusSet1Cardinality)
intersect := float64(set1.Intersect(set2).Cardinality())
return intersect / (intersect + t.Beta*(t.Alpha*a+(1-t.Alpha)*b)), nil
}
func max(a, b float64) float64 {
if a > b {
return a
}
return b
}
func min(a, b float64) float64 {
if a > b {
return b
}
return a
}