diff --git a/docs/word-prediction.md b/docs/word-prediction.md index 90609926..6f9d3549 100644 --- a/docs/word-prediction.md +++ b/docs/word-prediction.md @@ -14,7 +14,7 @@ One Word prediction setting controls an offline SmolLM2-135M int8 ONNX model. Th For a first typed prefix, a fixed local context keeps the model from favoring website names at the start of a document; it adds no user text. The model loads in the worker while keyboard input remains available. Suggestions are blank until loading finishes. A passive badge beside the scan prompt distinguishes loading, a ready keyboard awaiting typed context, no matching suggestions, available suggestions, paused activity tracking, and prediction failure. It never shows typed text and is not a scan target. If loading or inference fails, or a call exceeds 1.5 seconds, the keyboard continues accepting input and offers **Retry predictions** in its toolbar. Retry restarts only the prediction worker, clears its private text context, and leaves the keyboard open; type a new prefix afterward. A 400 ms search budget bounds candidate exploration. A clipped buffer with no complete earlier word yields no suggestions. -Candidates contain ASCII letters and apostrophes and must have sufficient model probability. There is no vocabulary filter: any word the model finds likely can be suggested, including swearing, because the person typing chose it. Single-letter candidates are limited to “a” and “I”. The pronoun I and its contractions, such as I’m and I’ll, are completed with a capital I; letters already typed stay as typed. Up to five suggestions are shown: the first, third and fifth are the most likely single words, and the second and fourth are the two most likely two-word phrases that begin with one of the top three words, ranked by the probability of the pair. When fewer phrases are found within an extra 200 ms, single words fill the remaining slots, and the other way round. Accepting a phrase inserts the rest of its first word, a space, the second word and a trailing space. Ordinary words use lowercase. Names keep the model's capital: mixed case anywhere, such as WhatsApp, or an initial capital mid-sentence that the model clearly prefers, such as London or Monday, which it does for proper nouns and occasionally a rare word. At a sentence start the model capitalises every word, so the lowercase form wins there and the sentence rule decides. Casing is applied when the keyboard displays a candidate, and the label shows exactly what will be inserted. Caps, or Shift locked, uppercases the whole completion, and together they cancel as they do on typed letters. Before any letter of the word is typed, Shift once changes only the first letter, the way it would change the next typed letter, and a sentence start capitalises it without any modifier. The keyboard alone decides what a sentence start is: a full stop, exclamation or question mark it typed, not yet followed by a letter and not cancelled by pressing Shift. Punctuation the worker merely sees in its buffer, or the bare start of that buffer after a reset, does not capitalise anything, so a suggestion never disagrees with the keyboard's own automatic Shift. Shift once under Caps gives a lowercase first letter there, as it would on a typed letter. After typed letters, a pending Shift once is left for the next letter and does not touch the completion. +Candidates contain ASCII letters and apostrophes and must have sufficient model probability. There is no vocabulary filter: any word the model finds likely can be suggested, including swearing, because the person typing chose it. Single-letter candidates are limited to “a” and “I”. The pronoun I and its contractions, such as I’m and I’ll, are completed with a capital I; letters already typed stay as typed. Up to five suggestions are shown: the first, third and fifth are the most likely single words, and the second and fourth are the two most likely two-word phrases that begin with one of the top three words, ranked by the probability of the pair. When fewer phrases are found within an extra 200 ms, single words fill the remaining slots, and the other way round. Accepting a phrase inserts the rest of its first word, a space, the second word and a trailing space. Ordinary words use lowercase. Names keep the model's capital: mixed case anywhere, such as WhatsApp, or an initial capital mid-sentence that the model clearly prefers, such as London or Monday, which it does for proper nouns and occasionally a rare word. At a sentence start the model capitalises every word, so the lowercase form wins there and the sentence rule decides. Casing is applied when the keyboard displays a candidate, and the label shows exactly what will be inserted. A typed prefix of two or more letters, all capitals, is completed in capitals whatever the modifiers now say. Otherwise Caps, or Shift locked, uppercases the whole completion, and together they cancel as they do on typed letters. Before any letter of the word is typed, Shift once changes only the first letter, the way it would change the next typed letter, and a sentence start capitalises it without any modifier. The keyboard alone decides what a sentence start is: a full stop, exclamation or question mark it typed, not yet followed by a letter and not cancelled by pressing Shift. Punctuation the worker merely sees in its buffer, or the bare start of that buffer after a reset, does not capitalise anything, so a suggestion never disagrees with the keyboard's own automatic Shift. Shift once under Caps gives a lowercase first letter there, as it would on a typed letter. After typed letters, a pending Shift once is left for the next letter and does not touch the completion. The model files are too large to commit. `npm run prediction-model` downloads them from a pinned upstream revision and verifies their SHA-256. The Tauri dev and build commands and CI run it automatically. Run it once before `cargo test` or `cargo clippy` in a fresh checkout. diff --git a/src-tauri/src/prediction/worker.rs b/src-tauri/src/prediction/worker.rs index 9b386f2a..6f56d742 100644 --- a/src-tauri/src/prediction/worker.rs +++ b/src-tauri/src/prediction/worker.rs @@ -242,13 +242,17 @@ impl Engine { if word[..offset].to_lowercase() != ctx.prefix.to_lowercase() { continue; } - let suffix = cased( - &word[offset..], - ctx.prefix.is_empty(), - sentence_start, - shift, - caps, - ); + let suffix = if shouting(&ctx.prefix) { + word[offset..].to_uppercase() + } else { + cased( + &word[offset..], + ctx.prefix.is_empty(), + sentence_start, + shift, + caps, + ) + }; labels.push(ctx.prefix.clone() + &suffix); self.suffixes.push(suffix + " "); } @@ -317,6 +321,18 @@ impl Engine { } } } +/// A typed prefix of two or more letters, all capitals, is a word being +/// written in capitals: its completion continues that way whatever the +/// modifiers now say, so the label shows exactly what will be inserted. +fn shouting(prefix: &str) -> bool { + let mut letters = prefix.chars().filter(|c| c.is_alphabetic()); + let first = letters.next(); + let second = letters.next(); + first.is_some_and(char::is_uppercase) + && second.is_some_and(char::is_uppercase) + && letters.all(char::is_uppercase) +} + /// The case a suggestion's suffix is inserted in. Caps, or Shift locked, /// uppercases it all, and together they cancel like they do on typed letters. /// With nothing typed yet, Shift once flips the first character's case the @@ -582,6 +598,38 @@ mod tests { assert_ne!(upper.token, b.token); } #[test] + fn an_all_caps_prefix_continues_in_capitals_whatever_the_modifiers() { + let mut e = engine(); + e.query(vec![append("WA")], 1, Shift::Off, false, false) + .unwrap(); + let row = e.query(vec![], 1, Shift::Off, false, false).unwrap(); + assert_eq!(row.words[0], "WATER"); + assert_eq!(row.words[1], "WATER IS"); + assert_eq!( + e.query(vec![], 1, Shift::Locked, false, false) + .unwrap() + .words[0], + "WATER" + ); + assert_eq!( + e.query(vec![], 1, Shift::Off, true, false).unwrap().words[0], + "WATER" + ); + // One capital is a capitalised word, not a word in capitals. + e.query( + vec![edit(Edit::Reset), append("W")], + 2, + Shift::Off, + false, + false, + ) + .unwrap(); + assert_eq!( + e.query(vec![], 2, Shift::Off, false, false).unwrap().words[0], + "Water" + ); + } + #[test] fn the_keyboard_alone_decides_a_sentence_start() { let mut e = engine(); // Punctuation in the buffer is not enough: the keyboard may have seen