inkhaven 1.8.6

Inkhaven — TUI literary work editor for Typst books
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
//! Verse scansion (1.8.2) — read the metre of a line of poetry.
//!
//! Scanning a line means marking each syllable stressed or unstressed and then
//! recognising the repeating *foot* — iamb, trochee, dactyl — that the pattern
//! spells out. This composes two primitives already in the phonology layer:
//! [`syllabify`](crate::conlang::phonology::syllable::syllabify) splits a word
//! into syllables, and stress is resolved per word through a three-link chain
//! so the tool works for languages with lexical (unpredictable) stress, Russian
//! chief among them:
//!
//! 1. an **explicit mark** in the text — a combining acute over the stressed
//!    vowel (`лу́`), the way Russian verse is annotated;
//! 2. the lexicon's **`stress`** field for that word;
//! 3. the language's **stress rule** (initial / penult / …) as a last resort.
//!
//! A monosyllable with no explicit or lexical stress is left *flexible* — it may
//! promote or demote to fit the metre, exactly as a monosyllabic function word
//! does in real scansion. Pure and deterministic.

use crate::conlang::phonology::{stress_eval, syllable};
use crate::conlang::types::{Phonology, PhonemeKind};
use crate::language_entry::DictionaryEntry;

/// Combining acute accent — the standard stress mark over a vowel.
const ACUTE: char = '\u{0301}';

/// Whether a syllable bears the beat.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Beat {
    /// Carries primary stress.
    Stressed,
    /// Unstressed.
    Unstressed,
    /// A monosyllable (or a word of unknown stress) that may take either value
    /// to fit the metre — promotion/demotion.
    Flexible,
}

impl Beat {
    /// The scansion glyph: `/` stressed, `×` unstressed, `·` flexible.
    pub fn glyph(self) -> char {
        match self {
            Beat::Stressed => '/',
            Beat::Unstressed => '×',
            Beat::Flexible => '·',
        }
    }
}

/// One syllable of a scanned word.
#[derive(Debug, Clone, PartialEq)]
pub struct ScannedSyllable {
    pub text: String,
    pub beat: Beat,
}

/// One word of a scanned line, split into syllables.
#[derive(Debug, Clone, PartialEq)]
pub struct ScannedWord {
    pub surface: String,
    pub syllables: Vec<ScannedSyllable>,
}

/// The recognised metre of a line.
#[derive(Debug, Clone, PartialEq)]
pub struct Meter {
    /// Foot name: `iamb`, `trochee`, `anapest`, `dactyl`, `amphibrach`.
    pub foot: &'static str,
    /// How many feet (→ dimeter, trimeter, tetrameter, …).
    pub feet: usize,
    /// The full name, e.g. `iambic tetrameter`.
    pub name: String,
    /// Fraction of fixed (non-flexible) syllables that conform, 0..1.
    pub conformance: f64,
}

/// A scanned line: its words, the flat beat pattern, and the best metre guess.
#[derive(Debug, Clone, PartialEq)]
pub struct ScannedLine {
    pub words: Vec<ScannedWord>,
    pub meter: Option<Meter>,
}

impl ScannedLine {
    /// The flat sequence of beats across the whole line.
    pub fn beats(&self) -> Vec<Beat> {
        self.words.iter().flat_map(|w| w.syllables.iter().map(|s| s.beat)).collect()
    }

}

/// The metrical foot templates we recognise, as stress patterns (true =
/// stressed). Binary feet first so they win ties over the rarer ternary ones.
const FEET: &[(&str, &[bool])] = &[
    ("iamb", &[false, true]),
    ("trochee", &[true, false]),
    ("anapest", &[false, false, true]),
    ("dactyl", &[true, false, false]),
    ("amphibrach", &[false, true, false]),
];

/// Scan one line against a language.
pub fn scan_line(phon: &Phonology, entries: &[DictionaryEntry], line: &str) -> ScannedLine {
    let vowels = vowel_graphemes(phon);
    let mut words = Vec::new();
    for raw in line.split_whitespace() {
        let word = trim_punct(raw);
        if word.chars().all(|c| !c.is_alphabetic()) {
            continue;
        }
        words.push(scan_word(phon, entries, &vowels, word));
    }
    let meter = detect_meter(&flatten(&words));
    ScannedLine { words, meter }
}

/// Scan every non-empty line of a (possibly multi-line) text.
pub fn scan_text(phon: &Phonology, entries: &[DictionaryEntry], text: &str) -> Vec<ScannedLine> {
    text.lines()
        .filter(|l| !l.trim().is_empty())
        .map(|l| scan_line(phon, entries, l))
        .collect()
}

/// The dominant metre across a set of scanned lines — the one most lines share.
pub fn dominant_meter(lines: &[ScannedLine]) -> Option<Meter> {
    use std::collections::HashMap;
    let mut tally: HashMap<(&str, usize), (usize, f64)> = HashMap::new();
    for l in lines {
        if let Some(m) = &l.meter {
            let e = tally.entry((m.foot, m.feet)).or_insert((0, 0.0));
            e.0 += 1;
            e.1 += m.conformance;
        }
    }
    tally.into_iter().max_by_key(|(_, (count, _))| *count).map(|((foot, feet), (count, sumc))| Meter {
        foot,
        feet,
        name: format!("{} {}", adjective(foot), length_name(feet)),
        conformance: sumc / count as f64,
    })
}

fn flatten(words: &[ScannedWord]) -> Vec<Beat> {
    words.iter().flat_map(|w| w.syllables.iter().map(|s| s.beat)).collect()
}

/// Scan a single word: syllabify it, resolve which syllable is stressed, and
/// assign a beat to each.
fn scan_word(
    phon: &Phonology,
    entries: &[DictionaryEntry],
    vowels: &[String],
    word: &str,
) -> ScannedWord {
    // The explicit mark, if any, and the word with the mark removed.
    let (clean, marked_syllable) = strip_stress_mark(word, vowels);

    let seq = phon.segment(&clean);
    let sylls = syllable::syllabify(phon, &seq);
    let n = sylls.len().max(1);

    // Resolve the stressed syllable index through the chain.
    let (stressed, lexical) = if let Some(i) = marked_syllable {
        (Some(i.min(n - 1)), true)
    } else if let Some(i) = lexicon_stress(entries, &clean) {
        (Some((i.saturating_sub(1)).min(n - 1)), true)
    } else if let Some(rule) = &phon.stress {
        (stress_eval::primary_stress(rule, &sylls), false)
    } else {
        (None, false)
    };

    let render = |s: &syllable::Syllable| -> String {
        s.onset
            .iter()
            .chain(&s.nucleus)
            .chain(&s.coda)
            .map(|ipa| phon.phoneme(ipa).map(|p| p.grapheme().to_string()).unwrap_or_else(|| ipa.clone()))
            .collect()
    };

    let mono = sylls.len() <= 1;
    let syllables = sylls
        .iter()
        .enumerate()
        .map(|(i, s)| {
            let beat = if mono {
                // A monosyllable is only firmly stressed when we were told so
                // (explicit mark / lexicon); otherwise it flexes to the metre.
                if lexical && stressed == Some(i) { Beat::Stressed } else { Beat::Flexible }
            } else {
                match stressed {
                    Some(k) if k == i => Beat::Stressed,
                    Some(_) => Beat::Unstressed,
                    None => Beat::Flexible,
                }
            };
            ScannedSyllable { text: render(s), beat }
        })
        .collect();

    ScannedWord { surface: word.to_string(), syllables }
}

/// Find `word`'s lexical stress in the dictionary (case-insensitive on the
/// citation form).
fn lexicon_stress(entries: &[DictionaryEntry], word: &str) -> Option<usize> {
    entries
        .iter()
        .find(|e| e.word.eq_ignore_ascii_case(word) || unicode_eq(&e.word, word))
        .and_then(|e| e.stress)
        .filter(|&s| s > 0)
}

fn unicode_eq(a: &str, b: &str) -> bool {
    a.to_lowercase() == b.to_lowercase()
}

/// Remove combining-acute marks from a word; return the cleaned word and, if a
/// mark was present, the 0-based index of the syllable it fell on (counted as
/// the ordinal of the marked vowel among the word's vowels).
fn strip_stress_mark(word: &str, vowels: &[String]) -> (String, Option<usize>) {
    if !word.contains(ACUTE) {
        return (word.to_string(), None);
    }
    let mut clean = String::with_capacity(word.len());
    let mut vowel_ord = 0usize; // vowels seen so far (before the current char)
    let mut marked: Option<usize> = None;
    for ch in word.chars() {
        if ch == ACUTE {
            // The acute follows its vowel: that vowel's ordinal is vowel_ord-1.
            if marked.is_none() && vowel_ord > 0 {
                marked = Some(vowel_ord - 1);
            }
            continue;
        }
        clean.push(ch);
        if is_vowel_char(ch, vowels) {
            vowel_ord += 1;
        }
    }
    (clean, marked)
}

/// A char is a vowel if it is one of the language's vowel graphemes.
fn is_vowel_char(ch: char, vowels: &[String]) -> bool {
    let s = ch.to_lowercase().to_string();
    vowels.iter().any(|v| v.to_lowercase() == s)
}

/// The language's vowel graphemes (romanization, falling back to the IPA).
fn vowel_graphemes(phon: &Phonology) -> Vec<String> {
    phon.phonemes
        .iter()
        .filter(|p| p.kind == PhonemeKind::Vowel)
        .map(|p| p.grapheme().to_string())
        .collect()
}

fn trim_punct(s: &str) -> &str {
    s.trim_matches(|c: char| !c.is_alphabetic() && c != ACUTE && c != '\u{0300}')
}

/// Detect the best-fitting metre for a beat sequence. Each foot template is
/// tiled across the line; flexible beats match either value, fixed beats must
/// agree. The template with the highest conformance (over the fixed beats) wins,
/// requiring a clear majority to name a metre at all.
fn detect_meter(beats: &[Beat]) -> Option<Meter> {
    let n = beats.len();
    if n < 2 {
        return None;
    }
    let mut best: Option<Meter> = None;
    for (foot, pattern) in FEET {
        let f = pattern.len();
        if n < f {
            continue;
        }
        // Floor division: a rising line with a feminine (extra unstressed)
        // ending — 9 syllables of iambic verse — is tetrameter with a
        // hypermetrical syllable, not pentameter. Feminine endings are far
        // commoner than catalexis, so flooring is the better default.
        let feet = (n / f).max(1);
        let mut fixed = 0usize;
        let mut hits = 0usize;
        for (i, b) in beats.iter().enumerate() {
            let expect_stressed = pattern[i % f];
            match b {
                Beat::Flexible => {}
                Beat::Stressed => {
                    fixed += 1;
                    if expect_stressed {
                        hits += 1;
                    }
                }
                Beat::Unstressed => {
                    fixed += 1;
                    if !expect_stressed {
                        hits += 1;
                    }
                }
            }
        }
        if fixed == 0 {
            continue;
        }
        let conformance = hits as f64 / fixed as f64;
        let cand = Meter {
            foot,
            feet,
            name: format!("{} {}", adjective(foot), length_name(feet)),
            conformance,
        };
        if best.as_ref().is_none_or(|b| cand.conformance > b.conformance) {
            best = Some(cand);
        }
    }
    // Only name a metre when the fit is convincing.
    best.filter(|m| m.conformance >= 0.7)
}

/// Adjective form of a foot name (`iamb` → `iambic`).
fn adjective(foot: &str) -> &'static str {
    match foot {
        "iamb" => "iambic",
        "trochee" => "trochaic",
        "anapest" => "anapestic",
        "dactyl" => "dactylic",
        "amphibrach" => "amphibrachic",
        _ => "metrical",
    }
}

/// Line-length name from the foot count.
fn length_name(feet: usize) -> String {
    match feet {
        1 => "monometer".into(),
        2 => "dimeter".into(),
        3 => "trimeter".into(),
        4 => "tetrameter".into(),
        5 => "pentameter".into(),
        6 => "hexameter".into(),
        7 => "heptameter".into(),
        8 => "octameter".into(),
        n => format!("{n}-meter"),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::conlang::types::{Phoneme, PhonemeKind, StressRule};
    use crate::conlang::types::stress::StressPlacement;

    fn ph(ipa: &str, roman: &str, kind: PhonemeKind) -> Phoneme {
        Phoneme { ipa: ipa.into(), romanize: Some(roman.into()), kind, sonority: None }
    }

    // A tiny Russian-ish phonology: Cyrillic vowels + a few consonants, no
    // stress rule (so the chain must use marks / lexicon).
    fn ru() -> Phonology {
        Phonology {
            phonemes: vec![
                ph("a", "а", PhonemeKind::Vowel),
                ph("o", "о", PhonemeKind::Vowel),
                ph("u", "у", PhonemeKind::Vowel),
                ph("i", "и", PhonemeKind::Vowel),
                ph("e", "е", PhonemeKind::Vowel),
                ph("ɨ", "ы", PhonemeKind::Vowel),
                ph("d", "д", PhonemeKind::Consonant),
                ph("m", "м", PhonemeKind::Consonant),
                ph("k", "к", PhonemeKind::Consonant),
                ph("n", "н", PhonemeKind::Consonant),
                ph("r", "р", PhonemeKind::Consonant),
                ph("l", "л", PhonemeKind::Consonant),
                ph("t", "т", PhonemeKind::Consonant),
                ph("s", "с", PhonemeKind::Consonant),
                ph("v", "в", PhonemeKind::Consonant),
                ph("b", "б", PhonemeKind::Consonant),
                ph("g", "г", PhonemeKind::Consonant),
                ph("j", "й", PhonemeKind::Consonant),
                ph("z", "з", PhonemeKind::Consonant),
                ph("p", "п", PhonemeKind::Consonant),
                ph("x", "х", PhonemeKind::Consonant),
                ph("f", "ф", PhonemeKind::Consonant),
                ph("ʃ", "ш", PhonemeKind::Consonant),
                ph("ʒ", "ж", PhonemeKind::Consonant),
            ],
            ..Default::default()
        }
    }

    #[test]
    fn explicit_mark_sets_the_stressed_syllable() {
        let p = ru();
        // окно́ — stress on the 2nd (final) syllable.
        let w = scan_word(&p, &[], &vowel_graphemes(&p), "окно\u{0301}");
        assert_eq!(w.syllables.len(), 2);
        assert_eq!(w.syllables[0].beat, Beat::Unstressed);
        assert_eq!(w.syllables[1].beat, Beat::Stressed);
    }

    #[test]
    fn lexicon_stress_is_used_when_text_is_unmarked() {
        let p = ru();
        let entries = vec![DictionaryEntry {
            word: "окно".into(),
            stress: Some(2),
            ..Default::default()
        }];
        let w = scan_word(&p, &entries, &vowel_graphemes(&p), "окно");
        assert_eq!(w.syllables[1].beat, Beat::Stressed);
        assert_eq!(w.syllables[0].beat, Beat::Unstressed);
    }

    #[test]
    fn stress_rule_is_the_last_resort() {
        let mut p = ru();
        p.stress = Some(StressRule { primary: StressPlacement::Initial });
        // молоко — three syllables; the Initial rule stresses the first.
        let w = scan_word(&p, &[], &vowel_graphemes(&p), "молоко");
        assert_eq!(w.syllables.len(), 3);
        assert_eq!(w.syllables[0].beat, Beat::Stressed);
        assert_eq!(w.syllables[1].beat, Beat::Unstressed);
        assert_eq!(w.syllables[2].beat, Beat::Unstressed);
    }

    #[test]
    fn unmarked_monosyllable_is_flexible() {
        let p = ru();
        let w = scan_word(&p, &[], &vowel_graphemes(&p), "дуб");
        assert_eq!(w.syllables.len(), 1);
        assert_eq!(w.syllables[0].beat, Beat::Flexible);
    }

    #[test]
    fn beats_render_the_glyph_row() {
        let p = ru();
        // окно́ дуб → [× /] [·]
        let line = scan_line(&p, &[], "окно\u{0301} дуб");
        let glyphs: String = line.beats().iter().map(|b| b.glyph()).collect();
        assert_eq!(glyphs, "×/·");
        assert_eq!(line.beats().len(), 3);
    }

    #[test]
    fn detects_iambic_tetrameter() {
        // × / × / × / × /  — four iambs.
        let beats = vec![
            Beat::Unstressed, Beat::Stressed, Beat::Unstressed, Beat::Stressed,
            Beat::Unstressed, Beat::Stressed, Beat::Unstressed, Beat::Stressed,
        ];
        let m = detect_meter(&beats).expect("a metre");
        assert_eq!(m.foot, "iamb");
        assert_eq!(m.feet, 4);
        assert_eq!(m.name, "iambic tetrameter");
        assert!((m.conformance - 1.0).abs() < 1e-9);
    }

    #[test]
    fn flexible_monosyllables_promote_to_fit() {
        // A line of content polysyllables pinning iambic feet, with a flexible
        // monosyllable that must promote — still reads as iambic.
        let beats = vec![
            Beat::Flexible, Beat::Stressed, Beat::Unstressed, Beat::Stressed,
        ];
        let m = detect_meter(&beats).expect("a metre");
        assert_eq!(m.foot, "iamb");
        assert!((m.conformance - 1.0).abs() < 1e-9); // only fixed beats scored
    }

    #[test]
    fn free_verse_names_no_metre() {
        // An all-stressed (spondaic) run fits none of the common feet above the
        // 0.7 threshold — no regular metre is claimed.
        let beats = vec![
            Beat::Stressed, Beat::Stressed, Beat::Stressed, Beat::Stressed, Beat::Stressed,
        ];
        assert!(detect_meter(&beats).map(|m| m.conformance < 0.7).unwrap_or(true));
    }
}