Repository navigation
Expand file tree
/
Copy pathtext_replacements.rs
More file actions
368 lines (336 loc) · 13.2 KB
/
Copy pathtext_replacements.rs
File metadata and controls
368 lines (336 loc) · 13.2 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
use std::sync::OnceLock;
use regex::{Regex, RegexBuilder};
/// One configured phrase and the text that replaces it.
#[derive(Clone, Debug, Default, serde::Deserialize, Eq, PartialEq, serde::Serialize)]
#[serde(default)]
pub struct TextReplacement {
pub matched_phrase: String,
pub output: String,
}
#[derive(Clone)]
struct CompiledRule {
matcher: Regex,
output: String,
}
struct Candidate {
start: usize,
end: usize,
rule: usize,
}
#[derive(Clone, Default)]
pub(crate) struct ReplacementSet {
rules: Vec<CompiledRule>,
}
impl ReplacementSet {
pub(crate) fn new(rules: &[TextReplacement]) -> Self {
let rules = rules
.iter()
.filter_map(|rule| {
let matched_phrase = rule.matched_phrase.trim();
if matched_phrase.is_empty() {
return None;
}
RegexBuilder::new(®ex::escape(matched_phrase))
.case_insensitive(true)
.unicode(true)
.build()
.ok()
.map(|matcher| CompiledRule {
matcher,
output: rule.output.clone(),
})
})
.collect();
Self { rules }
}
pub(crate) fn replace(&self, text: &str) -> String {
let mut candidates = self
.rules
.iter()
.enumerate()
.flat_map(|(rule, replacement)| {
phrase_matches(&replacement.matcher, text).map(move |(start, end)| Candidate {
start,
end,
rule,
})
})
.collect::<Vec<_>>();
candidates.sort_by(|left, right| {
(right.end - right.start)
.cmp(&(left.end - left.start))
.then_with(|| left.start.cmp(&right.start))
.then_with(|| left.rule.cmp(&right.rule))
});
let mut selected = Vec::<Candidate>::new();
for candidate in candidates {
if selected
.iter()
.any(|existing| candidate.start < existing.end && existing.start < candidate.end)
{
continue;
}
selected.push(candidate);
}
if selected.is_empty() {
return text.into();
}
selected.sort_by_key(|candidate| candidate.start);
let mut output = String::with_capacity(text.len());
let mut cursor = 0;
let mut capitalize_next = false;
for (index, candidate) in selected.iter().enumerate() {
if cursor < candidate.start {
push_segment(
&mut output,
&text[cursor..candidate.start],
&mut capitalize_next,
);
}
let replacement = &self.rules[candidate.rule].output;
if !replacement.is_empty() {
push_segment(&mut output, replacement, &mut capitalize_next);
cursor = candidate.end;
continue;
}
let next_limit = selected
.get(index + 1)
.map_or(text.len(), |next| next.start);
let between = &text[candidate.end..next_limit];
if at_sentence_start(&output) {
if text[candidate.start..candidate.end]
.chars()
.find(|character| character.is_alphabetic())
.is_some_and(char::is_uppercase)
{
capitalize_next = true;
}
let after_punct = between.trim_start_matches(CLAUSE_PUNCTUATION);
let after_spaces = after_punct.trim_start_matches(HORIZONTAL_WHITESPACE);
cursor = candidate.end + (between.len() - after_spaces.len());
if (after_spaces.is_empty() && next_limit == text.len())
|| after_spaces.starts_with('\n')
{
trim_trailing_horizontal_spaces(&mut output);
}
} else {
let remaining = text[candidate.end..].trim_start_matches(HORIZONTAL_WHITESPACE);
if between.starts_with(CLAUSE_PUNCTUATION)
|| remaining.is_empty()
|| remaining.starts_with('\n')
{
trim_trailing_horizontal_spaces(&mut output);
if output.ends_with(',') {
output.pop();
}
if remaining.is_empty() || remaining.starts_with('\n') {
let after_spaces = between.trim_start_matches(HORIZONTAL_WHITESPACE);
cursor = candidate.end + (between.len() - after_spaces.len());
} else {
cursor = candidate.end;
}
} else if output.ends_with(HORIZONTAL_WHITESPACE) {
let after_spaces = between.trim_start_matches(HORIZONTAL_WHITESPACE);
cursor = candidate.end + (between.len() - after_spaces.len());
} else {
cursor = candidate.end;
}
}
}
if cursor < text.len() {
push_segment(&mut output, &text[cursor..], &mut capitalize_next);
}
output
}
}
const HORIZONTAL_WHITESPACE: [char; 2] = [' ', '\t'];
const CLAUSE_PUNCTUATION: [char; 6] = ['.', '!', '?', ',', ';', ':'];
const SENTENCE_ENDINGS: [char; 4] = ['.', '!', '?', '\n'];
fn at_sentence_start(output: &str) -> bool {
let trimmed = output.trim_end_matches(HORIZONTAL_WHITESPACE);
trimmed.is_empty() || trimmed.ends_with(SENTENCE_ENDINGS)
}
fn trim_trailing_horizontal_spaces(output: &mut String) {
let trimmed_len = output.trim_end_matches(HORIZONTAL_WHITESPACE).len();
output.truncate(trimmed_len);
}
fn push_segment(output: &mut String, segment: &str, capitalize_next: &mut bool) {
if !*capitalize_next || segment.is_empty() {
output.push_str(segment);
if segment.chars().any(|character| !character.is_whitespace()) {
*capitalize_next = false;
}
return;
}
let Some((index, character)) = segment
.char_indices()
.find(|(_, character)| !character.is_whitespace())
else {
output.push_str(segment);
return;
};
output.push_str(&segment[..index]);
if character.is_lowercase() && at_sentence_start(output) {
output.extend(character.to_uppercase());
} else {
output.push(character);
}
output.push_str(&segment[index + character.len_utf8()..]);
*capitalize_next = false;
}
/// Finds whole-phrase matches. A match rejected for touching a word resumes the
/// search one character later, so an overlapping whole-phrase match is still found.
fn phrase_matches<'a>(
matcher: &'a Regex,
text: &'a str,
) -> impl Iterator<Item = (usize, usize)> + 'a {
let mut from = 0;
std::iter::from_fn(move || {
while let Some(matched) = matcher.find_at(text, from) {
if phrase_boundary(text, matched.start(), matched.end()) {
from = matched.end();
return Some((matched.start(), matched.end()));
}
from = matched.start()
+ text[matched.start()..]
.chars()
.next()
.map_or(1, char::len_utf8);
}
None
})
}
fn phrase_boundary(text: &str, start: usize, end: usize) -> bool {
let before = text[..start].chars().next_back();
let after = text[end..].chars().next();
!before.is_some_and(word_character) && !after.is_some_and(word_character)
}
fn word_character(character: char) -> bool {
space_delimited_script(character) && (character.is_alphanumeric() || character == '_')
}
/// Scripts that do not put spaces between words, or attach particles to the
/// word they modify. Their characters are Unicode word characters, so a phrase
/// surrounded by them would never look like it stands on its own and
/// replacements inside these sentences would never match.
fn space_delimited_script(character: char) -> bool {
static UNSPACED: OnceLock<Regex> = OnceLock::new();
let unspaced = UNSPACED.get_or_init(|| {
Regex::new(
r"^[\p{Han}\p{Hiragana}\p{Katakana}\p{Hangul}\p{Thai}\p{Lao}\p{Khmer}\p{Myanmar}\p{Tibetan}]$",
)
.expect("the unspaced-script pattern is valid")
});
let mut buffer = [0; 4];
!unspaced.is_match(character.encode_utf8(&mut buffer))
}
#[cfg(test)]
mod tests {
use super::*;
fn rule(matched_phrase: &str, output: &str) -> TextReplacement {
TextReplacement {
matched_phrase: matched_phrase.into(),
output: output.into(),
}
}
#[test]
fn replaces_globally_on_unicode_phrase_boundaries_without_disturbing_punctuation() {
let replacements = ReplacementSet::new(&[
rule("cafe au lait", "café au lait"),
rule("open code", "OpenCode"),
rule("über", "Über"),
]);
assert_eq!(
replacements.replace("OPEN CODE, open code. café au lait; cafe au lait! ÜBER."),
"OpenCode, OpenCode. café au lait; café au lait! Über."
);
assert_eq!(
replacements.replace("reopen code, open codes, übercool"),
"reopen code, open codes, übercool"
);
}
#[test]
fn replaces_phrases_adjacent_to_unspaced_scripts() {
let replacements = ReplacementSet::new(&[
rule("issue", "Issue"),
rule("open code", "OpenCode"),
rule("严肃", "严肃"),
rule("이슈", "issue"),
]);
assert_eq!(replacements.replace("这个issue很重要"), "这个Issue很重要");
assert_eq!(replacements.replace("用open code写"), "用OpenCode写");
assert_eq!(replacements.replace("ใช้issueบ่อย"), "ใช้Issueบ่อย");
assert_eq!(replacements.replace("이슈를 만들었다"), "issue를 만들었다");
assert_eq!(replacements.replace("issue很重要"), "Issue很重要");
assert_eq!(replacements.replace("这个很重要issue"), "这个很重要Issue");
assert_eq!(replacements.replace("issue_id"), "issue_id");
assert_eq!(replacements.replace("issue2"), "issue2");
assert_eq!(replacements.replace("reissues"), "reissues");
}
#[test]
fn longest_match_wins_and_replacements_do_not_recurse() {
let replacements = ReplacementSet::new(&[
rule("open", "closed"),
rule("open code", "OpenCode"),
rule("code then open", "combined"),
rule("closed", "shut"),
]);
assert_eq!(
replacements.replace("open code then open"),
"closed combined"
);
}
#[test]
fn outputs_do_not_trigger_other_rules_during_the_same_pass() {
let replacements = ReplacementSet::new(&[
rule("open code", "OpenCode"),
rule("alpha", "beta release"),
rule("beta", "gamma"),
]);
let corrected = replacements.replace("OPEN CODE alpha");
assert_eq!(corrected, "OpenCode beta release");
assert_eq!(replacements.replace("beta release"), "gamma release");
}
#[test]
fn a_match_rejected_inside_a_word_does_not_hide_an_overlapping_phrase() {
let replacements = ReplacementSet::new(&[rule("I I", "I"), rule("the the", "the")]);
assert_eq!(replacements.replace("Hi I I think so."), "Hi I think so.");
assert_eq!(
replacements.replace("Breathe the the air."),
"Breathe the air."
);
}
#[test]
fn duplicate_phrases_use_settings_order_deterministically() {
let replacements = ReplacementSet::new(&[rule("hex", "first"), rule("HEX", "second")]);
assert_eq!(replacements.replace("hex"), "first");
}
#[test]
fn empty_replacements_delete_phrases_and_collapse_surrounding_spacing_and_punctuation() {
let replacements =
ReplacementSet::new(&[rule("um", ""), rule("uh", ""), rule("thank you", "")]);
assert_eq!(replacements.replace("I um think so."), "I think so.");
assert_eq!(replacements.replace("I, um, think so."), "I, think so.");
assert_eq!(replacements.replace("I, um, uh, think so."), "I, think so.");
assert_eq!(
replacements.replace("Um, the build passed."),
"The build passed."
);
assert_eq!(
replacements.replace("Um, uh, the build passed."),
"The build passed."
);
assert_eq!(
replacements.replace("um, the build passed."),
"the build passed."
);
assert_eq!(replacements.replace("I think so um."), "I think so.");
assert_eq!(replacements.replace("I think so, um."), "I think so.");
assert_eq!(replacements.replace("I think so, um"), "I think so");
assert_eq!(replacements.replace("Thank you."), "");
assert_eq!(replacements.replace("Hello. Thank you."), "Hello.");
assert_eq!(
replacements.replace("Hello. Thank you. The build passed."),
"Hello. The build passed."
);
}
}