prosaic-core 1.0.1

General-purpose natural language generation from structured data
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
//! Composite scorer for the retrospective refine pass.
//!
//! Computes a single weighted-sum quality score over a [`RenderedDocument`]
//! for a given [`RefineWeights`] and optional [`StyleProfile`]. The
//! scorer is a pure function — no mutation, no side effects. Higher
//! scores are better; the iteration controller compares candidate scores
//! to decide whether each refinement iteration is improving the output.
//!
//! Each component lands in `[0.0, 1.0]` so the weighted sum stays within
//! a predictable range. Components:
//!
//! - **Repetition compliance** — 1 - average per-sentence word
//!   repetition fraction. Higher = more lexical variety across sentences.
//! - **Rhythm compliance** — 1 - normalized cadence flatness. Higher =
//!   more sentence-length variance.
//! - **Connective family balance** — 1 - dominant-family share. Higher
//!   = no single family dominates document-scope emissions.
//! - **Paragraph opener diversity** — distinct openers / total
//!   paragraphs that opened with a connective. Higher = more variety.
//! - **List-style diversity** — distinct styles / total styles emitted.
//! - **RST relation balance** — 1 - dominant-relation share.
//! - **Profile match** — 1 - L1 distance between observed and profile
//!   target length distribution; gated on profile presence.

#[cfg(not(feature = "std"))]
use alloc::string::String;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

use crate::discourse::ListStyle;
use crate::refine::{RefineWeights, RenderedDocument};
use crate::rst::RstRelation;
use crate::style::StyleProfile;

/// Compute the composite score for `document` under `weights` and
/// `profile`. Returns a value in `[0.0, sum_of_weights]`. Higher is
/// better.
pub fn score_document(
    document: &RenderedDocument,
    weights: &RefineWeights,
    profile: Option<&StyleProfile>,
) -> f32 {
    weights.repetition * repetition_compliance(document)
        + weights.rhythm * rhythm_compliance(document)
        + weights.connective * connective_family_balance(document)
        + weights.paragraph_opener * paragraph_opener_diversity(document)
        + weights.list_style_diversity * list_style_diversity(document)
        + weights.rst_balance * rst_relation_balance(document)
        + weights.profile_match * profile_match(document, profile)
}

fn repetition_compliance(document: &RenderedDocument) -> f32 {
    if document.sentences.len() < 2 {
        return 1.0;
    }
    // Approximation: 1 minus the average pairwise Jaccard similarity over
    // adjacent sentences. Adjacent-pair similarity is what discourse
    // repetition perceives most strongly.
    let mut total_sim = 0.0_f32;
    let mut pairs = 0_usize;
    for window in document.sentences.windows(2) {
        let a = tokenize(&window[0].text);
        let b = tokenize(&window[1].text);
        if a.is_empty() || b.is_empty() {
            continue;
        }
        let intersection: usize = a.iter().filter(|w| b.contains(w)).count();
        let union: usize = a
            .iter()
            .chain(b.iter())
            .collect::<alloc::collections::BTreeSet<_>>()
            .len();
        if union > 0 {
            total_sim += intersection as f32 / union as f32;
            pairs += 1;
        }
    }
    if pairs == 0 {
        return 1.0;
    }
    1.0 - (total_sim / pairs as f32).clamp(0.0, 1.0)
}

fn rhythm_compliance(document: &RenderedDocument) -> f32 {
    if document.sentences.len() < 3 {
        return 1.0;
    }
    let lengths: Vec<f32> = document
        .sentences
        .iter()
        .map(|s| s.word_count as f32)
        .collect();
    let n = lengths.len() as f32;
    let mean = lengths.iter().sum::<f32>() / n;
    let variance = lengths
        .iter()
        .map(|x| {
            let d = x - mean;
            d * d
        })
        .sum::<f32>()
        / n;
    let stdev = approx_sqrt(variance);
    // Normalize: stdev of 0 → score 0 (perfectly flat); stdev ≥ 6 → score 1.
    (stdev / 6.0_f32).clamp(0.0, 1.0)
}

/// Newton-Raphson `sqrt` approximation. Used in place of `f32::sqrt` to
/// keep the refine module no_std-compatible (the std `sqrt` impl isn't
/// available in `core` on stable).
fn approx_sqrt(x: f32) -> f32 {
    if x <= 0.0 {
        return 0.0;
    }
    let mut g = if x >= 1.0 { x } else { 1.0 };
    for _ in 0..6 {
        g = 0.5 * (g + x / g);
    }
    g
}

fn connective_family_balance(document: &RenderedDocument) -> f32 {
    if document.connectives_used.is_empty() {
        return 1.0;
    }
    let total = document.connectives_used.len() as f32;
    let mut count = alloc::collections::BTreeMap::<&'static str, usize>::new();
    for u in &document.connectives_used {
        if let Some(family) = family_for(&u.connective) {
            *count.entry(family).or_insert(0) += 1;
        }
    }
    if count.is_empty() {
        return 1.0;
    }
    let dominant = count.values().copied().max().unwrap_or(0) as f32;
    (1.0 - dominant / total).clamp(0.0, 1.0)
}

fn paragraph_opener_diversity(document: &RenderedDocument) -> f32 {
    let openers: Vec<&String> = document
        .paragraphs
        .iter()
        .filter_map(|p| {
            p.sentences
                .first()
                .and_then(|s| s.opening_connective.as_ref())
        })
        .collect();
    if openers.is_empty() {
        return 1.0;
    }
    let distinct: alloc::collections::BTreeSet<&String> = openers.iter().copied().collect();
    (distinct.len() as f32 / openers.len() as f32).clamp(0.0, 1.0)
}

fn list_style_diversity(document: &RenderedDocument) -> f32 {
    if document.list_styles_used.is_empty() {
        return 1.0;
    }
    let distinct: alloc::collections::BTreeSet<ListStyle> = document
        .list_styles_used
        .iter()
        .map(|u| u.list_style)
        .collect();
    (distinct.len() as f32 / document.list_styles_used.len() as f32).clamp(0.0, 1.0)
}

fn rst_relation_balance(document: &RenderedDocument) -> f32 {
    if document.connectives_used.is_empty() {
        return 1.0;
    }
    let mut count = alloc::collections::BTreeMap::<RstRelation, usize>::new();
    let mut classified_total = 0_usize;
    for u in &document.connectives_used {
        if let Some(rst) = rst_for(&u.connective) {
            *count.entry(rst).or_insert(0) += 1;
            classified_total += 1;
        }
    }
    if classified_total == 0 {
        return 1.0;
    }
    let dominant = count.values().copied().max().unwrap_or(0) as f32;
    (1.0 - dominant / classified_total as f32).clamp(0.0, 1.0)
}

fn profile_match(document: &RenderedDocument, profile: Option<&StyleProfile>) -> f32 {
    let Some(profile) = profile else {
        return 1.0;
    };
    if profile.sentence_length.is_neutral() || document.sentences.is_empty() {
        return 1.0;
    }
    let dist = &profile.sentence_length;
    let mut counts = [0_usize; 3];
    for sentence in &document.sentences {
        let bucket = if sentence.word_count <= dist.short_max_words as usize {
            0
        } else if sentence.word_count <= dist.medium_max_words as usize {
            1
        } else {
            2
        };
        counts[bucket] += 1;
    }
    let total = document.sentences.len() as f32;
    let observed = [
        counts[0] as f32 / total,
        counts[1] as f32 / total,
        counts[2] as f32 / total,
    ];
    let target_sum = dist.short + dist.medium + dist.long;
    if target_sum <= 0.0 {
        return 1.0;
    }
    let target = [
        dist.short / target_sum,
        dist.medium / target_sum,
        dist.long / target_sum,
    ];
    let l1 = (observed[0] - target[0]).abs()
        + (observed[1] - target[1]).abs()
        + (observed[2] - target[2]).abs();
    // L1 distance ranges 0..=2 for normalized distributions.
    (1.0 - l1 / 2.0).clamp(0.0, 1.0)
}

fn tokenize(text: &str) -> Vec<String> {
    text.split_whitespace()
        .filter_map(|w| {
            let cleaned: String = w
                .chars()
                .filter(|c| c.is_alphanumeric())
                .flat_map(|c| c.to_lowercase())
                .collect();
            if cleaned.len() > 2 {
                Some(cleaned)
            } else {
                None
            }
        })
        .collect()
}

fn family_for(connective: &str) -> Option<&'static str> {
    for c in &["Additionally,", "Furthermore,", "It also"] {
        if connective.starts_with(c) {
            return Some("continuation");
        }
    }
    for c in &["Similarly,", "Likewise,"] {
        if connective.starts_with(c) {
            return Some("similarity");
        }
    }
    for c in &["Meanwhile,", "However,", "On the other hand,"] {
        if connective.starts_with(c) {
            return Some("contrast");
        }
    }
    None
}

fn rst_for(connective: &str) -> Option<RstRelation> {
    for c in &["Additionally,", "Furthermore,", "It also"] {
        if connective.starts_with(c) {
            return Some(RstRelation::Elaboration);
        }
    }
    for c in &["Similarly,", "Likewise,"] {
        if connective.starts_with(c) {
            return Some(RstRelation::Sequence);
        }
    }
    for c in &["Meanwhile,", "However,", "On the other hand,"] {
        if connective.starts_with(c) {
            return Some(RstRelation::Contrast);
        }
    }
    None
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::refine::{EventMeta, ParagraphRender, RenderedDocument};

    fn doc_from(paragraphs: Vec<ParagraphRender>) -> RenderedDocument {
        RenderedDocument::from_paragraphs(paragraphs)
    }

    fn one_paragraph(
        text: &str,
        connective: Option<&str>,
        list_style: Option<ListStyle>,
    ) -> ParagraphRender {
        ParagraphRender {
            text: text.to_string(),
            events: vec![EventMeta {
                connective: connective.map(|s| s.to_string()),
                list_style,
            }],
        }
    }

    fn weights() -> RefineWeights {
        RefineWeights::default()
    }

    // ── Pure-function determinism ────────────────────────────────────────

    #[test]
    fn empty_document_scores_at_max() {
        // No sentences = no detected failures. Score should sum to all
        // weights at full value.
        let doc = doc_from(vec![]);
        let s = score_document(&doc, &weights(), None);
        let max = weights().repetition
            + weights().rhythm
            + weights().connective
            + weights().paragraph_opener
            + weights().list_style_diversity
            + weights().rst_balance
            + weights().profile_match;
        assert!((s - max).abs() < 0.001);
    }

    #[test]
    fn score_is_deterministic() {
        let doc = doc_from(vec![
            one_paragraph("First short sentence.", None, None),
            one_paragraph(
                "Additionally, second longer sentence with more words.",
                Some("Additionally,"),
                None,
            ),
        ]);
        let a = score_document(&doc, &weights(), None);
        let b = score_document(&doc, &weights(), None);
        assert_eq!(a, b);
    }

    // ── Monotonicity ─────────────────────────────────────────────────────

    #[test]
    fn rhythm_compliance_higher_with_more_variance() {
        let flat = doc_from(
            (0..6)
                .map(|i| {
                    one_paragraph(
                        &format!(
                            "{} word word word word word word word word word.",
                            "x".repeat(i + 1)
                        ),
                        None,
                        None,
                    )
                })
                .collect(),
        );
        let varied = doc_from(vec![
            one_paragraph("Short.", None, None),
            one_paragraph("A medium length sentence here for context.", None, None),
            one_paragraph(
                "And a much longer sentence with several clauses extending well beyond average length.",
                None,
                None,
            ),
            one_paragraph("Tiny.", None, None),
            one_paragraph(
                "Another medium length sentence with reasonable word count.",
                None,
                None,
            ),
            one_paragraph(
                "Yet another extended one with more words to really push the variance up.",
                None,
                None,
            ),
        ]);
        assert!(rhythm_compliance(&varied) > rhythm_compliance(&flat));
    }

    #[test]
    fn paragraph_opener_diversity_higher_with_distinct_openers() {
        let monotone = doc_from(
            (0..4)
                .map(|_| {
                    one_paragraph(
                        "Additionally, opener text here.",
                        Some("Additionally,"),
                        None,
                    )
                })
                .collect(),
        );
        let diverse = doc_from(vec![
            one_paragraph("Additionally, opener.", Some("Additionally,"), None),
            one_paragraph("Furthermore, opener.", Some("Furthermore,"), None),
            one_paragraph("However, opener.", Some("However,"), None),
            one_paragraph("Similarly, opener.", Some("Similarly,"), None),
        ]);
        assert!(paragraph_opener_diversity(&diverse) > paragraph_opener_diversity(&monotone));
    }

    #[test]
    fn list_style_diversity_higher_with_distinct_styles() {
        let monotone = doc_from(
            (0..4)
                .map(|_| one_paragraph("Sentence with list.", None, Some(ListStyle::Including)))
                .collect(),
        );
        let diverse = doc_from(vec![
            one_paragraph("Sentence.", None, Some(ListStyle::Including)),
            one_paragraph("Sentence.", None, Some(ListStyle::SuchAs)),
            one_paragraph("Sentence.", None, Some(ListStyle::Dash)),
            one_paragraph("Sentence.", None, Some(ListStyle::Bracketed)),
        ]);
        assert!(list_style_diversity(&diverse) > list_style_diversity(&monotone));
    }

    #[test]
    fn rst_relation_balance_higher_when_balanced() {
        let imbalanced = doc_from(
            (0..5)
                .map(|_| one_paragraph("Additionally, sentence.", Some("Additionally,"), None))
                .collect(),
        );
        let balanced = doc_from(vec![
            one_paragraph("Additionally, sentence.", Some("Additionally,"), None),
            one_paragraph("However, sentence.", Some("However,"), None),
            one_paragraph("Similarly, sentence.", Some("Similarly,"), None),
            one_paragraph("Furthermore, sentence.", Some("Furthermore,"), None),
            one_paragraph("Likewise, sentence.", Some("Likewise,"), None),
        ]);
        assert!(rst_relation_balance(&balanced) > rst_relation_balance(&imbalanced));
    }

    #[test]
    fn profile_match_higher_when_distribution_aligns() {
        let target = crate::style::LengthDistribution {
            short: 1.0,
            medium: 0.0,
            long: 0.0,
            short_max_words: 8,
            medium_max_words: 18,
        };
        let p = StyleProfile::builder("short-only")
            .sentence_length(target)
            .build()
            .unwrap();
        let aligned = doc_from(
            (0..6)
                .map(|_| {
                    one_paragraph("Short text here.", None, None) // 3 words → short
                })
                .collect(),
        );
        let misaligned = doc_from(
            (0..6)
                .map(|_| {
                    one_paragraph(
                        "A long sentence with many many words far above the short threshold count.",
                        None,
                        None,
                    )
                })
                .collect(),
        );
        assert!(profile_match(&aligned, Some(&p)) > profile_match(&misaligned, Some(&p)));
    }

    #[test]
    fn full_score_increases_when_one_dimension_strictly_improves() {
        // Same documents except `improved` has more diverse paragraph
        // openers. All other components compute the same — so the full
        // score must rise by ≈ weights.paragraph_opener × delta.
        let mono_openers = doc_from(vec![
            one_paragraph("Additionally, foo.", Some("Additionally,"), None),
            one_paragraph("Additionally, bar.", Some("Additionally,"), None),
            one_paragraph("Additionally, baz.", Some("Additionally,"), None),
            one_paragraph("Additionally, qux.", Some("Additionally,"), None),
        ]);
        let diverse_openers = doc_from(vec![
            one_paragraph("Additionally, foo.", Some("Additionally,"), None),
            one_paragraph("Furthermore, bar.", Some("Furthermore,"), None),
            one_paragraph("However, baz.", Some("However,"), None),
            one_paragraph("Similarly, qux.", Some("Similarly,"), None),
        ]);
        assert!(
            score_document(&diverse_openers, &weights(), None)
                > score_document(&mono_openers, &weights(), None)
        );
    }

    // ── Tokenizer guard ──────────────────────────────────────────────────

    #[test]
    fn tokenize_drops_short_and_punct() {
        let toks = tokenize("a, foo bar! the. baz?");
        assert_eq!(
            toks,
            vec![
                "foo".to_string(),
                "bar".to_string(),
                "the".to_string(),
                "baz".to_string()
            ]
        );
    }
}