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”. 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.
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.

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
3 changes: 0 additions & 3 deletions src-tauri/src/prediction/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use unicode_segmentation::UnicodeSegmentation;

pub struct Context {
pub prefix: String,
pub sentence_start: bool,
/// Buffered text before `prefix`, without a clipped leading fragment.
pub before: String,
/// Clipping left no complete earlier word, so an empty `before` is not
Expand Down Expand Up @@ -40,7 +39,6 @@ fn extract_words(text: &str, clipped: bool) -> Context {
String::new()
};
Context {
sentence_start: tokens.is_empty() && (!clipped || boundary.is_some()),
prefix,
before: String::new(),
partial: false,
Expand All @@ -53,7 +51,6 @@ mod tests {
fn unicode_boundaries_and_clipped_words() {
let c = extract("Discard this. I can’t find cafe\u{301}", false);
assert_eq!(c.prefix, "cafe\u{301}");
assert!(extract("Done! ", false).sentence_start);
assert_eq!(
extract("Hi. I would like wa", false).before,
"Hi. I would like "
Expand Down
4 changes: 3 additions & 1 deletion src-tauri/src/prediction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ struct Service {
reset: bool,
accept: Option<(u64, usize)>,
accepting: bool,
case: Option<(worker::Shift, bool, bool)>,
case: Option<(worker::Shift, bool, bool, bool)>,
tracking: bool,
}
#[derive(Clone, Copy)]
Expand Down Expand Up @@ -414,6 +414,7 @@ pub fn poll(app: &AppHandle, keyboard: Option<&mut Keyboard>, enabled: bool, ign
keyboard.prediction_shift(),
keyboard.caps,
keyboard.modifiers[1..].iter().any(|m| *m != Modifier::Off),
keyboard.prediction_sentence_start(),
);
if s.case != Some(case) {
s.case = Some(case);
Expand Down Expand Up @@ -534,6 +535,7 @@ pub fn poll(app: &AppHandle, keyboard: Option<&mut Keyboard>, enabled: bool, ign
revision: s.edit_revision,
shift: keyboard.prediction_shift(),
caps: keyboard.caps,
sentence_start: keyboard.prediction_sentence_start(),
}
};
let now = Instant::now();
Expand Down
Loading
Loading