tobogganSe connecter

louise

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

197 lignes · 5148 octetsBrut · Historique
1// The trigram index answers exactly the documents containing the query
2// (a substring match on the normalized text), checked against a naive
3// scan on random corpora.
4
5import gen
6import gen/check
7import search
8import std/list
9import std/random.{type random}
10import std/string
11import std/test.{assert, assert_equal}
12
13fn notes :: -> search.Index
14{
15 search.build
16 [ (1, "Toki Pona notes")
17 , (2, "TODO: plant the toki pona stickers")
18 , (3, "Shopping: milk, eggs")
19 ]
20}
21
22@test
23fn normalize_lowercases_and_trims ::
24{
25 assert_equal (search.normalize " ToKi Pona\t") "toki pona"
26 assert_equal (search.normalize "") ""
27}
28
29@test
30fn trigrams_are_the_distinct_three_character_windows ::
31{
32 assert_equal (search.trigrams "abcd") ["abc", "bcd"]
33 assert_equal (search.trigrams "aaaa") ["aaa"]
34 assert_equal (search.trigrams "ab") []
35 assert_equal (search.trigrams "") []
36}
37
38@test
39fn search_is_a_case_insensitive_substring_match ::
40{
41 let idx = notes ()
42 assert_equal (search.search idx "toki") [1, 2]
43 assert_equal (search.search idx "PONA") [1, 2]
44 assert_equal (search.search idx "toki pona") [1, 2]
45 assert_equal (search.search idx "milk") [3]
46 assert_equal (search.search idx " bread ") []
47 assert_equal (search.size idx) 3
48}
49
50@test
51fn an_empty_query_matches_everything ::
52{
53 assert_equal (search.search (notes ()) "") [1, 2, 3]
54 assert_equal (search.search (notes ()) " ") [1, 2, 3]
55}
56
57@test
58fn a_short_query_scans_the_documents ::
59{
60 assert_equal (search.search (notes ()) "to") [1, 2]
61 assert_equal (search.search (notes ()) "M") [3]
62 assert_equal (search.search (notes ()) "z") []
63}
64
65@test
66fn trigrams_alone_do_not_decide_a_candidate_is_verified ::
67{
68 // Every trigram of "abxy" occurs in the document, but "abxy" does not.
69 let idx = search.build [(1, "abx bxy")]
70 assert_equal (search.search idx "abxy") []
71 assert_equal (search.search idx "bxy") [1]
72}
73
74@test
75fn add_stores_and_replaces_a_document ::
76{
77 let idx = search.add (notes ()) 4 "a bread recipe"
78 assert_equal (search.search idx "bread") [4]
79 // Adding an existing id replaces its text.
80 let idx = search.add idx 4 "a cake recipe"
81 assert_equal (search.search idx "bread") []
82 assert_equal (search.search idx "cake") [4]
83 assert_equal (search.size idx) 4
84}
85
86@test
87fn remove_drops_a_document ::
88{
89 let idx = search.remove (notes ()) 2
90 assert_equal (search.search idx "toki") [1]
91 assert_equal (search.size idx) 2
92 // Removing an absent id changes nothing.
93 assert_equal (search.search (search.remove idx 99) "toki") [1]
94}
95
96@test
97fn a_duplicate_id_in_build_keeps_the_last_text ::
98{
99 let idx = search.build [(1, "first text"), (1, "second text")]
100 assert_equal (search.search idx "second") [1]
101 assert_equal (search.search idx "first") []
102}
103
104@test
105@should_panic "negative id"
106fn a_negative_id_panics ::
107{
108 let _ = search.build [(0 - 1, "nope")]
109 ()
110}
111
112// ---- against a naive scan ---------------------------------------------------
113
114/// A small corpus; the ids are the positions.
115fn corpus :: -> List<(Int, String)> / <random>
116{
117 let texts =
118 gen.list
119 ~min: 0
120 ~max: 14
121 fn { gen.string ~alphabet: "abcAB " ~min: 0 ~max: 24 }
122 list.zip (list.range 0 (list.length texts - 1)) texts
123}
124
125/// Mostly a slice of a document (a likely match), sometimes anything.
126fn query :: docs: List<(Int, String)> -> String / <random>
127{
128 if docs != [] && gen.int ~low: 1 ~high: 10 <= 6
129 {
130 let (_, text) = gen.pick docs
131 let n = string.length text
132 let start = gen.int ~low: 0 ~high: n
133 string.slice text start (start + gen.int ~low: 0 ~high: 8)
134 }
135 else
136 {
137 gen.string ~alphabet: "abcAB " ~min: 0 ~max: 8
138 }
139}
140
141fn case :: -> (List<(Int, String)>, String) / <random>
142{
143 let docs = corpus ()
144 (docs, query docs)
145}
146
147/// The ids of the documents containing `q`, ascending: the scan `search`
148/// must agree with.
149fn naive :: docs: List<(Int, String)> q: String -> List<Int>
150{
151 let q = search.normalize q
152 list.sort
153 ( list.map
154 (list.filter docs fn d { string.contains (search.normalize d.1) q })
155 fn d { d.0 }
156 )
157}
158
159fn matches_the_naive_scan :: case: (List<(Int, String)>, String) -> Bool
160{
161 let (docs, q) = case
162 let expected = naive docs q
163 let built = search.build docs
164 let added =
165 list.fold docs (search.empty ()) fn idx d { search.add idx d.0 d.1 }
166 search.search built q == expected && search.search added q == expected
167}
168
169@test
170fn search_matches_the_naive_scan ::
171{
172 check.seeded ~seed: 201 ~runs: 100 case matches_the_naive_scan
173}
174
175/// A random document of the corpus is removed; the rest still match.
176fn removed_case :: -> (List<(Int, String)>, Int, String) / <random>
177{
178 let docs = corpus ()
179 let gone = gen.int ~low: 0 ~high: 20
180 (docs, gone, query docs)
181}
182
183fn remove_matches_the_naive_scan ::
184 case: (List<(Int, String)>, Int, String)
185 -> Bool
186{
187 let (docs, gone, q) = case
188 let idx = search.remove (search.build docs) gone
189 let expected = naive (list.filter docs fn d { d.0 != gone }) q
190 search.search idx q == expected
191}
192
193@test
194fn remove_matches_the_naive_scan_of_the_rest ::
195{
196 check.seeded ~seed: 202 ~runs: 100 removed_case remove_matches_the_naive_scan
197}