-
Notifications
You must be signed in to change notification settings - Fork 2
/
search_test.go
66 lines (53 loc) · 1.58 KB
/
search_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
package weakand
import (
"flag"
"path"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
var (
pretty bool
indexDumpDir string
)
func init() {
flag.BoolVar(&pretty, "pretty", false, "Pretty print index and frontier when calling Search")
flag.StringVar(&indexDumpDir, "indexDir", "/tmp", "Directory containing index dumps")
}
func TestSearch(t *testing.T) {
guaranteeSegmenter(&sgmt)
idx := testBuildIndex()
rs := idx.Search(strings.Join(idx.Vocab.Terms, " "), 10, pretty) // Pretty print intermediate steps.
assert.Equal(t, len(testingCorpus), len(rs)) // All documents should be retrieved.
for _, r := range rs {
assert.Equal(t, 0.5, r.Score) // Jaccard coeffcient of all documents should be 1/2.
}
}
func TestSearchWithAAAI14Titles(t *testing.T) {
testWithBigData(t,
"github.com/topicai/weakand/testdata/aaai14papers.txt",
"incomplete ontologies",
"aaai14titlesindex.csv")
}
func TestSearchWithZhWikiNews(t *testing.T) {
testWithBigData(t,
"github.com/topicai/weakand/testdata/zhwikinews.txt",
"中药商",
"zhwikinewsindex.csv")
}
func testWithBigData(t *testing.T, corpusFile string, query string, indexDumpFile string) {
guaranteeSegmenter(&sgmt)
idx := NewIndexFromFile(
path.Join(gosrc(), corpusFile),
sgmt,
path.Join(indexDumpDir, indexDumpFile))
q := NewQuery(query, idx.Vocab, sgmt)
for _, r := range idx.Search(query, 10, pretty) {
doc := strings.ToLower(r.Literal)
contain := false
for qterm := range q.Terms {
contain = contain || strings.Contains(doc, idx.Vocab.Term(qterm))
}
assert.True(t, contain)
}
}