lmm 0.2.6

A language agnostic framework for emulating reality.
Documentation
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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
// Copyright 2026 Mahmoud Harmouch.
//
// Licensed under the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! # Symbolic Text Prediction
//!
//! This module implements [`TextPredictor`], a symbolic continuation engine that extends a
//! text prompt by fitting tone and rhythm trajectories then selecting words token-by-token.
//!
//! ## Algorithm
//!
//! 1. **Tokenise** - split input into a sliding window of tokens.
//! 2. **Tone trajectory** - fit a symbolic expression to `(token_position → mean_byte_value)`.
//! 3. **Rhythm trajectory** - fit a second expression to `(token_position → word_length)`.
//! 4. **Predict** - for each step, determine the expected POS; select the word from a curated
//!    pool or lexicon that best matches the predicted tone and length.
//!
//! ## Optimisation: Compile-time PHF Sets
//!
//! The four lexical pools (nouns, adjectives, verbs, adverbs) used for POS detection are
//! stored as compile-time [`phf::Set`]s, replacing the previous O(n) `slice.contains(&w)`
//! linear scans in `detect_pos` with **O(1) perfect-hash lookups**.

use crate::discovery::SymbolicRegression;
use crate::equation::Expression;
use crate::error::{LmmError, Result};
use crate::lexicon::{Lexicon, word_tone};
use phf::{Set, phf_set};
use std::collections::HashMap;

/// Compile-time perfect-hash set of common function-word articles.
static ARTICLES: Set<&'static str> = phf_set! { "the", "a", "an" };

/// Compile-time perfect-hash set of common prepositions.
static PREPOSITIONS: Set<&'static str> = phf_set! {
    "in", "of", "through", "beyond", "within", "across", "beneath", "among", "into", "from",
    "toward", "over", "between", "after", "before", "along", "behind", "around", "upon",
    "to", "at", "by", "on", "under", "beside", "above", "below"
};

/// Compile-time perfect-hash set of coordinating/subordinating conjunctions.
static CONJUNCTIONS: Set<&'static str> = phf_set! {
    "and", "yet", "or", "while", "because", "where", "that", "which", "though", "whereas",
    "since", "as", "but", "nor", "so", "although", "if", "unless", "until", "when", "who"
};

/// Compile-time perfect-hash set of copular/auxiliary verbs.
static COPULAS: Set<&'static str> = phf_set! {
    "is", "was", "are", "were", "have", "had", "would", "could", "may", "must", "shall",
    "should", "can", "be", "been", "has", "do", "does", "did", "will", "might"
};

/// Compile-time perfect-hash set of common nouns used in generation pools.
static COMMON_NOUNS: Set<&'static str> = phf_set! {
    "world", "life", "time", "mind", "heart", "soul", "truth", "light", "force", "power",
    "knowledge", "form", "space", "nature", "order", "energy", "field", "depth", "path",
    "wave", "core", "edge", "flow", "source", "point", "thought", "reason", "vision",
    "sense", "voice", "code", "pattern", "motion", "change", "state", "law", "ground",
    "center", "realm", "layer", "base", "frame", "structure", "system", "process", "signal",
    "curve", "node", "origin", "principle", "concept", "theory", "equation", "formula",
    "rhythm", "harmony", "logic", "relation", "measure", "value", "symbol", "meaning",
    "wisdom", "insight", "clarity", "beauty", "shape", "dimension", "symmetry", "balance",
    "proportion", "ratio", "scale", "scope", "proof", "creation", "design", "purpose",
    "legacy", "cosmos", "matter", "element", "charge", "pulse", "cycle", "basis", "language",
    "message", "craft", "substance", "presence", "silence", "movement", "boundary", "horizon",
    "current", "gravity", "tension", "density", "volume", "surface", "fabric", "memory",
    "future", "history", "moment", "age", "epoch", "dawn"
};

/// Compile-time perfect-hash set of common adjectives.
static COMMON_ADJECTIVES: Set<&'static str> = phf_set! {
    "ancient", "deep", "vast", "pure", "clear", "bright", "true", "real", "great", "strong",
    "long", "wide", "high", "free", "open", "dark", "light", "still", "calm", "bold", "new",
    "old", "known", "hidden", "sacred", "cosmic", "eternal", "infinite", "divine", "natural",
    "logical", "formal", "primal", "human", "living", "moving", "rising", "central", "vital",
    "basic", "complex", "simple", "ordered", "precise", "exact", "linear", "dynamic", "static",
    "global", "total", "subtle", "dense", "outer", "inner", "silent", "finite", "higher",
    "noble", "fluid", "solid", "curved", "broken", "woven", "resonant", "latent"
};

/// Compile-time perfect-hash set of common verbs.
static COMMON_VERBS: Set<&'static str> = phf_set! {
    "reveal", "encode", "create", "form", "hold", "reach", "grow", "rise", "flows", "moves",
    "builds", "shapes", "contains", "reflects", "extends", "emerges", "unfolds", "expands",
    "compresses", "expresses", "represents", "models", "captures", "defines", "describes",
    "governs", "enables", "transforms", "generates", "solves", "proves", "measures", "encodes",
    "connects", "binds", "weaves", "follows", "guides", "aligns"
};

/// Compile-time perfect-hash set of common adverbs.
static COMMON_ADVERBS: Set<&'static str> = phf_set! {
    "deeply", "clearly", "truly", "freely", "greatly", "widely", "highly", "fully", "still",
    "always", "often", "never", "only", "merely", "precisely", "exactly", "naturally",
    "silently", "eternally"
};

/// Ordered slice of articles (for `pick_function_word`'s min-recency selection).
static ARTICLES_SLICE: &[&str] = &["the", "a", "an"];
static PREPOSITIONS_SLICE: &[&str] = &[
    "in", "of", "through", "beyond", "within", "across", "beneath", "among", "into", "from",
    "toward", "over", "between", "after", "before", "along", "behind", "around", "upon",
];
static CONJUNCTIONS_SLICE: &[&str] = &[
    "and", "yet", "or", "while", "because", "where", "that", "which", "though", "whereas", "since",
    "as",
];
static COPULAS_SLICE: &[&str] = &[
    "is", "was", "are", "were", "have", "had", "would", "could", "may", "must", "shall", "should",
    "can",
];
static COMMON_NOUNS_SLICE: &[&str] = &[
    "world",
    "life",
    "time",
    "mind",
    "heart",
    "soul",
    "truth",
    "light",
    "force",
    "power",
    "knowledge",
    "form",
    "space",
    "nature",
    "order",
    "energy",
    "field",
    "depth",
    "path",
    "wave",
    "core",
    "edge",
    "flow",
    "source",
    "point",
    "thought",
    "reason",
    "vision",
    "sense",
    "voice",
    "code",
    "pattern",
    "motion",
    "change",
    "state",
    "law",
    "ground",
    "center",
    "realm",
    "layer",
    "base",
    "frame",
    "structure",
    "system",
    "process",
    "signal",
    "curve",
    "node",
    "origin",
    "principle",
    "concept",
    "theory",
    "equation",
    "formula",
    "rhythm",
    "harmony",
    "logic",
    "relation",
    "measure",
    "value",
    "symbol",
    "meaning",
    "wisdom",
    "insight",
    "clarity",
    "beauty",
    "shape",
    "dimension",
    "symmetry",
    "balance",
    "proportion",
    "ratio",
    "scale",
    "scope",
    "proof",
    "creation",
    "design",
    "purpose",
    "legacy",
    "cosmos",
    "matter",
    "element",
    "charge",
    "pulse",
    "cycle",
    "basis",
    "language",
    "message",
    "craft",
    "substance",
    "presence",
    "silence",
    "movement",
    "boundary",
    "horizon",
    "current",
    "gravity",
    "tension",
    "density",
    "volume",
    "surface",
    "fabric",
    "memory",
    "future",
    "history",
    "moment",
    "age",
    "epoch",
    "dawn",
];
static COMMON_ADJECTIVES_SLICE: &[&str] = &[
    "ancient", "deep", "vast", "pure", "clear", "bright", "true", "real", "great", "strong",
    "long", "wide", "high", "free", "open", "dark", "light", "still", "calm", "bold", "new", "old",
    "known", "hidden", "sacred", "cosmic", "eternal", "infinite", "divine", "natural", "logical",
    "formal", "primal", "human", "living", "moving", "rising", "central", "vital", "basic",
    "complex", "simple", "ordered", "precise", "exact", "linear", "dynamic", "static", "global",
    "total", "subtle", "dense", "outer", "inner", "silent", "finite", "higher", "noble", "fluid",
    "solid", "curved", "broken", "woven", "resonant", "latent",
];
static COMMON_VERBS_SLICE: &[&str] = &[
    "reveal",
    "encode",
    "create",
    "form",
    "hold",
    "reach",
    "grow",
    "rise",
    "flows",
    "moves",
    "builds",
    "shapes",
    "contains",
    "reflects",
    "extends",
    "emerges",
    "unfolds",
    "expands",
    "compresses",
    "expresses",
    "represents",
    "models",
    "captures",
    "defines",
    "describes",
    "governs",
    "enables",
    "transforms",
    "generates",
    "solves",
    "proves",
    "measures",
    "encodes",
    "connects",
    "binds",
    "weaves",
    "follows",
    "guides",
    "aligns",
];
static COMMON_ADVERBS_SLICE: &[&str] = &[
    "deeply",
    "clearly",
    "truly",
    "freely",
    "greatly",
    "widely",
    "highly",
    "fully",
    "still",
    "always",
    "often",
    "never",
    "only",
    "merely",
    "precisely",
    "exactly",
    "naturally",
    "silently",
    "eternally",
];

/// Coarse part-of-speech categories.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
enum CoarsePos {
    Article,
    Preposition,
    Conjunction,
    Verb,
    Noun,
    Adjective,
    Adverb,
    Unknown,
}

/// Tags `word` with a coarse POS category using PHF set lookups (O(1) each).
fn detect_pos(word: &str) -> CoarsePos {
    let w = word.to_ascii_lowercase();
    let w = w.as_str();
    if ARTICLES.contains(w) {
        return CoarsePos::Article;
    }
    if PREPOSITIONS.contains(w) {
        return CoarsePos::Preposition;
    }
    if CONJUNCTIONS.contains(w) {
        return CoarsePos::Conjunction;
    }
    if COPULAS.contains(w) {
        return CoarsePos::Verb;
    }
    if COMMON_NOUNS.contains(w) {
        return CoarsePos::Noun;
    }
    if COMMON_ADJECTIVES.contains(w) {
        return CoarsePos::Adjective;
    }
    if COMMON_VERBS.contains(w) {
        return CoarsePos::Verb;
    }
    if COMMON_ADVERBS.contains(w) || w.ends_with("ly") {
        return CoarsePos::Adverb;
    }
    if w.ends_with("tion")
        || w.ends_with("ness")
        || w.ends_with("ity")
        || w.ends_with("ment")
        || w.ends_with("ics")
        || w.ends_with("ism")
        || w.ends_with("ence")
        || w.ends_with("ance")
        || w.ends_with("dom")
        || w.ends_with("hood")
    {
        return CoarsePos::Noun;
    }
    if w.ends_with("ful")
        || w.ends_with("less")
        || w.ends_with("ical")
        || w.ends_with("ous")
        || w.ends_with("ive")
        || w.ends_with("ible")
        || w.ends_with("able")
        || w.ends_with("ic")
        || w.ends_with("al")
    {
        return CoarsePos::Adjective;
    }
    if w.ends_with("ing")
        || w.ends_with("ize")
        || w.ends_with("ise")
        || w.ends_with("es")
        || w.ends_with("ed")
    {
        return CoarsePos::Verb;
    }
    CoarsePos::Unknown
}

/// Returns the most probable next POS given the recent history and current step counter.
fn expected_next_pos(history: &[CoarsePos], step: usize) -> CoarsePos {
    match history.last().copied() {
        Some(CoarsePos::Article) => match step % 3 {
            0 => CoarsePos::Adjective,
            1 => CoarsePos::Noun,
            _ => CoarsePos::Adjective,
        },
        Some(CoarsePos::Adjective) => CoarsePos::Noun,
        Some(CoarsePos::Noun) => match step % 7 {
            0 => CoarsePos::Verb,
            1 => CoarsePos::Preposition,
            2 => CoarsePos::Conjunction,
            3 => CoarsePos::Verb,
            4 => CoarsePos::Adverb,
            5 => CoarsePos::Preposition,
            _ => CoarsePos::Verb,
        },
        Some(CoarsePos::Verb) => match step % 5 {
            0 => CoarsePos::Article,
            1 => CoarsePos::Noun,
            2 => CoarsePos::Preposition,
            3 => CoarsePos::Adverb,
            _ => CoarsePos::Article,
        },
        Some(CoarsePos::Preposition) => CoarsePos::Article,
        Some(CoarsePos::Conjunction) => CoarsePos::Article,
        Some(CoarsePos::Adverb) => match step % 2 {
            0 => CoarsePos::Verb,
            _ => CoarsePos::Adjective,
        },
        Some(CoarsePos::Unknown) => CoarsePos::Preposition,
        None => CoarsePos::Article,
    }
}

fn is_function_pos(pos: CoarsePos) -> bool {
    matches!(
        pos,
        CoarsePos::Article | CoarsePos::Preposition | CoarsePos::Conjunction | CoarsePos::Verb
    )
}

fn pick_function_word(pos: CoarsePos, recency: &HashMap<String, usize>) -> &'static str {
    let candidates: &[&str] = match pos {
        CoarsePos::Article => ARTICLES_SLICE,
        CoarsePos::Preposition => PREPOSITIONS_SLICE,
        CoarsePos::Conjunction => CONJUNCTIONS_SLICE,
        CoarsePos::Verb => COPULAS_SLICE,
        _ => ARTICLES_SLICE,
    };
    candidates
        .iter()
        .min_by_key(|&&w| recency.get(w).copied().unwrap_or(0))
        .copied()
        .unwrap_or("the")
}

fn suffix_match(window: &[String], recent: &[String], max_suffix: usize) -> Option<String> {
    for len in (2..=max_suffix.min(recent.len())).rev() {
        let suffix = &recent[recent.len() - len..];
        for pos in 0..window.len().saturating_sub(len) {
            if window[pos..pos + len] == *suffix && pos + len < window.len() {
                return Some(window[pos + len].clone());
            }
        }
    }
    None
}

fn evaluate_at(eq: &Expression, pos: f64) -> f64 {
    let mut vars = HashMap::new();
    vars.insert("x".to_string(), pos);
    eq.evaluate(&vars).unwrap_or(107.0)
}

fn stable_target_tone(eq: &Expression, pos: f64, context_tones: &[f64]) -> f64 {
    let mean = context_tones.iter().sum::<f64>() / context_tones.len().max(1) as f64;
    let gp = evaluate_at(eq, pos);
    (if !gp.is_finite() || (gp - mean).abs() > 6.0 {
        mean
    } else {
        gp
    })
    .clamp(97.0, 122.0)
}

fn stable_target_length(eq: &Expression, pos: f64) -> usize {
    let raw = evaluate_at(eq, pos).round();
    if raw.is_finite() && (3.0..=10.0).contains(&raw) {
        raw as usize
    } else {
        5
    }
}

fn score_word(
    word: &str,
    target_tone: f64,
    target_length: usize,
    recency: &HashMap<String, usize>,
) -> f64 {
    let r = recency.get(word).copied().unwrap_or(0) as f64 * 15.0;
    let tone_diff = (word_tone(word) - target_tone).abs();
    let len_diff = (word.len() as f64 - target_length as f64).abs() * 1.2;
    r + tone_diff + len_diff
}

fn best_from_pool<'a>(
    pool: &[&'a str],
    target_tone: f64,
    target_length: usize,
    recency: &HashMap<String, usize>,
) -> Option<&'a str> {
    pool.iter().copied().min_by(|&a, &b| {
        let sa = score_word(a, target_tone, target_length, recency);
        let sb = score_word(b, target_tone, target_length, recency);
        sa.partial_cmp(&sb).unwrap_or(std::cmp::Ordering::Equal)
    })
}

/// A symbolic text continuation engine.
///
/// `TextPredictor` uses symbolic regression to fit a tone trajectory and a rhythm
/// trajectory over the input window, then generates tokens token-by-token, selecting
/// words from compile-time lexical pools that best match the predicted tone and length.
///
/// # Examples
///
/// ```
/// use lmm::traits::Simulatable;
/// use lmm::predict::TextPredictor;
///
/// let predictor = TextPredictor::new(20, 30, 3);
/// let result = predictor.predict_continuation("the universe reveals its form", 40);
/// assert!(result.is_ok());
/// let cont = result.unwrap();
/// assert!(!cont.continuation.trim().is_empty());
/// ```
pub struct TextPredictor {
    /// Number of input tokens used as context.
    pub window_size: usize,
    /// Symbolic regression iterations.
    pub iterations: usize,
    /// Maximum expression tree depth.
    pub depth: usize,
    /// Optional system dictionary for extended vocabulary.
    pub lexicon: Option<Lexicon>,
}

impl TextPredictor {
    /// Creates a new [`TextPredictor`] without a lexicon.
    ///
    /// # Arguments
    ///
    /// * `window_size` - Number of context tokens.
    /// * `iterations` - Symbolic regression iterations.
    /// * `depth` - Maximum expression depth.
    ///
    /// # Examples
    ///
    /// ```
    /// use lmm::predict::TextPredictor;
    /// let p = TextPredictor::new(10, 20, 3);
    /// assert_eq!(p.window_size, 10);
    /// ```
    pub fn new(window_size: usize, iterations: usize, depth: usize) -> Self {
        Self {
            window_size,
            iterations,
            depth,
            lexicon: None,
        }
    }

    /// Attaches a [`Lexicon`] for extended vocabulary during content-word selection.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use lmm::predict::TextPredictor;
    /// use lmm::lexicon::Lexicon;
    ///
    /// let lex = Lexicon::load_system().unwrap();
    /// let p = TextPredictor::new(20, 30, 3).with_lexicon(lex);
    /// assert!(p.lexicon.is_some());
    /// ```
    pub fn with_lexicon(mut self, lexicon: Lexicon) -> Self {
        self.lexicon = Some(lexicon);
        self
    }

    fn fit_trajectory(&self, positions: &[f64], tones: &[f64]) -> Result<Expression> {
        if positions.len() < 2 {
            return Err(LmmError::Discovery("Need at least 2 tokens".into()));
        }
        let inputs: Vec<Vec<f64>> = positions.iter().map(|&p| vec![p]).collect();
        SymbolicRegression::new(self.depth, self.iterations)
            .with_variables(vec!["x".into()])
            .with_population(60)
            .fit(&inputs, tones)
    }

    fn fit_rhythm(&self, positions: &[f64], lengths: &[f64]) -> Result<Expression> {
        if positions.len() < 2 {
            return Err(LmmError::Discovery("Need at least 2 tokens".into()));
        }
        let inputs: Vec<Vec<f64>> = positions.iter().map(|&p| vec![p]).collect();
        SymbolicRegression::new(self.depth.min(3), self.iterations / 2)
            .with_variables(vec!["x".into()])
            .with_population(40)
            .fit(&inputs, lengths)
    }

    fn curated_pool_for_pos(next_pos: CoarsePos) -> &'static [&'static str] {
        match next_pos {
            CoarsePos::Noun => COMMON_NOUNS_SLICE,
            CoarsePos::Adjective => COMMON_ADJECTIVES_SLICE,
            CoarsePos::Verb => COMMON_VERBS_SLICE,
            CoarsePos::Adverb => COMMON_ADVERBS_SLICE,
            _ => COMMON_NOUNS_SLICE,
        }
    }

    #[allow(clippy::too_many_arguments)]
    fn select_content_word(
        &self,
        next_pos: CoarsePos,
        trajectory_eq: &Expression,
        rhythm_eq: &Expression,
        pos: f64,
        recency: &HashMap<String, usize>,
        context_tones: &[f64],
    ) -> String {
        let target_tone = stable_target_tone(trajectory_eq, pos, context_tones);
        let target_length = stable_target_length(rhythm_eq, pos);
        let curated = Self::curated_pool_for_pos(next_pos);
        if let Some(w) = best_from_pool(curated, target_tone, target_length, recency) {
            return w.to_string();
        }
        if let Some(lexicon) = &self.lexicon {
            let candidates = lexicon.candidates(target_length, target_tone, 2, 8.0, 30);
            if let Some(w) = best_from_pool(&candidates, target_tone, target_length, recency) {
                return w.to_string();
            }
        }
        "world".to_string()
    }

    #[allow(clippy::too_many_arguments)]
    fn select_from_context(
        &self,
        next_pos: CoarsePos,
        window_tokens: &[String],
        trajectory_eq: &Expression,
        rhythm_eq: &Expression,
        pos: f64,
        recency: &HashMap<String, usize>,
        context_tones: &[f64],
    ) -> String {
        let target_tone = stable_target_tone(trajectory_eq, pos, context_tones);
        let target_len = stable_target_length(rhythm_eq, pos);

        let mut candidates: Vec<&str> = window_tokens.iter().map(String::as_str).collect();
        candidates.extend_from_slice(Self::curated_pool_for_pos(next_pos));

        best_from_pool(&candidates, target_tone, target_len, recency)
            .unwrap_or("world")
            .to_string()
    }

    /// Predicts a continuation of `text` up to `predict_length` characters.
    ///
    /// # Arguments
    ///
    /// * `text` - Input prompt (at least 2 whitespace-delimited words).
    /// * `predict_length` - Target continuation character count.
    ///
    /// # Returns
    ///
    /// (`Result<PredictedContinuation>`): The generated continuation, fitted equations,
    /// and metadata.
    ///
    /// # Errors
    ///
    /// Returns [`LmmError::Perception`] for empty/too-short input, or
    /// [`LmmError::Discovery`] if symbolic regression fails.
    ///
    /// # Examples
    ///
    /// ```
    /// use lmm::predict::TextPredictor;
    ///
    /// let p = TextPredictor::new(20, 30, 3);
    /// let c = p.predict_continuation("mathematics reveals the truth of existence", 50).unwrap();
    /// assert!(!c.continuation.trim().is_empty());
    /// ```
    pub fn predict_continuation(
        &self,
        text: &str,
        predict_length: usize,
    ) -> Result<PredictedContinuation> {
        if text.is_empty() {
            return Err(LmmError::Perception("Input text is empty".into()));
        }
        let all_tokens: Vec<String> = text.split_whitespace().map(String::from).collect();
        if all_tokens.len() < 2 {
            return Err(LmmError::Perception("Need at least 2 words".into()));
        }
        let window_start = all_tokens.len().saturating_sub(self.window_size);
        let window_tokens = &all_tokens[window_start..];

        let positions: Vec<f64> = (0..window_tokens.len()).map(|i| i as f64).collect();
        let tones: Vec<f64> = window_tokens
            .iter()
            .map(|t| word_tone(&t.to_ascii_lowercase()))
            .collect();
        let lengths: Vec<f64> = window_tokens.iter().map(|t| t.len() as f64).collect();

        let trajectory_eq = self.fit_trajectory(&positions, &tones)?;
        let rhythm_eq = self.fit_rhythm(&positions, &lengths)?;

        let mut pos_history: Vec<CoarsePos> = window_tokens.iter().map(|t| detect_pos(t)).collect();
        let mut suffix_context: Vec<String> = Vec::new();
        let mut continuation = String::new();
        let mut pos = window_tokens.len() as f64;
        let mut recency: HashMap<String, usize> = HashMap::new();
        let mut step: usize = 0;

        while continuation.len() < predict_length {
            let suffix_result =
                suffix_match(window_tokens, &suffix_context, 2.min(suffix_context.len()));
            let chosen = match suffix_result {
                Some(w) => w,
                None => {
                    let next_pos = expected_next_pos(&pos_history, step);
                    if is_function_pos(next_pos) {
                        pick_function_word(next_pos, &recency).to_string()
                    } else if let Some(_lexicon) = &self.lexicon {
                        self.select_content_word(
                            next_pos,
                            &trajectory_eq,
                            &rhythm_eq,
                            pos,
                            &recency,
                            &tones,
                        )
                    } else {
                        self.select_from_context(
                            next_pos,
                            window_tokens,
                            &trajectory_eq,
                            &rhythm_eq,
                            pos,
                            &recency,
                            &tones,
                        )
                    }
                }
            };

            continuation.push(' ');
            continuation.push_str(&chosen);
            *recency.entry(chosen.clone()).or_insert(0) += 1;
            pos_history.push(detect_pos(&chosen));
            if pos_history.len() > 8 {
                pos_history.remove(0);
            }
            suffix_context.push(chosen);
            if suffix_context.len() > 3 {
                suffix_context.remove(0);
            }
            pos += 1.0;
            step += 1;
        }

        Ok(PredictedContinuation {
            trajectory_equation: trajectory_eq,
            rhythm_equation: rhythm_eq,
            window_used: window_tokens.len(),
            continuation,
        })
    }
}

/// The output of a text prediction pass.
///
/// # Fields
///
/// - `trajectory_equation` - symbolic expression mapping token position → tone.
/// - `rhythm_equation` - symbolic expression mapping token position → word length.
/// - `window_used` - number of input tokens used as context.
/// - `continuation` - the generated text continuation (prefixed with a space).
///
/// # Examples
///
/// ```
/// use lmm::traits::Simulatable;
/// use lmm::predict::TextPredictor;
///
/// let p = TextPredictor::new(10, 20, 2);
/// let c = p.predict_continuation("mathematics reveals the truth", 30).unwrap();
/// println!("Equation: {}", c.trajectory_equation);
/// println!("Window: {}", c.window_used);
/// ```
pub struct PredictedContinuation {
    /// Symbolic expression fitting tone over token positions.
    pub trajectory_equation: Expression,
    /// Symbolic expression fitting word length over token positions.
    pub rhythm_equation: Expression,
    /// Number of input tokens used as context.
    pub window_used: usize,
    /// The generated continuation text.
    pub continuation: String,
}

// Copyright 2026 Mahmoud Harmouch.
//
// Licensed under the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.