-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminhash_test.go
132 lines (103 loc) · 3.54 KB
/
minhash_test.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package lsh
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_ToSetsMatrix(t *testing.T) {
shingles := make([][]string, 3)
shingles[0] = aShingles
shingles[1] = bShingles
shingles[2] = cShingles
setsMatrix := ToSetsMatrix(shingles)
intersection := make(map[string]bool)
for _, set := range shingles {
for _, sh := range set {
intersection[sh] = true
}
}
// Assert that number of sets in setsMatrix is the amount of provided sets of shingles
assert.Equal(t, setsMatrix.setsNum, len(shingles))
// Assert that number of rows in setsMatrix is the length of the union of provided unique sets of shingles
assert.Equal(t, setsMatrix.ShinglesNum(), len(intersection))
// Assert that 1st and 2nd sets have the same entry for shingle - "is good for"
assert.True(t, setsMatrix.m["is good for"][0])
assert.True(t, setsMatrix.m["is good for"][1])
// Assert that 1st and 3rd sets have the same entry for shingle - "the Sudzo Corporation"
assert.True(t, setsMatrix.m["the Sudzo Corporation"][0])
assert.True(t, setsMatrix.m["the Sudzo Corporation"][2])
}
func Test_ToSetsMatrix2(t *testing.T) {
setsMatrix := ToSetsMatrix(simpleShingles)
// Assert that number of sets in setsMatrix is the amount of provided sets of shingles
assert.Equal(t, setsMatrix.setsNum, 4)
// Assert that number of rows in setsMatrix is the length of the intersection of provided sets of shingles
assert.Equal(t, setsMatrix.ShinglesNum(), 5)
// "a" is in set1 and set4
assert.True(t, setsMatrix.m["a"][0])
assert.True(t, setsMatrix.m["a"][3])
// "b" is only in set3
assert.True(t, setsMatrix.m["b"][2])
// "c" is in set2 and set4
assert.True(t, setsMatrix.m["c"][1])
assert.True(t, setsMatrix.m["c"][3])
// "d" is in set1, set3 and set4
assert.True(t, setsMatrix.m["d"][0])
assert.True(t, setsMatrix.m["d"][2])
assert.True(t, setsMatrix.m["d"][3])
// "e" is only in set3
assert.True(t, setsMatrix.m["e"][2])
}
func Test_ToSetsComputeMatrix(t *testing.T) {
// Input matrix:
// row | s1 | s2 | s3 | s4
// 0 | 1 | 0 | 0 | 1
// 1 | 0 | 0 | 1 | 0
// 2 | 0 | 1 | 0 | 1
// 3 | 1 | 0 | 1 | 1
// 4 | 0 | 0 | 1 | 0
expected := &SetsComputeMatrix{
m: [][]bool{
0: {true, false, false, true},
1: {false, false, true, false},
2: {false, true, false, true},
3: {true, false, true, true},
4: {false, false, true, false},
},
rowsNum: 5,
setsNum: 4,
}
actual := ToSetsComputeMatrix(ToSetsMatrix(simpleShingles))
assert.Equal(t, expected.rowsNum, actual.rowsNum)
assert.Equal(t, expected.setsNum, actual.setsNum)
assert.Equal(t, len(expected.m), len(actual.m))
for i, row := range expected.m {
assert.Equal(t, len(row), len(actual.m[i]))
for k, value := range row {
assert.Equal(t, value, actual.m[i][k])
}
}
}
func Test_MinHash_EnforcesOrder(t *testing.T) {
// Input matrix:
// row | s1 | s2 | s3 | s4
// 0 | 1 | 0 | 0 | 1
// 1 | 0 | 0 | 1 | 0
// 2 | 0 | 1 | 0 | 1
// 3 | 1 | 0 | 1 | 1
// 4 | 0 | 0 | 1 | 0
minhash := MinhashWithHashers(simpleShingles, []*Hasher{NewPatternX(1, 1), NewPatternX(3, 1)})
// Output matrix:
// h | s1 | s2 | s3 | s4
// h1 | 1 | 3 | 0 | 1
// h2 | 0 | 2 | 0 | 0
// h1 hasing function assertions
assert.Equal(t, 1.0, minhash[0][0])
assert.Equal(t, 3.0, minhash[0][1])
assert.Equal(t, 0.0, minhash[0][2])
assert.Equal(t, 1.0, minhash[0][3])
// h2 hasing function assertions
assert.Equal(t, 0.0, minhash[1][0])
assert.Equal(t, 2.0, minhash[1][1])
assert.Equal(t, 0.0, minhash[1][2])
assert.Equal(t, 0.0, minhash[1][3])
}