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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
//! §10.7 initial-letter contractions, pronunciation-gated (feature
//! `english_ueb_cmudict`).
//!
//! Extends the safe spelling-only set in [`super::rule_10_7`] with the
//! contractions that module defers as pronunciation-dependent. A contraction is
//! applied at a position ONLY when the whole word's CMUdict pronunciation
//! contains the standalone contraction word's pronunciation as a contiguous
//! phoneme run (comparing ARPABET base symbols, ignoring stress) — i.e. the
//! letters keep their normal sound there: `someone` /-wʌn/ → use `one`;
//! `money` /-ʌni/ and `component` → spell out. All recorded pronunciations of
//! the word must agree, mirroring the conservative §10.6 classifier: a wrong
//! contraction is far worse than a missed one, so unknown words or non-matching
//! pronunciations spell out.
//!
//! Spelling-based occurrences whose sound differs (`acknowledge`→`know` /-nɒl-/,
//! `Germany`→`many` /-ʌnɪ/) are *safely missed* by this gate (no contraction),
//! never mis-encoded.
use phf::phf_map;
use super::contraction::{ContractionMatch, ContractionRule};
use super::pronunciation::{Phoneme, PronunciationProvider};
use crate::unicode::decode_unicode;
/// §10.7 deferred initial-letter contractions → (prefix cell, first-letter cell),
/// taken from the RUEB 2024 §10.7 examples.
/// The unambiguous ones (`right`, `cannot`, `world`, …) already live ungated in
/// [`super::rule_10_7`]; this table holds only the pronunciation-gated remainder.
static PRON_CONTRACTIONS: phf::Map<&'static str, [u8; 2]> = phf_map! {
// ⠐ (dot 5) prefix.
"day" => [decode_unicode('⠐'), decode_unicode('⠙')],
"ever" => [decode_unicode('⠐'), decode_unicode('⠑')],
"father" => [decode_unicode('⠐'), decode_unicode('⠋')],
"here" => [decode_unicode('⠐'), decode_unicode('⠓')],
"know" => [decode_unicode('⠐'), decode_unicode('⠅')],
// `lord` deferred: its phonemes appear contiguously across a morpheme
// boundary in `chlordane` (chlor|dane), a §10.11 bridging case this gate
// cannot detect without syllabification — a wrong contraction is worse than
// the few missed `lordship`/`Gaylord` wins.
"mother" => [decode_unicode('⠐'), decode_unicode('⠍')],
"part" => [decode_unicode('⠐'), decode_unicode('⠏')],
"some" => [decode_unicode('⠐'), decode_unicode('⠎')],
"under" => [decode_unicode('⠐'), decode_unicode('⠥')],
"where" => [decode_unicode('⠐'), decode_unicode('⠱')],
"work" => [decode_unicode('⠐'), decode_unicode('⠺')],
"name" => [decode_unicode('⠐'), decode_unicode('⠝')],
"one" => [decode_unicode('⠐'), decode_unicode('⠕')],
"there" => [decode_unicode('⠐'), decode_unicode('⠮')],
"time" => [decode_unicode('⠐'), decode_unicode('⠞')],
"young" => [decode_unicode('⠐'), decode_unicode('⠽')],
// ⠘ (dots 4-5) prefix. (`word` moved to the ungated spelling-only §10.7 set.)
"these" => [decode_unicode('⠘'), decode_unicode('⠮')],
"those" => [decode_unicode('⠘'), decode_unicode('⠹')],
"upon" => [decode_unicode('⠘'), decode_unicode('⠥')],
// ⠸ (dots 4-5-6) prefix. (`many` moved to the ungated spelling-only §10.7 set.)
// `had` is pronunciation-gated (unlike the ungated `many`/`world`), so it is not
// taken where the letters take a different sound (`shadow` /SH AE D/, not
// /HH AE D/) — `haddock`, `Galahad`, `hadn't` contract, `shadow` spells out.
"had" => [decode_unicode('⠸'), decode_unicode('⠓')],
"their" => [decode_unicode('⠸'), decode_unicode('⠮')],
};
/// Pronunciation-gated §10.7 initial-letter contraction rule.
pub struct InitialContractionPronunciationRule {
provider: Box<dyn PronunciationProvider>,
}
impl InitialContractionPronunciationRule {
/// Build the rule with the pronunciation source used to gate each contraction.
pub fn new(provider: Box<dyn PronunciationProvider>) -> Self {
Self { provider }
}
/// True iff EVERY recorded pronunciation of `full_word` contains SOME recorded
/// pronunciation of `contraction` as a contiguous phoneme run.
fn pronunciation_supports(&self, full_word: &str, contraction: &str) -> bool {
let full = self.provider.pronunciations(full_word);
let sub = self.provider.pronunciations(contraction);
if full.is_empty() || sub.is_empty() {
return false;
}
full.iter()
.all(|fp| sub.iter().any(|sp| contains_contiguous(fp, sp)))
}
/// §10.7: a SUFFICIENT acceptance for a contraction whose letters end in a silent
/// `e` (`one`, `some`, `name`, `time`, `here`, `there`, `where`, `these`,
/// `those`). The letters form the `…one`/`…ere` unit — word-final OR medial —
/// when the trailing `e` is silent or `ey`-merged in every recorded pronunciation
/// (`cone`, `atonement`, `honey`); a `e` voicing its own vowel splits the unit
/// (`krone` /…ə/, `Monet` /…eɪ/, `phonetic` /…ɛ…/) — see [`super::pronunciation::aligner`].
/// A preceding vowel letter is rejected so the first letter never continues a
/// vowel digraph (`B[oo]ne`, `R[oo]ney`).
fn silent_final_e_supports(&self, word: &[char], pos: usize, key: &str, full: &str) -> bool {
if !key.ends_with('e') {
return false;
}
// The preceding-vowel guard stops a VOWEL-initial contraction (`one`, the
// `ere` of `here`/`there`/`where`) from continuing a vowel digraph
// (`B[oo]ne`, `R[oo]ney`). A CONSONANT-initial contraction (`some`, `name`,
// `time`, `here`, `there`, `where`) cannot form a digraph with a preceding
// vowel, so the guard does not apply — `blithe·some`, `chromo·some` keep it.
let key_starts_vowel = key.starts_with(['a', 'e', 'i', 'o', 'u']);
if key_starts_vowel && pos > 0 && matches!(word[pos - 1], 'a' | 'e' | 'i' | 'o' | 'u') {
return false;
}
let e_idx = pos + key.chars().count() - 1;
// §10.10: a following `ed`/`er` strong groupsign claims the trailing `e` with
// the next letter (`stone·d`→⠫, `ton·er`→⠻, `on·er·ous`→⠻), so the silent `e`
// must not be swallowed into the contraction: `stoned`=st·o·n·ed,
// `toner`=t·o·n·er — not st·one·d / t·one·r.
if matches!(word.get(e_idx + 1), Some('d' | 'r')) {
return false;
}
let prons = self.provider.pronunciations(full);
super::pronunciation::aligner::trailing_letter_is_silent_or_merged(word, e_idx, &prons)
}
/// §10.7 `ever`: used where the letters `e·v·e·r` form the unstressed `-er` unit
/// (`fever`, `several`, `beverage`) — the trailing `er` carrying no stress — but
/// not a stressed `e·VER·sion`/`se·VER·ity`, nor across a preceding vowel digraph
/// (`belie·ver`'s `ie`). Restricted to `ever`; `father`/`mother`/`under` are
/// deferred (no test corpus, and `under` risks coincidental substrings like
/// `th·under`).
fn ever_shape_supports(&self, word: &[char], pos: usize, key: &str, full: &str) -> bool {
if key != "ever" {
return false;
}
// 국립국어원 회신(2026-09-12): §10.7.4 는 `ever` 의 첫 `e` 에 강세가 오고
// 그 앞에 `e` 나 `i` 가 오지 않을 때 어두 약자를 쓰도록 한다. 앞의 `ie`/`ee`
// 가 이 자리에서 걸러진다(`belie·ver`, `thie·very`, `McKe·ever`).
if pos > 0 && matches!(word[pos - 1], 'e' | 'i') {
return false;
}
let er_e_idx = pos + 2; // the `e` of the trailing `er` in `e·v·e·r`
let prons = self.provider.pronunciations(full);
if prons.is_empty() {
// 사전에 없어 강세를 확인할 수 없는 낱말은 `ever` 가 낱말 끝이거나 굴절
// 어미 `s` 하나만 남기고 끝날 때에만 그 `er` 가 강세 없는 어미가 되어
// §10.7 의 꼴을 이룬다(`cantilever`, `Clevers`). 뒤에 다른 글자가 더
// 붙으면 그 `ver` 가 강세를 받을 수 있어(`e·ver·sion`, `re·ver·ify`)
// 약자를 쓰지 않는다.
return match word.len() - (pos + 4) {
0 => true,
1 => word[pos + 4] == 's',
_ => false,
};
}
super::pronunciation::aligner::trailing_er_is_unstressed(word, er_e_idx, &prons)
}
/// True iff `chars` is a recorded CMUdict headword.
fn is_word(&self, chars: &[char]) -> bool {
!self
.provider
.pronunciations(&chars.iter().collect::<String>())
.is_empty()
}
/// §10.7 danger zone: decide a final-`e` contraction `key` at `pos` whose next
/// letter is the `r`/`d` of an -er/-ed suffix. The contraction is used only when
/// it is a morpheme exposed by word structure, not merely by sound.
fn danger_zone_use(&self, word: &[char], pos: usize, key: &str, end: usize) -> bool {
// The suffix must be a single trailing `r`/`d`; a longer tail (`some|rsault`,
// `some|rs`) means the letters do not sit on the -er/-ed boundary.
if end + 1 != word.len() {
return false;
}
// The base IS the contraction: `time`+r, `name`+d. There is no word
// `somer`/`somed`, so a bare `some`+suffix never carries the -some morpheme.
if word[..end].iter().copied().eq(key.chars()) {
return key != "some";
}
// If the base WITHOUT the contraction's final `e` is itself a word, the word
// is (that word)+ed/er and the `e` is the suffix vowel — not part of the
// -some/-name morpheme (`ransom`+ed, `blossom`+ed). `handsom` (from
// `handsome`+r) is not a word, so `handsomer` survives this guard.
if self.is_word(&word[..end - 1]) {
return false;
}
// Otherwise the contraction must be an exposed suffix of the base: a known
// word or a closed bound prefix precedes it, AND the base's pronunciation
// carries the contraction's sound. `one` is excluded from known-word
// recovery so `toner`/`sooner`/`crooner` keep the `er` groupsign (§10.7).
// A closed bound prefix (`mis`·time·d, `re`·name·d) is reliable evidence on
// its own — skip phonology, since the base (`mistime`, `rename`) is often
// absent from CMUdict. With the §10.10 path-priority tie-break the offered
// contraction is now actually selected over the overlapping `st`/`en`.
let left_str: String = word[..pos].iter().collect();
if bound_prefix_exposes(key, &left_str) {
return true;
}
// A known word before the contraction still needs phonology on the base
// (`hand`·some → `handsome` carries /S AH M/) to avoid a coincidence.
matches!(key, "some" | "time" | "name")
&& self.is_word(&word[..pos])
&& self.pronunciation_supports(&word[..end].iter().collect::<String>(), key)
}
/// §10.7 morphology recovery for CMUdict GAPS: when a word is absent from the
/// dictionary (so phonology can neither confirm nor deny) but its structure
/// exposes the contraction as a clean morpheme — `blithe·some`, `some·such`,
/// `tea·time`, `time·ously` — the sign is safe. Restricted to the
/// consonant-initial final-`e` keys whose `e` is reliably silent as a
/// suffix/compound unit (`one` is excluded: its vowel onset risks a
/// digraph/diphthong with a coincidental neighbour). A known word is left to
/// the phonology gates above; unknown words with no clean split spell out.
fn morphology_recovers(&self, word: &[char], pos: usize, key: &str, end: usize) -> bool {
// Consonant-initial final-`e` keys (silent-`e` compounds) plus selected
// vowel-initial `one` compounds (`stone·work`, `lone·some`) and `upon`, whose
// `here·upon` compound is a clean word-edge morpheme (`there·upon`/
// `where·upon` are already in CMUdict; `coupon` is, so it never reaches here).
if !matches!(
key,
"some" | "name" | "time" | "here" | "there" | "where" | "upon" | "one"
) || self.is_word(word)
{
return false;
}
// The contraction must sit at a WORD EDGE. A fragment on BOTH sides
// (`ga`|some|`ter` in `gasometer`, where the dict happens to list `ga`) is a
// coincidence, not a morpheme boundary — so exactly one side must be empty.
let after = &word[end..];
let after_ok = after.is_empty()
|| self.is_word(after)
|| is_safe_suffix(&after.iter().collect::<String>());
if key == "one" {
// §10.7.6: use `one` in compounds/derivatives (`stonework`,
// `demonetise`, `lonesomest`) when not preceded by `o`; a following
// component must be a real word/suffix (`after_ok`). Word-final `one`
// in an unknown word (`ZEROBASEONE`, `homezone`) is read as the /oʊn/
// unit under §10.12.7, except after `i`, whose `io` diphthong voices
// the `e` (`pensione`, cf. §10.7.6 `Hermione`, `pioneer`); known
// `-one` words such as `anemone` never reach this recovery.
(pos == 0 || !matches!(word[pos - 1], 'o' | 'i'))
&& !(pos == 0 && after.is_empty())
&& after_ok
} else if pos == 0 {
// Word-initial unit: `some`·such, `time`·ously, `name`·able, `where`·of.
// A bare key alone is a known word (handled by phonology), so a valid
// non-empty remainder is required.
!after.is_empty() && after_ok
} else if after.is_empty() {
// Word-final unit: `blithe`·some, `tea`·time, `your`·name. A base that
// is not itself a word still exposes the morpheme when it ends in the
// combining-form linking vowel `o` (`exo`·some, `azoto`·some,
// `lipo`·some) — the `o` closes the preceding element, so the final
// four letters cannot be a chance spill from it.
self.is_word(&word[..pos]) || (key == "some" && pos > 1 && word[pos - 1] == 'o')
} else if matches!(key, "some" | "time")
&& is_safe_suffix(&after.iter().collect::<String>())
{
self.is_word(&word[..pos])
} else {
false
}
}
}
impl ContractionRule for InitialContractionPronunciationRule {
fn try_match(&self, word: &[char], pos: usize) -> Option<ContractionMatch> {
let full: String = word.iter().collect();
let mut best: Option<(usize, [u8; 2])> = None;
for (key, &cells) in PRON_CONTRACTIONS.entries() {
let klen = key.chars().count();
let end = pos + klen;
if end > word.len()
|| !key.chars().zip(&word[pos..end]).all(|(k, w)| k == *w)
|| best.is_some_and(|(bl, _)| klen <= bl)
{
continue;
}
if *key == "where"
&& (word.get(end) == Some(&'\'')
|| word.get(end..).is_some_and(|tail| {
tail.starts_with(&['e', 'v', 'e', 'r'])
|| tail.starts_with(&['v', 'e', 'r'])
}))
{
continue;
}
if *key == "time"
&& pos > 0
&& word[pos - 1] == 'n'
&& !matches!(word.get(..pos), Some(['u', 'n']))
{
continue;
}
if *key == "day"
&& end != word.len()
&& word
.get(end)
.is_some_and(|c| matches!(c, 'a' | 'e' | 'i' | 'o' | 'u'))
{
continue;
}
if *key == "ever" && pos > 0 && word[pos - 1] == 'i' {
continue;
}
// §10.7.6 excludes only a preceding `o` (`Boone`, `Rooney`); after
// another vowel the pronunciation gates decide (`someone`, `anyone`).
if *key == "one" && pos > 0 && word[pos - 1] == 'o' {
continue;
}
if *key == "one" && word.get(end..) == Some(&['s', 's']) {
continue;
}
if *key == "one" && word.get(end..) == Some(&['g', 'a', 'l']) {
continue;
}
// The letters keep their sound here when EITHER the whole word's
// pronunciation contains the contraction's run (`part`/`work`), OR — for
// a silent-`e` contraction standing word-finally — the trailing `e` is
// proven silent (`cone`, `phone`, `adhere`). Note: these §10.7
// initial-letter contractions are whole-unit signs (priority 55), exempt
// from §10.11 compound-seam bridging — `up·on`/`there·up·on` legitimately
// span their own etymological seams.
// §10.7 danger zone: a final-`e` contraction immediately followed by
// `r`/`d` (the -er/-ed suffix) cannot be judged by phonology alone —
// `time+r` (USE) shares the /… ER/ / /… D/ shape with `some+rsault`,
// interior `tone`+`r`, and `ransom+ed`. Require an exposed morpheme.
let danger =
key.ends_with('e') && word.get(end).is_some_and(|c| matches!(c, 'r' | 'd'));
let accept = if (*key == "had" && pos == 0 && !matches!(word.get(3), Some('e' | 'r')))
|| (*key == "day"
&& (end == word.len()
|| word
.get(end)
.is_some_and(|c| !matches!(c, 'a' | 'e' | 'i' | 'o' | 'u'))))
|| (*key == "under" && pos > 0 && word[pos - 1] == 'w')
|| (*key == "ever" && word.get(pos..pos + 5) == Some(&['e', 'v', 'e', 'r', 'y']))
|| (*key == "ever" && ever_morpheme_exposed(word, pos, end))
|| (*key == "one"
&& (word.get(pos..pos + 5) == Some(&['o', 'n', 'e', 's', 't'])
|| word.get(end..end + 4) == Some(&['s', 'o', 'm', 'e'])
|| word.get(end..) == Some(&['t', 'i', 's', 'e'])
|| word.get(end) == Some(&'y')))
|| (*key == "time"
&& pos > 0
&& matches!(word.get(..pos), Some(['u', 'n']))
&& word.get(end..) == Some(&['l', 'y']))
{
true
} else if danger {
self.danger_zone_use(word, pos, key, end)
} else {
self.pronunciation_supports(&full, key)
|| self.silent_final_e_supports(word, pos, key, &full)
|| self.ever_shape_supports(word, pos, key, &full)
|| self.morphology_recovers(word, pos, key, end)
};
if accept {
best = Some((klen, cells));
}
}
best.map(|(klen, cells)| ContractionMatch {
cells: cells.to_vec(),
consumed: klen,
// Same band as the spelling-only §10.7 set so longest-match drives
// the choice; below §10.4 strong groupsigns (60) on equal length.
priority: 55,
// §10.10.1: pronunciation-validated (`part` in `apartheid`), so the
// cell-minimiser must not split it with a cheaper generic contraction.
protect_span: true,
})
}
}
/// Closed set of bound prefixes that expose a final-`e` contraction as a morpheme
/// in an -er/-ed word (`mis·time·d`, `re·name·d`). Kept deliberately small so the
/// danger-zone override never over-accepts.
fn bound_prefix_exposes(key: &str, left: &str) -> bool {
matches!((key, left), ("time", "mis") | ("name", "re"))
}
/// Closed set of safe derivational/inflectional suffixes that may follow a
/// recovered final-`e` contraction (`time·ously`, `lone·some·st`, `some`·`s`).
fn is_safe_suffix(s: &str) -> bool {
matches!(
s,
"s" | "es" | "st" | "ly" | "ness" | "ous" | "ously" | "able" | "ment"
)
}
/// §10.7 `ever` in transparent bound forms not reliably covered by CMUdict.
fn ever_morpheme_exposed(word: &[char], pos: usize, end: usize) -> bool {
matches!(word.get(end..), Some([] | ['a', 't', 'e']))
&& matches!(word.get(..pos), Some(['a', 's', 's'] | [.., 's', 'o']))
}
/// Whether `needle` occurs as a contiguous run inside `haystack`, comparing only
/// ARPABET base symbols (lexical stress shifts in context: `one` is W AH1 N alone
/// but W AH0 N in `anyone`).
fn contains_contiguous(haystack: &[Phoneme], needle: &[Phoneme]) -> bool {
if needle.is_empty() || needle.len() > haystack.len() {
return false;
}
haystack
.windows(needle.len())
.any(|w| w.iter().zip(needle).all(|(a, b)| a.base == b.base))
}
#[cfg(test)]
mod tests {
use super::super::pronunciation::cmudict::CmuDictProvider;
use super::*;
fn rule() -> InitialContractionPronunciationRule {
InitialContractionPronunciationRule::new(Box::new(CmuDictProvider::new()))
}
fn try_at(word: &str, pos: usize) -> Option<(Vec<u8>, usize)> {
let chars: Vec<char> = word.chars().collect();
rule().try_match(&chars, pos).map(|m| (m.cells, m.consumed))
}
/// Pronunciation MATCHES → contraction used (the letters keep their sound).
#[rstest::rstest]
// partake = P AA R T EY K — `part` (P AA R T) at pos 0.
#[case::partake("partake", 0, Some((vec![decode_unicode('⠐'), decode_unicode('⠏')], 4)))]
// apartheid = AH P AA R T … — `part` (P AA R T) at pos 1.
#[case::apartheid("apartheid", 1, Some((vec![decode_unicode('⠐'), decode_unicode('⠏')], 4)))]
// network = N EH T W ER K — `work` (W ER K) at pos 3.
#[case::network("network", 3, Some((vec![decode_unicode('⠐'), decode_unicode('⠺')], 4)))]
fn applies_when_pronunciation_matches(
#[case] word: &str,
#[case] pos: usize,
#[case] expected: Option<(Vec<u8>, usize)>,
) {
assert_eq!(try_at(word, pos), expected);
}
/// §10.7 `ever`: the unstressed `-er` unit contracts (`fever` /…V ER0/,
/// `several` /…V R AH0…/); a stressed `e·VER` (`eversion`) or a full vowel at the
/// `e` (`severity`) does not.
#[rstest::rstest]
#[case::fever("fever", 1, Some((vec![decode_unicode('⠐'), decode_unicode('⠑')], 4)))]
#[case::several("several", 1, Some((vec![decode_unicode('⠐'), decode_unicode('⠑')], 4)))]
#[case::beverage("beverage", 1, Some((vec![decode_unicode('⠐'), decode_unicode('⠑')], 4)))]
#[case::whosesoever("whosesoever", 7, Some((vec![decode_unicode('⠐'), decode_unicode('⠑')], 4)))]
#[case::asseverate("asseverate", 3, Some((vec![decode_unicode('⠐'), decode_unicode('⠑')], 4)))]
#[case::eversion("eversion", 0, None)] // e·VER stressed → use `er`
#[case::severity("severity", 1, None)] // se·VER·ity — e is a full vowel
#[case::believer("believer", 4, None)] // §10.7.4 — preceding `i`
#[case::mckeever("mckeever", 4, None)] // §10.7.4 — preceding `e`
fn applies_ever_when_unstressed(
#[case] word: &str,
#[case] pos: usize,
#[case] expected: Option<(Vec<u8>, usize)>,
) {
assert_eq!(try_at(word, pos), expected);
}
/// The letters split across the contraction's sound → safely spelled out.
/// `component` voices a schwa at the `e` (`…N AH0 N T`), so the `one` unit is
/// split; `acknowledge`'s `know` is /N AA…/, not /N OW/.
#[rstest::rstest]
#[case::component_not_one("component", 4)] // K AH M P OW N AH0 N T — e voices schwa
#[case::acknowledge_not_know("acknowledge", 2)] // AE K N AA L … — `know` is N OW
fn spells_out_when_pronunciation_differs(#[case] word: &str, #[case] pos: usize) {
assert_eq!(try_at(word, pos), None);
}
/// Silent word-final `e` → the e-ending contraction is used even though the
/// in-word vowel differs from the standalone sound (`cone` /…N/ ≠ `one` /wʌn/).
#[rstest::rstest]
#[case::cone("cone", 1, decode_unicode('⠕'), 3)] // c·one — word-final
#[case::done("done", 1, decode_unicode('⠕'), 3)]
#[case::phone("phone", 2, decode_unicode('⠕'), 3)]
#[case::everyone("everyone", 5, decode_unicode('⠕'), 3)] // y precedes — not a digraph
#[case::adhere("adhere", 2, decode_unicode('⠓'), 4)] // ad·here
#[case::atonement("atonement", 2, decode_unicode('⠕'), 3)] // medial: at·one·ment
#[case::dishonesty("dishonesty", 4, decode_unicode('⠕'), 3)] // dis·h·one·sty
#[case::honey("honey", 1, decode_unicode('⠕'), 3)] // medial: h·one·y (ey merge)
#[case::money("money", 1, decode_unicode('⠕'), 3)] // m·one·y — identical to honey
fn applies_silent_final_e(
#[case] word: &str,
#[case] pos: usize,
#[case] letter: u8,
#[case] consumed: usize,
) {
assert_eq!(
try_at(word, pos),
Some((vec![decode_unicode('⠐'), letter], consumed))
);
}
/// The silent-`e` path stays conservative: a SOUNDED final `e` (`krone` /…ə/,
/// `anemone` /…i/), a preceding vowel digraph (`b[oo]ne`), or a medial occurrence
/// (`money`) all spell out rather than risk a wrong contraction.
#[rstest::rstest]
#[case::krone("krone", 2)] // K R OW N AH0 — sounded final e
#[case::anemone("anemone", 4)] // … N IY0 — sounded final e
#[case::boone("boone", 2)] // b·oo·ne — o starts inside the `oo` digraph
#[case::abalone("abalone", 4)] // … OW N IY0 — sounded final e
#[case::colonel("colonel", 3)] // irregular K ER1 N AH0 L — e voices schwa
#[case::stoned("stoned", 2)] // st·o·n·ed — `ed` groupsign claims e-d
#[case::toner("toner", 1)] // t·o·n·er — `er` groupsign claims e-r
fn silent_final_e_stays_conservative(#[case] word: &str, #[case] pos: usize) {
assert_eq!(try_at(word, pos), None);
}
/// The contiguous-run helper compares ARPABET base only (stress-insensitive)
/// and requires a true contiguous run.
#[test]
fn contiguous_run_compares_base_only() {
use super::super::pronunciation::parse_phoneme;
let hay: Vec<Phoneme> = "S AH0 M W AH1 N"
.split_whitespace()
.map(parse_phoneme)
.collect();
// `W AH N` matches despite stress differing (AH1 needle vs AH1 hay here).
let need: Vec<Phoneme> = "W AH0 N".split_whitespace().map(parse_phoneme).collect();
assert!(contains_contiguous(&hay, &need));
// `M AH N` is present as letters but not as a contiguous phoneme run.
let nope: Vec<Phoneme> = "M AH1 N".split_whitespace().map(parse_phoneme).collect();
assert!(!contains_contiguous(&hay, &nope));
}
/// §10.7 danger zone (a final-`e` contraction immediately followed by `r`/`d`).
/// The sign is used only when it is an EXPOSED morpheme — the whole base
/// (`time`+r, `name`+d), or a known word / bound prefix + key (`hand`·some·r,
/// `re`·name·d) — never a coincidental run (`somer`sault), an interior substring
/// (`t`·one within `tone`+r), or the `-ed`/`-er` suffix vowel (`ransom`+ed,
/// `blossom`+ed). `one` is excluded from known-word recovery.
#[rstest::rstest]
#[case::timer("timer", 0, true)] // time + r
#[case::named("named", 0, true)] // name + d
#[case::renamed("renamed", 2, true)] // re + name + d (bound prefix)
#[case::mistimed("mistimed", 3, true)] // mis + time + d (bound prefix)
#[case::handsomer("handsomer", 4, true)] // hand + some + r
#[case::toner("toner", 1, false)] // tone interior; keep `er`
#[case::somersault("somersault", 0, false)] // somer… not a morpheme
#[case::somerset("somerset", 0, false)]
#[case::somers("somers", 0, false)] // som + er + s (tail not a single r)
#[case::ransomed("ransomed", 3, false)] // ransom + ed
#[case::blossomed("blossomed", 4, false)] // blossom + ed
fn danger_zone_er_ed(#[case] word: &str, #[case] pos: usize, #[case] use_it: bool) {
assert_eq!(try_at(word, pos).is_some(), use_it, "danger zone: {word}");
}
/// §10.7 morphology recovery for CMUdict GAPS: an unknown word whose structure
/// exposes the contraction at a WORD EDGE (`some`·such, `tea`·time, `blithe`·some,
/// `time`·ously, `name`·able) uses the sign; a fragment on both sides
/// (`ga`·some·`ter`) is a coincidence and spells out.
#[rstest::rstest]
#[case::somesuch("somesuch", 0, true)] // some + such
#[case::teatime("teatime", 3, true)] // tea + time
#[case::blithesome("blithesome", 6, true)] // blithe + some
#[case::nameable("nameable", 0, true)] // name + able
#[case::hereupon("hereupon", 4, true)] // here + upon (word-edge, dict gap)
#[case::timeously("timeously", 0, true)] // time + ously
#[case::gasometer("gasometer", 2, false)] // ga | some | ter — coincidence
fn morphology_recovery_for_dict_gaps(
#[case] word: &str,
#[case] pos: usize,
#[case] use_it: bool,
) {
assert_eq!(try_at(word, pos).is_some(), use_it, "recovery: {word}");
}
#[test]
fn silent_final_e_support_rejects_vowel_bridge_and_ed_er_claims() {
let rule = rule();
let boone: Vec<char> = "boone".chars().collect();
assert!(!rule.silent_final_e_supports(&boone, 2, "one", "boone"));
let stoned: Vec<char> = "stoned".chars().collect();
assert!(!rule.silent_final_e_supports(&stoned, 2, "one", "stoned"));
}
#[test]
fn morphology_recovery_direct_paths_for_word_edges() {
let rule = rule();
let somesuch: Vec<char> = "somesuch".chars().collect();
assert!(rule.morphology_recovers(&somesuch, 0, "some", 4));
let teatime: Vec<char> = "teatime".chars().collect();
assert!(rule.morphology_recovers(&teatime, 3, "time", 7));
let nameable: Vec<char> = "nameable".chars().collect();
assert!(rule.morphology_recovers(&nameable, 0, "name", 4));
let blithesome: Vec<char> = "blithesome".chars().collect();
assert!(rule.morphology_recovers(&blithesome, 6, "some", 10));
let coincidental_middle: Vec<char> = "gasometer".chars().collect();
assert!(!rule.morphology_recovers(&coincidental_middle, 2, "some", 6));
}
#[test]
fn morphology_recovery_runtime_initial_unit_checks_suffix() {
let rule = rule();
let word: Vec<char> = std::hint::black_box("whereof").chars().collect();
assert!(rule.morphology_recovers(&word, 0, "where", 5));
}
#[test]
fn morphology_recovery_directly_covers_empty_after_word_final_unit() {
let rule = rule();
let teatime: Vec<char> = "teatime".chars().collect();
assert!(rule.morphology_recovers(&teatime, 3, "time", 7));
}
#[test]
fn morphology_recovery_directly_covers_initial_and_one_paths() {
let rule = rule();
let timeously: Vec<char> = "timeously".chars().collect();
assert!(rule.morphology_recovers(&timeously, 0, "time", 4));
let stonework: Vec<char> = "stonework".chars().collect();
assert!(rule.morphology_recovers(&stonework, 2, "one", 5));
let alone: Vec<char> = "alone".chars().collect();
assert!(!rule.morphology_recovers(&alone, 2, "one", 5));
}
#[test]
fn morphology_recovery_directly_rejects_known_word_and_accepts_initial_suffix() {
let rule = rule();
let some: Vec<char> = "some".chars().collect();
assert!(!rule.morphology_recovers(&some, 0, "some", 4));
let somewise: Vec<char> = "somewise".chars().collect();
assert!(rule.morphology_recovers(&somewise, 0, "some", 4));
}
#[rstest::rstest]
#[case::initial_some_suffix("somesuch", 0, "some", 4, true)]
#[case::one_compound_suffix("stonework", 2, "one", 5, true)]
#[case::final_time_component("teatime", 3, "time", 7, true)]
fn morphology_recovery_runtime_word_edge_paths(
#[case] word: &str,
#[case] pos: usize,
#[case] key: &str,
#[case] end: usize,
#[case] expected: bool,
) {
let rule = rule();
let chars: Vec<char> = std::hint::black_box(word).chars().collect();
assert_eq!(
rule.morphology_recovers(&chars, pos, std::hint::black_box(key), end),
expected
);
}
#[test]
fn contains_contiguous_rejects_empty_or_oversized_needle() {
// An empty needle — like any needle longer than the haystack — is not
// contained.
assert!(!contains_contiguous(&[], &[]));
}
}
#[cfg(test)]
mod ever_shape_coverage {
/// §10.7: with no dictionary entry the `ever` sign is decided by shape —
/// word-final, or with a single inflectional `s` left.
#[rstest::rstest]
#[case::word_final("그는 cantilever 를", "⠐⠑")]
#[case::inflected_s("그는 Clevers 를", "⠐⠑")]
#[case::longer_tail("그는 Cleverse 를", "⠑⠧⠻")]
fn dictionaryless_ever_follows_the_word_edge(#[case] input: &str, #[case] expected: &str) {
let encoded = crate::encode_to_unicode(input).unwrap();
assert!(
encoded.contains(expected),
"expected {expected} in {encoded}"
);
}
}