diff --git a/crates/mind-body/src/projection/interpret.rs b/crates/mind-body/src/projection/interpret.rs index f20092b..35644fe 100644 --- a/crates/mind-body/src/projection/interpret.rs +++ b/crates/mind-body/src/projection/interpret.rs @@ -667,7 +667,7 @@ pub(crate) fn speech_prompt_in(packet: &Packet, narration: &str, prose: &str, tu /// it. Short answers such as "ok" or "Hi" are never echoes, a short line she heard is never one, and a line that answers /// a claim by naming it is not one. fn echo(line: &str, bodies: &[String]) -> bool { - let long = |text: &str| text.split(|c: char| !c.is_alphanumeric() && c != '\'').filter(|word| !word.is_empty()).count() >= ECHO_MIN_WORDS; + let long = |text: &str| span::tokens(text).count() >= ECHO_MIN_WORDS; long(line) && (heard_overlap(line, &bodies.join("\n")) >= ECHO_OVERLAP || bodies.iter().any(|body| long(body) && heard_overlap(body, line) >= ECHO_OVERLAP)) } @@ -1335,19 +1335,13 @@ fn reopen_of(raw: &Value, packet: &Packet, prose: &str) -> Option { Some(Proposal::PromiseReopen { promise: promise.to_owned(), words: proposed.words.trim().to_owned(), needs, reached }) } -/// Her words compared as a promise is found in a draft: case-blind, and a curly apostrophe or single quote read as a -/// straight one. -fn folded(text: &str) -> String { - text.to_lowercase().replace(['\u{2018}', '\u{2019}'], "'") -} - -/// Whether `promise` names an act in `draft`: at least `PROMISE_MIN_WORDS` words, found in the draft's text as `folded` -/// reads both. Why not, when not. +/// Whether `promise` names an act in `draft`: at least `PROMISE_MIN_WORDS` words (`span::tokens`), found in the draft's +/// text with both folded by `span::fold` and lowercased (K52). Why not, when not. fn promise_found(draft: &str, promise: &str) -> Result<(), &'static str> { - let promise = folded(promise); - if promise.split(|c: char| !c.is_alphanumeric() && c != '\'').filter(|word| !word.is_empty()).count() < PROMISE_MIN_WORDS { + let folded = |text: &str| text.chars().map(span::fold).collect::().to_lowercase(); + if span::tokens(promise).count() < PROMISE_MIN_WORDS { Err("fewer than four words") - } else if !folded(draft).contains(&promise) { + } else if !folded(draft).contains(&folded(promise)) { Err("not in the draft") } else { Ok(()) diff --git a/crates/mind-body/src/projection/interpret/tests.rs b/crates/mind-body/src/projection/interpret/tests.rs index 5c03a70..c7b70ed 100644 --- a/crates/mind-body/src/projection/interpret/tests.rs +++ b/crates/mind-body/src/projection/interpret/tests.rs @@ -2176,3 +2176,78 @@ fn k48_a_non_contract_answer_twice_still_fails_the_turn() { assert_eq!(decision["failed"].as_str(), Some("the answer is not the speech contract"), "{decision}"); assert!(decision.get("said").is_none() && decision.get("undecided").is_none(), "{decision}"); } + +// --------------------------------------------------------------------------------------------- +// K51 and K52 (map 16.31.3, 16.31.4): what a hold has held is every draft she did not send, measured over the room, and +// every comparison of her words reads typographic punctuation as the locator does. Every turn-2 test builds its packet +// from turn 1's committed state, never from carries written by hand. + +/// Turn 2 of X.1 over her state `holds`: Kiko asks in #minds what she wrote. +fn kiko_asks(holds: &Value) -> Packet { + let mut next = heimdall(); + next["holds"] = holds.clone(); + let room = Room { messages: vec![line(MINDS, KIKO, "Kiko", "Libby, what did you end up writing for the newcomers?", true)], here: Some(MINDS.into()), ..Room::default() }; + packet_in(TurnKind::Chat, &next, &room, 1) +} + +/// Turn 1 of X.1: Meta's hold opens from q3, and each of `texts` is a draft of hers naming it, so none goes out. The +/// decision is committed through `commit::apply`, and turn 2's packet is built from the committed state. +fn held_then_kiko_asks(texts: &[&str]) -> (Value, Packet) { + let prose = format!("Meta asked me to hold it. {}", texts.iter().map(|text| format!("{text}")).collect::>().join(" ")); + let say: Vec = (1..=texts.len() as u64).map(|n| say_holding(n, json!(["q3"]))).collect(); + let decision = answered(&x1_packet(), &prose, json!({ "say": say, "gaps": [], "hold": meta_holds_here() })); + let (state, _) = committed(&decision, &prose); + let packet = kiko_asks(&state["holds"]); + (state, packet) +} + +/// Soul's `soul_c`, inverted, in both of its shapes: the carried text retyped with typographic apostrophes, naming no +/// hold, is held; so are the welcome's own lines retyped curly behind a lead-in. +#[test] +fn k52_soul_c_a_curly_retype_is_held() { + let (_, p2) = held_then_kiko_asks(&[K45_WELCOME]); + let curly = K45_WELCOME.replace('\'', "\u{2019}"); + assert_ne!(curly, K45_WELCOME); + let d = answered(&p2, &format!("Kiko asked. {curly}"), json!({ "say": [say_holding(1, json!([]))], "gaps": [] })); + assert_eq!((texts(&d), d["refused"][0]["reason"].as_str()), (Vec::::new(), Some("held")), "{d}"); + let (_, p2) = held_then_kiko_asks(&[x1_libby_r1_draft_1().as_str()]); + let lines = x1_libby_r1_draft_1().replace('\'', "\u{2019}"); + let d = answered(&p2, &format!("Kiko asked. Kiko, here it is: {lines}"), json!({ "say": [say_holding(1, json!([]))], "gaps": [] })); + assert_eq!((texts(&d), d["refused"][0]["reason"].as_str()), (Vec::::new(), Some("held")), "{d}"); +} + +/// U+02BC, the modifier apostrophe, folds in the locator, in the word split and in a promise found in a draft. This +/// pins Soul's surviving mutant `k47-fold-no-modifier-apostrophe`. +#[test] +fn k52_modifier_apostrophe_folds() { + let source = "Libby, don\u{02BC}t post it yet."; + assert_eq!(crate::projection::span::locate(source, "don't post it").map(|span| &source[span.start..span.end]), Some("don\u{02BC}t post it")); + assert_eq!(crate::projection::span::tokens("You\u{02BC}re HERE, we\u{2019}re here").collect::>(), ["you're", "here", "we're", "here"]); + assert_eq!(promise_found("I\u{02BC}ll send the file tonight.", "I'll send the file"), Ok(())); + assert_eq!(promise_found("I\u{2018}ll send the file tonight.", "I'll send the file"), Ok(())); +} + +/// An echo with typographic apostrophes is an echo: the words she heard, retyped curly, are not her speech. +#[test] +fn k52_an_echo_with_curly_apostrophes_is_an_echo() { + let heard = "I don't know, you're right, we'd better wait, it's late and they're tired."; + let mut packet = packet(); + packet.bodies = vec![heard.into()]; + let curly = heard.replace('\'', "\u{2019}"); + let d = answered(&packet, &format!("{curly}"), json!({ "say": [say_holding(1, json!([]))], "gaps": [] })); + assert_eq!((texts(&d), d["refused"][0]["reason"].as_str()), (Vec::::new(), Some("echo")), "{d}"); +} + +/// Negative grep: `projection/` compares her words with anyone's through `span::tokens` and folds through `span::fold` +/// alone: no other likeness split keeps apostrophes, and no second fold table replaces typographic quotes. +#[test] +fn k52_one_fold_one_word_split() { + for (path, text) in &projection_sources() { + if path.ends_with("span.rs") { + continue; + } + for banned in ["!c.is_alphanumeric() && c != '\''", "fn folded(text: &str) -> String", ".replace('\u{2019}'", ".replace(['\u{2018}'"] { + assert!(!text.contains(banned), "{path} holds {banned}"); + } + } +} diff --git a/crates/mind-body/src/projection/narrate.rs b/crates/mind-body/src/projection/narrate.rs index f056cb7..71a89c8 100644 --- a/crates/mind-body/src/projection/narrate.rs +++ b/crates/mind-body/src/projection/narrate.rs @@ -351,10 +351,11 @@ pub(crate) fn persona_prompt(packet: &Packet, narration: &str, tuning: &Tuning) } /// The share of the narration's word trigrams that also appear in what she heard: a lexical overlap, cheap and blind to -/// paraphrase. A metric, never a filter: 0 is no shared word run. +/// paraphrase, over `span::tokens`, so typographic punctuation reads as the locator reads it (K52). A metric in the +/// narration reports, and `interpret::echo`'s measure: 0 is no shared word run. pub(crate) fn heard_overlap(narration: &str, heard: &str) -> f64 { let trigrams = |text: &str| -> Vec<(String, String, String)> { - let words: Vec = text.split(|c: char| !c.is_alphanumeric() && c != '\'').filter(|word| !word.is_empty()).map(str::to_lowercase).collect(); + let words: Vec = super::span::tokens(text).collect(); words.windows(3).map(|three| (three[0].clone(), three[1].clone(), three[2].clone())).collect() }; let told = trigrams(narration); @@ -369,7 +370,7 @@ pub(crate) fn heard_overlap(narration: &str, heard: &str) -> f64 { /// renders the moment and the Persona decides; a menu of choices is the Projector deciding. A lexical flag on a narrow /// form, reported as a metric, never a filter. pub(crate) fn offers_options(narration: &str) -> bool { - let lower = format!(" {} ", narration.to_lowercase().replace('\u{2019}', "'")); + let lower = format!(" {} ", narration.chars().map(super::span::fold).collect::().to_lowercase()); ["i may ", "i could ", "i might ", "she may ", "she could ", "she might ", "nothing asks me", "nothing asks her", "or i could", "or she could"] .iter() .any(|form| lower.contains(&format!(" {form}")) || lower.contains(&format!(". {form}")) || lower.contains(&format!(", {form}"))) diff --git a/crates/mind-body/src/projection/span.rs b/crates/mind-body/src/projection/span.rs index f468f91..d7ec729 100644 --- a/crates/mind-body/src/projection/span.rs +++ b/crates/mind-body/src/projection/span.rs @@ -20,8 +20,9 @@ pub(crate) struct Span { } /// The one char typographic punctuation is compared as: curly and modifier apostrophes as `'`, curly double quotes as -/// `"`, en and em dashes as `-`, a no-break space as a space. -fn fold(c: char) -> char { +/// `"`, en and em dashes as `-`, a no-break space as a space. The one fold table of `projection/` (map 16.31.4, K52): +/// every comparison of her words with anyone's reads typographic punctuation as the locator does. +pub(crate) fn fold(c: char) -> char { match c { '\u{2018}' | '\u{2019}' | '\u{02BC}' => '\'', '\u{201C}' | '\u{201D}' => '"', @@ -31,6 +32,14 @@ fn fold(c: char) -> char { } } +/// The words of `text` as every comparison of her words with anyone's reads them (map 16.31.4, K52): folded, +/// lowercased, split at every char that is neither alphanumeric nor `'`, the empty pieces dropped, so a curly +/// apostrophe joins a word as a straight one does. The one word split for likeness: `narrate::words` counts length. +pub(crate) fn tokens(text: &str) -> impl Iterator { + let folded: String = text.chars().map(fold).collect::().to_lowercase(); + folded.split(|c: char| !c.is_alphanumeric() && c != '\'').filter(|word| !word.is_empty()).map(str::to_owned).collect::>().into_iter() +} + /// `text` folded, with the byte offset in `text` of each char of the folded text, and `text.len()` last. fn folded(text: &str) -> (String, Vec, Vec) { let mut out = String::with_capacity(text.len()); diff --git a/crates/mind-body/src/projection/tests.rs b/crates/mind-body/src/projection/tests.rs index f7dcaad..f19d53f 100644 --- a/crates/mind-body/src/projection/tests.rs +++ b/crates/mind-body/src/projection/tests.rs @@ -2754,7 +2754,7 @@ fn k25_her_domain_is_in_her_view_whole_in_a_stated_window_that_shrinks_and_never /// Whether any run of three words of `text` is in `other`: words as the echo check reads them, lowercase. fn shares_a_run(text: &str, other: &str) -> bool { - let tokens = |text: &str| -> Vec { text.split(|c: char| !c.is_alphanumeric() && c != '\'').filter(|word| !word.is_empty()).map(str::to_lowercase).collect() }; + let tokens = |text: &str| -> Vec { super::span::tokens(text).collect() }; let theirs: BTreeSet> = tokens(other).windows(3).map(<[String]>::to_vec).collect(); tokens(text).windows(3).any(|run| theirs.contains(run)) }