/
entry_and_intermediate_excerpts.rs
277 lines (243 loc) 路 10.3 KB
/
entry_and_intermediate_excerpts.rs
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
use std::collections::HashMap;
use crate::{
config::TitleBoost,
searcher::{HighlightRange, OutputEntry, OutputResult},
LatestVersion::structs::{Entry, PassthroughConfig, WordListSource},
};
use super::intermediate_excerpt::IntermediateExcerpt;
#[derive(Debug)]
pub(super) struct EntryAndIntermediateExcerpts {
pub(super) entry: Entry,
pub(super) config: PassthroughConfig,
pub(super) intermediate_excerpts: Vec<IntermediateExcerpt>,
}
#[allow(clippy::cast_possible_wrap, clippy::cast_possible_truncation)]
impl From<EntryAndIntermediateExcerpts> for OutputResult {
fn from(data: EntryAndIntermediateExcerpts) -> Self {
let entry = data.entry;
let excerpt_buffer = data.config.excerpt_buffer as usize;
let split_contents: Vec<String> = entry
.contents
.split_whitespace()
.map(ToString::to_string)
.collect();
let mut ies: Vec<&IntermediateExcerpt> = data
.intermediate_excerpts
.iter()
.filter(|ie| ie.source == WordListSource::Contents)
.collect();
// Get rid of intermediate excerpts that refer to the same word index.
// But first, sort by score so that only the highest score within the
// same word index is kept.
ies.sort_by_cached_key(|ie| ie.score);
ies.sort_by_cached_key(|ie| ie.word_index);
ies.dedup_by_key(|ie| ie.word_index);
let mut ies_grouped_by_word_index: Vec<Vec<&IntermediateExcerpt>> = vec![];
for ie in &ies {
if let Some(most_recent) = ies_grouped_by_word_index.last_mut() {
if let Some(trailing_ie) = most_recent.first() {
if (ie.word_index as isize) - (trailing_ie.word_index as isize)
< (excerpt_buffer as isize)
{
most_recent.push(ie);
continue;
}
}
}
ies_grouped_by_word_index.push(vec![ie])
}
let mut excerpts: Vec<crate::searcher::Excerpt> = ies_grouped_by_word_index
.iter()
.map(|ies| {
let minimum_word_index = ies
.first()
.unwrap()
.word_index
.saturating_sub(excerpt_buffer);
let maximum_word_index = std::cmp::min(
ies.last()
.unwrap()
.word_index
.saturating_add(excerpt_buffer),
split_contents.len(),
);
let text = split_contents[minimum_word_index..maximum_word_index].join(" ");
let mut highlight_ranges: Vec<HighlightRange> = ies
.iter()
.map(|ie| {
let beginning = split_contents[minimum_word_index..ie.word_index]
.join(" ")
.chars()
.count()
+ 1;
HighlightRange {
beginning,
end: beginning + ie.query.chars().count(),
}
})
.collect();
highlight_ranges.sort();
let highlighted_character_range = highlight_ranges.last().unwrap().end
- highlight_ranges.first().unwrap().beginning;
let highlighted_characters_count: usize = highlight_ranges
.iter()
.map(|hr| hr.end - hr.beginning)
.sum();
let score_modifier = highlighted_character_range - highlighted_characters_count;
let score = ies
.iter()
.map(|ie| (ie.score as usize))
.sum::<usize>()
.saturating_sub(score_modifier);
// Since we're mapping from multiple IntermediateExcerpts to one
// Excerpt, we have to either combine or filter data. For
// `fields` and `internal_annotations`, I'm taking the data from
// the first intermediate excerpt in the vector.
let fields = ies
.first()
.map_or_else(HashMap::new, |first| first.fields.clone());
let internal_annotations = ies
.first()
.map_or_else(Vec::default, |first| first.internal_annotations.clone());
crate::searcher::Excerpt {
text,
highlight_ranges,
score,
internal_annotations,
fields,
}
})
.collect();
excerpts.sort_by_key(|e| -(e.score as i16));
excerpts.truncate(data.config.excerpts_per_result as usize);
let split_title: Vec<&str> = entry.title.split_whitespace().collect();
let mut title_highlight_ranges: Vec<HighlightRange> = data
.intermediate_excerpts
.iter()
.filter(|&ie| ie.source == WordListSource::Title)
.map(|ie| {
let space_offset = if ie.word_index == 0 { 0 } else { 1 };
let beginning = split_title[0..ie.word_index].join(" ").len() + space_offset;
HighlightRange {
beginning,
end: beginning + ie.query.len(),
}
})
.collect();
title_highlight_ranges.sort();
let title_boost_modifier = title_highlight_ranges.len()
* match data.config.title_boost {
TitleBoost::Minimal => 25,
TitleBoost::Moderate => 75,
TitleBoost::Large => 150,
TitleBoost::Ridiculous => 5000,
};
let score = excerpts.first().map_or(0, |first| first.score) + title_boost_modifier;
OutputResult {
entry: OutputEntry::from(entry),
excerpts,
title_highlight_ranges,
score,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn title_highlight_ranges_are_sorted() {
let entry_and_intermediate_excerpts = EntryAndIntermediateExcerpts {
entry: Entry {
contents: "".to_string(),
title: "The quick brown fox jumps over the lazy dog".to_string(),
url: String::default(),
fields: HashMap::default(),
},
config: PassthroughConfig::default(),
intermediate_excerpts: vec![
IntermediateExcerpt {
query: "over".to_string(),
entry_index: 0,
score: 128,
source: WordListSource::Title,
word_index: 3,
internal_annotations: Vec::default(),
fields: HashMap::default(),
},
IntermediateExcerpt {
query: "brown".to_string(),
entry_index: 0,
score: 128,
source: WordListSource::Title,
word_index: 2,
internal_annotations: Vec::default(),
fields: HashMap::default(),
},
],
};
let output_result = OutputResult::from(entry_and_intermediate_excerpts);
let title_highlight_ranges = output_result.title_highlight_ranges;
println!("{:?}", title_highlight_ranges);
assert!(
title_highlight_ranges[0].beginning <= title_highlight_ranges[1].beginning,
"Title highlight ranges were not sorted! [0].beginning is {} while [1].beginning is {}",
title_highlight_ranges[0].beginning,
title_highlight_ranges[1].beginning
);
}
#[test]
fn highlighting_does_not_offset_with_special_characters() {
let entry_and_intermediate_excerpts = EntryAndIntermediateExcerpts {
entry: Entry {
contents: "AFTER a \u{2018}surprisingly\u{2019} unequivocal experience of the inefficiency of the subsisting federal government".to_string(),
title: "Introduction".to_string(),
url: String::default(),
fields: HashMap::default(),
},
config: PassthroughConfig::default(),
intermediate_excerpts: vec![
IntermediateExcerpt {
query: "unequivocal".to_string(),
entry_index: 0,
score: 128,
source: WordListSource::Contents,
word_index: 3,
internal_annotations: Vec::default(),
fields: HashMap::default(),
},
IntermediateExcerpt {
query: "\u{2018}surprisingly\u{2019}".to_string(),
entry_index: 0,
score: 128,
source: WordListSource::Contents,
word_index: 2,
internal_annotations: Vec::default(),
fields: HashMap::default(),
},
],
};
let output_result = OutputResult::from(entry_and_intermediate_excerpts);
let excerpt = output_result.excerpts.first().unwrap();
let excerpt_chars = excerpt.text.chars().collect::<Vec<char>>();
let first_highlight_range = &excerpt.highlight_ranges.first().unwrap();
let computed_first_word = &excerpt_chars
[first_highlight_range.beginning..first_highlight_range.end]
.iter()
.collect::<String>();
let second_highlight_range = &excerpt.highlight_ranges[1];
let computed_second_word = &excerpt_chars
[second_highlight_range.beginning..second_highlight_range.end]
.iter()
.collect::<String>();
assert_eq!(
computed_second_word, "unequivocal",
"Expected `unequivocal`, got {}",
computed_second_word
);
assert_eq!(
computed_first_word, "\u{2018}surprisingly\u{2019}",
"Expected `\u{2018}surprisingly\u{2019}`, got {}",
computed_first_word
);
}
}