Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/word-prediction.md
Original file line number Diff line number Diff line change
Expand Up @@ -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”. 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. 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. 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. 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.

Expand Down
24 changes: 21 additions & 3 deletions src-tauri/src/prediction/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,16 +227,17 @@ const NAME_SHARE: f32 = 0.9;
/// initial capital mid-sentence that the model clearly prefers, which it only
/// does for a proper noun. At a sentence start the model capitalises every
/// word, so the lowercase form wins there and the sentence rule decides. The
/// pronoun I is always capital, and the typed apostrophe style is kept.
/// pronoun I and its contractions are always capital, and the typed
/// apostrophe style is kept.
fn display(key: &str, f: &Finished, typed: &str, sentence_start: bool) -> String {
let spelling = &f.spelling;
let all_upper = spelling.chars().all(|c| c.is_ascii_uppercase());
let mixed = spelling.chars().skip(1).any(|c| c.is_ascii_uppercase());
let name = spelling.chars().next().is_some_and(char::is_uppercase)
&& !sentence_start
&& f.capital >= NAME_SHARE * f.mass;
let word = if key == "i" {
"I".to_owned()
let word = if key == "i" || key.starts_with("i'") {
format!("I{}", &key[1..])
} else if !all_upper && (mixed || name) {
spelling.replace('’', "'")
} else {
Expand Down Expand Up @@ -784,6 +785,22 @@ mod tests {
assert_eq!(recent(&format!("{} ", "x".repeat(300))), None);
}

#[test]
fn i_and_its_contractions_are_always_capital() {
let finished = |spelling: &str| Finished {
mass: 1.0,
capital: 0.0,
best: 0.0,
spelling: spelling.to_owned(),
pieces: Vec::new(),
logp: 0.0,
};
assert_eq!(display("i", &finished("i"), "", false), "I");
assert_eq!(display("i'm", &finished("i'm"), "", true), "I'm");
assert_eq!(display("i'll", &finished("i'll"), "i’", false), "I’ll");
assert_eq!(display("is", &finished("is"), "", false), "is");
}

#[test]
fn apostrophes_continue_words_rather_than_ending_them() {
let pieces = [" don", "’t", "'s", "’", " .", ","]
Expand All @@ -810,6 +827,7 @@ mod tests {
("Can you send a ", "wh", None),
("I live in ", "lon", Some("London")),
("See you on ", "mon", Some("Monday")),
("Tomorrow ", "i'", Some("I'm")),
("I want to ", "fa", None),
] {
let t = Instant::now();
Expand Down
Loading