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
#![allow(clippy::inline_always)]
use crate::simple::search_index::SearchIndex;
use kstring::KString;
use std::{collections::BTreeSet, hash::Hash};
// -----------------------------------------------------------------------------
//
/// This `struct` is used to access the
/// [rapidfuzz](https://crates.io/crates/rapidfuzz) crate in a generic manner.
pub struct Rapidfuzz;
// -----------------------------------------------------------------------------
//
/// This `trait` implementation is used to access the
/// [rapidfuzz](https://crates.io/crates/rapidfuzz) crate in a generic manner.
impl<'s, K: Hash + Ord> crate::simple::internal::fuzzers::Fuzzy<'s, K> for Rapidfuzz {
fn autocomplete_keyword(
search_index: &'s crate::simple::search_index::SearchIndex<K>,
last_autocomplete_options: &mut Vec<&'s str>,
keyword: &str,
) {
if last_autocomplete_options.is_empty() {
// No autocomplete options were found for the user's last
// (partial) keyword. Attempt to use fuzzy string search to find
// other autocomplete options:
let fuzzy_search_results: Vec<&str> = search_index
.rapidfuzz_keyword(keyword)
// The above method returns both the keyword and the keys. We're
// searching for keywords, so discard the keys.
.map(|(keyword, _keys)| keyword)
// Convert `&KString` to `&str`:
.map(kstring::KStringBase::as_str)
// Collect all keyword autocompletions into a `Vec`:
.collect();
*last_autocomplete_options = fuzzy_search_results;
} // if
} // fn
#[inline(always)]
fn autocomplete_global(
search_index: &'s SearchIndex<K>,
preceding_keywords: &[KString],
last_autocomplete_options: &mut Vec<&'s KString>,
last_keyword: &str,
) {
if last_autocomplete_options.is_empty() {
// No search results were found for the user's last (partial)
// keyword. Attempt to use fuzzy string search to find other
// options:
let fuzzy_search_results: Vec<&KString> =
search_index.rapidfuzz_global(
preceding_keywords,
last_keyword
)
// The above method returns both the keyword and the keys. We're
// searching for keywords, so discard the keys.
.map(|(keyword, _keys)| keyword)
// Collect all keyword autocompletions into a `Vec`:
.collect();
*last_autocomplete_options = fuzzy_search_results;
} // if
} // fn
#[inline(always)]
fn autocomplete_context(
search_index: &'s SearchIndex<K>,
preceding_keywords: &[KString],
preceding_results: &BTreeSet<&'s K>,
last_keyword: &str,
last_autocomplete_options: &mut Vec<&'s KString>,
) {
if last_autocomplete_options.is_empty() {
// No search results were found for the user's last (partial)
// keyword. Attempt to use fuzzy string search to find other
// options:
let fuzzy_search_results: Vec<&KString> =
search_index.rapidfuzz_context(
preceding_keywords,
preceding_results,
last_keyword
)
// The above method returns both the keyword and the keys. We're
// searching for keywords, so discard the keys.
.map(|(keyword, _keys)| keyword)
// Collect all keyword autocompletions into a `Vec`:
.collect();
*last_autocomplete_options = fuzzy_search_results;
} // if
} // fn
#[inline(always)]
fn live_search_keyword(
search_index: &'s SearchIndex<K>,
search_results: &mut BTreeSet<&'s K>,
user_keyword: &str,
) {
if search_results.is_empty() {
// No search results were found for the user's last (partial)
// keyword. Attempt to use fuzzy string search to find other
// options:
let fuzzy_search_results: BTreeSet<&K> = search_index
.rapidfuzz_keyword(user_keyword)
// The above method returns both the keyword and the keys. We're
// searching for keys, so discard the keywords.
.flat_map(|(_keyword, keys)| keys)
// Only return `maximum_search_results` number of keys.
//
// Note that the above method returns
// `maximum_autocomplete_options` items. However each item which
// is represented by a single keyword and multiple keys. The
// keys could expand into more than `maximum_search_results`
// keys. This additional filter is required.
.take(search_index.maximum_search_results)
// Collect all fuzzy key results into a `BTreeSet`:
.collect();
*search_results = fuzzy_search_results;
} // if
} // fn
#[inline(always)]
fn live_search_context(
search_index: &'s SearchIndex<K>,
preceding_results: &BTreeSet<&'s K>,
preceding_keywords: &[KString],
last_results: &mut BTreeSet<&'s K>,
last_keyword: &str,
) {
if last_results.is_empty() {
// No search results were found for the user's last (partial)
// keyword. Attempt to use fuzzy string search to find other
// options:
let fuzzy_search_results: BTreeSet<&K> =
search_index.rapidfuzz_context(
preceding_keywords,
preceding_results,
last_keyword
)
// The above method returns both the keyword and the keys. We're
// searching for keys, so discard the keywords.
.flat_map(|(_keyword, keys)| keys)
// Only return `maximum_search_results` number of keys.
//
// Note that the above method returns
// `maximum_autocomplete_options` items. However each item which
// is represented by a single keyword and multiple keys. The
// keys could expand into more than `maximum_search_results`
// keys. This additional filter is required.
.take(search_index.maximum_search_results)
// Collect all fuzzy key results into a `BTreeSet`:
.collect();
*last_results = fuzzy_search_results;
} // if
} // fn
} // trait Fuzzy