-
Notifications
You must be signed in to change notification settings - Fork 35
/
word2vec_test.go
153 lines (130 loc) · 2.72 KB
/
word2vec_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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package word2vec
import (
"bytes"
"encoding/binary"
"fmt"
"reflect"
"testing"
)
func TestVectorAdd(t *testing.T) {
tests := []struct {
x, y, ans []float32
}{
{
x: []float32{0},
y: []float32{0},
ans: []float32{0},
},
}
for _, tt := range tests {
v := Vector(tt.x)
u := Vector(tt.y)
v.Add(1.0, u)
vans := Vector(tt.ans)
if !reflect.DeepEqual(v, vans) {
t.Errorf("x.Add(y) = %v, expected %v", v, vans)
}
}
}
func TestVectorDot(t *testing.T) {
tests := []struct {
x, y []float32
ans float32
}{
{
x: []float32{0},
y: []float32{0},
ans: 0,
},
{
x: []float32{1},
y: []float32{0},
ans: 0,
},
{
x: []float32{0},
y: []float32{1},
ans: 0,
},
{
x: []float32{1},
y: []float32{1},
ans: 1,
},
}
for _, tt := range tests {
v := Vector(tt.x)
u := Vector(tt.y)
ans := v.Dot(u)
if ans != tt.ans {
t.Errorf("x.Dot(y) = %v, expected %v", ans, tt.ans)
}
}
}
func TestFromReader(t *testing.T) {
vecs := map[string]Vector{
"hello": Vector{0, 1},
"world": Vector{1, 0},
}
dim := 2
buf := &bytes.Buffer{}
fmt.Fprintln(buf, len(vecs), dim)
for k, v := range vecs {
fmt.Fprintf(buf, "%s ", k)
err := binary.Write(buf, binary.LittleEndian, v)
if err != nil {
t.Errorf("unexpected error writing vector")
}
fmt.Fprintf(buf, "\n")
}
m, err := FromReader(bytes.NewReader(buf.Bytes()))
if err != nil {
t.Errorf("unexpected error from FromReader: %v", err)
}
if m.Size() != len(vecs) {
t.Errorf("m.Size() = %d, expected %d", m.Size(), len(vecs))
}
if m.Dim() != 2 {
t.Errorf("m.Dim() = %d, expected 2", m.Dim())
}
mVecs := m.Map([]string{"hello", "world"})
if !reflect.DeepEqual(vecs, mVecs) {
t.Errorf("m.Map() = %v, expected %v", mVecs, vecs)
}
x := Expr{"hello": 1.0}
expectedMatches := []Match{
{Word: "hello", Score: 1.0},
{Word: "world", Score: 0.0},
}
matches, err := m.CosN(x, 2)
if err != nil {
t.Errorf("unexpected error from m.CosN(x, 2): %v", err)
}
if len(matches) != 2 {
t.Errorf("len(matches) = %d, expected %d", len(matches), 2)
}
if !reflect.DeepEqual(matches, expectedMatches) {
t.Errorf("m.CosN(x, 2) = %v, expected: %v", matches, expectedMatches)
}
y := Expr{"world": 1.0}
expectedCos := float32(0.0)
c, err := m.Cos(x, y)
if err != nil {
t.Errorf("unexpected error from m.Cos(x, y): %v", err)
}
if c != expectedCos {
t.Errorf("Cos(x, y) = %f, expected %f", c, expectedCos)
}
}
func TestAddWeight(t *testing.T) {
x := Expr{}
y := Expr{}
weights := []float32{0.1, 0.2}
words := []string{"one", "two"}
AddWeight(x, weights, words)
Add(y, 0.1, []string{"one", "two"})
Add(y, 0.1, []string{"two"})
if !reflect.DeepEqual(x, y) {
t.Errorf("x = %v, y = %v", x, y)
}
}