forked from lenaxia/bm25-golang
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbatched_test.go
More file actions
168 lines (149 loc) · 5.69 KB
/
Copy pathbatched_test.go
File metadata and controls
168 lines (149 loc) · 5.69 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package bm25
import (
"strings"
"testing"
)
func TestGetScoresBatched(t *testing.T) {
corpus := []string{"hello world", "this is a test"}
tokenizer := func(s string) []string { return strings.Split(s, " ") }
bm25, _ := NewBM25Okapi(corpus, tokenizer, 1.2, 0.75, nil)
// Test case: Getting scores in batches for an empty query
_, err := bm25.GetScoresBatched([]string{}, bm25, 1)
if err == nil {
t.Errorf("Expected an error for an empty query, but got nil")
}
// Test case: Getting scores in batches with an invalid batch size
_, err = bm25.GetScoresBatched([]string{"hello"}, bm25, 0)
if err == nil {
t.Errorf("Expected an error for an invalid batch size, but got nil")
}
// Test case: Getting scores in batches for a single-term query
scores, err := bm25.GetScoresBatched([]string{"hello"}, bm25, 1)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
expected := []float64{0.6931471805599453, 0.0}
if len(scores) != len(expected) {
t.Errorf("Expected %d scores, but got %d", len(expected), len(scores))
}
for i, score := range scores {
if score != expected[i] {
t.Errorf("Expected score %.2f at index %d, but got %.2f", expected[i], i, score)
}
}
// Test case: Getting scores in batches for a multi-term query
scores, err = bm25.GetScoresBatched([]string{"this", "test"}, bm25, 1)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
expected = []float64{0.0, 1.3862943611198906}
if len(scores) != len(expected) {
t.Errorf("Expected %d scores, but got %d", len(expected), len(scores))
}
for i, score := range scores {
if score != expected[i] {
t.Errorf("Expected score %.2f at index %d, but got %.2f", expected[i], i, score)
}
}
}
func TestGetBatchScoresBatched(t *testing.T) {
corpus := []string{"hello world", "this is a test"}
tokenizer := func(s string) []string { return strings.Split(s, " ") }
bm25, _ := NewBM25Okapi(corpus, tokenizer, 1.2, 0.75, nil)
// Test case: Getting batch scores in batches for an empty query
_, err := bm25.GetBatchScoresBatched([]string{}, []int{0, 1}, bm25, 1)
if err == nil {
t.Errorf("Expected an error for an empty query, but got nil")
}
// Test case: Getting batch scores in batches for an empty document IDs slice
_, err = bm25.GetBatchScoresBatched([]string{"hello"}, []int{}, bm25, 1)
if err == nil {
t.Errorf("Expected an error for an empty document IDs slice, but got nil")
}
// Test case: Getting batch scores in batches with an invalid batch size
_, err = bm25.GetBatchScoresBatched([]string{"hello"}, []int{0, 1}, bm25, 0)
if err == nil {
t.Errorf("Expected an error for an invalid batch size, but got nil")
}
// Test case: Getting batch scores in batches for invalid document IDs
_, err = bm25.GetBatchScoresBatched([]string{"hello"}, []int{-1, 2}, bm25, 1)
if err == nil {
t.Errorf("Expected an error for invalid document IDs, but got nil")
}
// Test case: Getting batch scores in batches for a single-term query
scores, err := bm25.GetBatchScoresBatched([]string{"hello"}, []int{0}, bm25, 1)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
expected := []float64{0.6931471805599453}
if len(scores) != len(expected) {
t.Errorf("Expected %d scores, but got %d", len(expected), len(scores))
}
for i, score := range scores {
if score != expected[i] {
t.Errorf("Expected score %.2f at index %d, but got %.2f", expected[i], i, score)
}
}
// Test case: Getting batch scores in batches for a multi-term query
scores, err = bm25.GetBatchScoresBatched([]string{"this", "test"}, []int{1}, bm25, 1)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
expected = []float64{1.3862943611198906}
if len(scores) != len(expected) {
t.Errorf("Expected %d scores, but got %d", len(expected), len(scores))
}
for i, score := range scores {
if score != expected[i] {
t.Errorf("Expected score %.2f at index %d, but got %.2f", expected[i], i, score)
}
}
}
func TestGetTopNBatched(t *testing.T) {
corpus := []string{"hello world", "this is a test"}
tokenizer := func(s string) []string { return strings.Split(s, " ") }
bm25, _ := NewBM25Okapi(corpus, tokenizer, 1.2, 0.75, nil)
// Test case: Getting top N documents in batches for an empty query
_, err := bm25.GetTopNBatched([]string{}, 2, bm25, 1)
if err == nil {
t.Errorf("Expected an error for an empty query, but got nil")
}
// Test case: Getting top N documents in batches with n <= 0
_, err = bm25.GetTopNBatched([]string{"hello"}, 0, bm25, 1)
if err == nil {
t.Errorf("Expected an error for n <= 0, but got nil")
}
// Test case: Getting top N documents in batches with an invalid batch size
_, err = bm25.GetTopNBatched([]string{"hello"}, 1, bm25, 0)
if err == nil {
t.Errorf("Expected an error for an invalid batch size, but got nil")
}
// Test case: Getting top N documents in batches for a single-term query
topDocs, err := bm25.GetTopNBatched([]string{"hello"}, 1, bm25, 1)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
expected := []string{"hello world"}
if len(topDocs) != len(expected) {
t.Errorf("Expected %d top documents, but got %d", len(expected), len(topDocs))
}
for i, doc := range topDocs {
if doc != expected[i] {
t.Errorf("Expected document '%s' at index %d, but got '%s'", expected[i], i, doc)
}
}
// Test case: Getting top N documents in batches for a multi-term query
topDocs, err = bm25.GetTopNBatched([]string{"this", "test"}, 1, bm25, 1)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
expected = []string{"this is a test"}
if len(topDocs) != len(expected) {
t.Errorf("Expected %d top documents, but got %d", len(expected), len(topDocs))
}
for i, doc := range topDocs {
if doc != expected[i] {
t.Errorf("Expected document '%s' at index %d, but got '%s'", expected[i], i, doc)
}
}
}