+ {/* docs/dictation.md §35: how the user's corrections get here, while no shortcut is set. */}
+ {learnHotkey == null && (
+
+
+ {t("dictionary.suggestions.hint")}
+
+
+
+ )}
diff --git a/apps/desktop/src/pages/dictionary/useSuggestions.ts b/apps/desktop/src/pages/dictionary/useSuggestions.ts
index e526ad3b..74ac0426 100644
--- a/apps/desktop/src/pages/dictionary/useSuggestions.ts
+++ b/apps/desktop/src/pages/dictionary/useSuggestions.ts
@@ -110,9 +110,10 @@ const INITIAL: State = {
busy: false,
};
-/** Whether a row passes `filter`. */
+/** Whether a row passes `filter`: 推荐 takes the pairs seen twice or more and those the user
+ * corrected themselves (docs/dictation.md §35). */
export function passes(row: Suggestion, filter: SuggestionsFilter): boolean {
- return filter === "all" || row.count >= SUGGESTION_REPEATED_MIN;
+ return filter === "all" || row.count >= SUGGESTION_REPEATED_MIN || row.from_edits > 0;
}
/** Whether a row with `action` is still to decide: never acted on, or the action did nothing. */
diff --git a/apps/desktop/src/pages/history/DetailParts.tsx b/apps/desktop/src/pages/history/DetailParts.tsx
index 0f18911c..0f101de1 100644
--- a/apps/desktop/src/pages/history/DetailParts.tsx
+++ b/apps/desktop/src/pages/history/DetailParts.tsx
@@ -1,5 +1,6 @@
// Parts of the history detail moved out of `History.tsx` unchanged: a voice edit's detail, the
-// dictionary corrections and rules that fired, and the timing bar.
+// dictionary corrections and rules that fired, and the timing bar; and the user's correction of a
+// take (docs/dictation.md §35).
import {
type EditRecord,
type HistoryEntry,
@@ -7,9 +8,10 @@ import {
type TFunction,
type VocabularyHit,
formatCount,
+ formatDateTime,
isImported,
} from "@voltip/shared";
-import { Lamp, type LampTone, useUiState } from "@voltip/ui";
+import { Button, Lamp, type LampTone, useBackend, useI18n, useUiState } from "@voltip/ui";
import { textChars } from "../../features/history/stats";
/** A voice edit's detail (docs/dictation.md §19.5): the instruction as sent to the LLM, the
@@ -239,3 +241,42 @@ export function TimingBar({ entry, t }: { entry: HistoryEntry; t: TFunction }) {
);
}
+
+/** What the user corrected the take to in the application it went to (docs/dictation.md §35): the
+ * corrected text, when and how it was read, and 移除, which forgets it (the learning suggestions
+ * then leave its pairs out). The take's own text stays what was inserted. */
+export function CorrectedDetail({ entry }: { entry: HistoryEntry }) {
+ const { backend } = useBackend();
+ const { t, locale } = useI18n();
+ const corrected = entry.corrected;
+ if (corrected === undefined) return null;
+ return (
+
{t("settings.hotkey.storage", { hotkey })}
diff --git a/apps/desktop/src/pages/settings/Settings.test.tsx b/apps/desktop/src/pages/settings/Settings.test.tsx
index 8875b875..0efaf360 100644
--- a/apps/desktop/src/pages/settings/Settings.test.tsx
+++ b/apps/desktop/src/pages/settings/Settings.test.tsx
@@ -1042,6 +1042,63 @@ describe("Settings · 热键", () => {
expect(screen.getByTestId("copy-last-hotkey-status")).toHaveTextContent("未设置");
});
+ it("docs/dictation.md section 35: 从选中文字学习 has its own recorder, none by default, and refuses the other three chords", async () => {
+ const user = userEvent.setup();
+ const { backend } = renderApp({ path: "/settings/hotkey" });
+ const learn = await screen.findByTestId("learn-hotkey-recorder");
+ const copyLast = screen.getByTestId("copy-last-hotkey-recorder");
+ expect(screen.getByText("从选中文字学习")).toBeInTheDocument();
+ expect(screen.getByText(/选中改好的文字按这个快捷键/)).toBeInTheDocument();
+ expect(screen.getByTestId("learn-hotkey-status")).toHaveTextContent("未设置");
+ expect(screen.queryByRole("button", { name: "清除 · 从选中文字学习" })).toBeNull();
+ // One recorder at a time.
+ await user.click(screen.getByRole("button", { name: "录制 · 复制上一条结果" }));
+ expect(copyLast).toHaveAttribute("data-recording", "true");
+ await user.click(screen.getByRole("button", { name: "录制 · 从选中文字学习" }));
+ expect(copyLast).toHaveAttribute("data-recording", "false");
+ expect(learn).toHaveAttribute("data-recording", "true");
+ await user.keyboard("{Control>}{Alt>}{l>}");
+ await user.keyboard("{/l}{/Alt}{/Control}");
+ await waitFor(() => {
+ expect(backend.peek().settings.learn_hotkey).toBe("Ctrl+Alt+L");
+ });
+ expect(screen.getByTestId("learn-hotkey-status")).toHaveTextContent("已保存 · 快捷键已生效");
+ // The voice edit's chord is refused with the core's reason; the chord stays.
+ await user.click(screen.getByRole("button", { name: "重新录制 · 从选中文字学习" }));
+ await user.keyboard("{Control>}{Alt>}{e>}{/e}{/Alt}{/Control}");
+ await waitFor(() => {
+ expect(
+ backend.log.some(
+ (e) =>
+ e.type === "error" &&
+ e.message === "learn_hotkey: Ctrl+Alt+E 已用作「编辑选中文本」的快捷键",
+ ),
+ ).toBe(true);
+ });
+ expect(backend.peek().settings.learn_hotkey).toBe("Ctrl+Alt+L");
+ // The shell could not register it: the row says so and the banner offers a new chord.
+ act(() => {
+ backend.publish({
+ type: "hotkey",
+ registered: DEFAULT_HOTKEY,
+ learn_error: "Ctrl+Alt+L 与另一个快捷键相同,未生效",
+ pressed: false,
+ capturing: false,
+ backend: "global-shortcut · Windows · RegisterHotKey",
+ });
+ });
+ expect(await screen.findByText("Ctrl+Alt+L 与另一个快捷键相同,未生效")).toBeInTheDocument();
+ expect(screen.getByTestId("learn-hotkey-status")).toHaveTextContent("已保存 · 未能生效");
+ await user.click(screen.getByRole("button", { name: "更换快捷键" }));
+ expect(learn).toHaveAttribute("data-recording", "true");
+ await user.keyboard("{Escape}");
+ await user.click(screen.getByRole("button", { name: "清除 · 从选中文字学习" }));
+ await waitFor(() => {
+ expect(backend.peek().settings).not.toHaveProperty("learn_hotkey");
+ });
+ expect(screen.getByTestId("learn-hotkey-status")).toHaveTextContent("未设置");
+ });
+
it("regression: without a refine key the edit row says it needs the AI refine service (section 19)", async () => {
const backend = new MockBackend({
settings: { engines: { ...defaultEngineSettings(), llm_provider: "groq" } },
diff --git a/apps/desktop/src/shell/Shell.tsx b/apps/desktop/src/shell/Shell.tsx
index 4355b186..e104ddf2 100644
--- a/apps/desktop/src/shell/Shell.tsx
+++ b/apps/desktop/src/shell/Shell.tsx
@@ -267,6 +267,14 @@ export function Shell({
navigate({ name: "history" });
return;
}
+ if (request === "suggestions") {
+ navigate({ name: "dictionary", view: "suggestions" });
+ return;
+ }
+ if (request === "privacy") {
+ navigate({ name: "settings", section: "privacy" });
+ return;
+ }
shell.setUpdateOpen(true);
if (updateState === "idle" || updateState === "up_to_date" || updateState === "failed")
void backend.invoke("update_check");
diff --git a/apps/mobile-rn/rust/tests/shell.rs b/apps/mobile-rn/rust/tests/shell.rs
index a7c0e5e3..19bfc7b2 100644
--- a/apps/mobile-rn/rust/tests/shell.rs
+++ b/apps/mobile-rn/rust/tests/shell.rs
@@ -54,6 +54,14 @@ const DESKTOP_ONLY: &[&str] = &[
// The dictionary's learning suggestions (docs/dictation.md §16.10) are the desktop's.
"vocabulary_suggestion_ignore",
"vocabulary_suggestion_unignore",
+ // 从选中文字学习 (docs/dictation.md §35): the desktop's hotkey, pill and history detail.
+ "settings_set_learn_hotkey",
+ "learn_notice_accept",
+ "learn_notice_undo",
+ "learn_notice_dismiss",
+ "history_clear_corrected",
+ "open_suggestions",
+ "open_privacy",
// The automatic backups (docs/dictation.md §4.6.1) are the desktop's.
"settings_set_backup",
"backup_folder_choose",
diff --git a/apps/mobile/src-tauri/tests/ipc.rs b/apps/mobile/src-tauri/tests/ipc.rs
index 3fdd1e79..8e7c749e 100644
--- a/apps/mobile/src-tauri/tests/ipc.rs
+++ b/apps/mobile/src-tauri/tests/ipc.rs
@@ -817,6 +817,14 @@ const DESKTOP_ONLY: &[&str] = &[
"vocabulary_suggestions_ignore",
"vocabulary_suggestions_undo",
"rules_warnings",
+ // 从选中文字学习 (docs/dictation.md §35): the desktop's hotkey, pill and history detail.
+ "settings_set_learn_hotkey",
+ "learn_notice_accept",
+ "learn_notice_undo",
+ "learn_notice_dismiss",
+ "history_clear_corrected",
+ "open_suggestions",
+ "open_privacy",
// The automatic backups (docs/dictation.md §4.6.1).
"settings_set_backup",
"backup_folder_choose",
diff --git a/apps/mobile/src/App.test.tsx b/apps/mobile/src/App.test.tsx
index de8d5825..088b49fd 100644
--- a/apps/mobile/src/App.test.tsx
+++ b/apps/mobile/src/App.test.tsx
@@ -272,6 +272,8 @@ describe("Mobile app flow", () => {
overlayCopy: inner.overlayCopy.bind(inner),
overlayPaste: inner.overlayPaste.bind(inner),
openHistory: inner.openHistory.bind(inner),
+ openSuggestions: inner.openSuggestions.bind(inner),
+ openPrivacy: inner.openPrivacy.bind(inner),
providerConsoleOpen: inner.providerConsoleOpen.bind(inner),
projectLinkOpen: inner.projectLinkOpen.bind(inner),
guideOpen: inner.guideOpen.bind(inner),
@@ -341,6 +343,8 @@ describe("Mobile app flow", () => {
overlayCopy: identityless.overlayCopy.bind(identityless),
overlayPaste: identityless.overlayPaste.bind(identityless),
openHistory: identityless.openHistory.bind(identityless),
+ openSuggestions: identityless.openSuggestions.bind(identityless),
+ openPrivacy: identityless.openPrivacy.bind(identityless),
providerConsoleOpen: identityless.providerConsoleOpen.bind(identityless),
projectLinkOpen: identityless.projectLinkOpen.bind(identityless),
guideOpen: identityless.guideOpen.bind(identityless),
diff --git a/crates/voltip-core/src/dictation/engine.rs b/crates/voltip-core/src/dictation/engine.rs
index 15f26c01..4dc4857a 100644
--- a/crates/voltip-core/src/dictation/engine.rs
+++ b/crates/voltip-core/src/dictation/engine.rs
@@ -3748,6 +3748,7 @@ impl DictationEngine {
preset: (refined && self.status.kind == TakeKind::Dictation).then(|| self.take_preset().to_ref()),
origin: None,
processed: None,
+ corrected: None,
file: None,
timing: Some(timing),
glossary_sent,
diff --git a/crates/voltip-core/src/dictation/fakes.rs b/crates/voltip-core/src/dictation/fakes.rs
index 98d8c40e..0ae2f481 100644
--- a/crates/voltip-core/src/dictation/fakes.rs
+++ b/crates/voltip-core/src/dictation/fakes.rs
@@ -1763,6 +1763,7 @@ pub fn history_entry(text: &str) -> crate::HistoryEntry {
preset: None,
origin: None,
processed: None,
+ corrected: None,
file: None,
timing: None,
glossary_sent: None,
diff --git a/crates/voltip-core/src/history/derived.rs b/crates/voltip-core/src/history/derived.rs
index 851b3ef5..81248ca5 100644
--- a/crates/voltip-core/src/history/derived.rs
+++ b/crates/voltip-core/src/history/derived.rs
@@ -72,7 +72,8 @@ pub(super) const fn outcome_name(outcome: &Outcome) -> &'static str {
/// The text `history_query` searches: the fields the history page searched when it filtered in the
/// webview — a built-in scene's name in both languages, the text, the transcript, both models, the
/// application's name and id, the scene's name, the voice edit's instruction and selection — the
-/// processed text (docs/dictation.md §22) and an imported file's name (§31), each lowercased on its
+/// processed text (docs/dictation.md §22), the user's correction (§35) and an imported file's name
+/// (§31), each lowercased on its
/// own and joined by [`FIELD_SEPARATOR`].
pub(super) fn search_text(entry: &HistoryEntry) -> String {
let builtin = entry.scene.as_ref().and_then(|s| s.builtin);
@@ -89,6 +90,7 @@ pub(super) fn search_text(entry: &HistoryEntry) -> String {
entry.edit.as_ref().map(|e| e.instruction.as_str()),
entry.edit.as_ref().map(|e| e.selection.as_str()),
entry.processed.as_ref().map(|p| p.text.as_str()),
+ entry.corrected.as_ref().map(|c| c.text.as_str()),
entry.file.as_ref().map(|f| f.name.as_str()),
];
let mut out = String::new();
diff --git a/crates/voltip-core/src/history/mod.rs b/crates/voltip-core/src/history/mod.rs
index 65a60962..a902765a 100644
--- a/crates/voltip-core/src/history/mod.rs
+++ b/crates/voltip-core/src/history/mod.rs
@@ -189,6 +189,12 @@ pub struct HistoryEntry {
/// was. Absent until it ran. Boxed: most entries have none.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub processed: Option>,
+ /// What the user corrected the inserted text to in the application it went to, as 从选中文字学习
+ /// read it (docs/dictation.md §35): the whole take with the user's wording where they changed
+ /// it. The learning suggestions learn from it; `text` stays what was inserted. Absent until the
+ /// user taught it, and in entries written before it. Boxed: most entries have none.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub corrected: Option>,
/// The audio or video file the entry was transcribed from (docs/dictation.md §31); absent for
/// takes and in entries written before it. Such an entry is no dictation of the user's: it
/// counts toward no statistics and teaches the dictionary nothing.
@@ -226,6 +232,26 @@ pub struct ProcessedText {
pub at_ms: u64,
}
+/// The user's correction of a take (docs/dictation.md §35): the take's whole text as they left it,
+/// when it was read, and how.
+#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
+pub struct CorrectedText {
+ /// The take's text with the user's wording.
+ pub text: String,
+ /// Unix time in milliseconds.
+ pub at_ms: u64,
+ /// How Voltip read it.
+ pub via: CorrectionVia,
+}
+
+/// How a correction was read (docs/dictation.md §35).
+#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize)]
+#[serde(rename_all = "snake_case")]
+pub enum CorrectionVia {
+ /// The user selected the corrected text and asked Voltip to learn from it (从选中文字学习).
+ Selection,
+}
+
/// The other device of an entry: on a computer the phone it came from (`take`, `typed`,
/// `clipboard`, `standalone`), on a phone the computer it went to (`sent`).
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
diff --git a/crates/voltip-core/src/history/reader.rs b/crates/voltip-core/src/history/reader.rs
index 1d98eb3e..ae9af31b 100644
--- a/crates/voltip-core/src/history/reader.rs
+++ b/crates/voltip-core/src/history/reader.rs
@@ -110,6 +110,9 @@ pub struct HistoryText {
pub refined: bool,
/// The corrections and rules that fired (`HistoryEntry.vocabulary`; empty when none did).
pub vocabulary: VocabularyHits,
+ /// The whole take as the user corrected it in the application it went to
+ /// (`HistoryEntry.corrected`, docs/dictation.md §35); `None` when they did not teach it.
+ pub corrected: Option,
}
/// [`HistoryReader::search`]: which entries, and where the page starts (docs/dictation.md §25).
@@ -293,7 +296,7 @@ impl HistoryReader {
let texts = self.read(|conn| {
let mut stmt = conn.prepare(&format!(
"SELECT id, at_ms, json_extract(json, '$.raw_text'), json_extract(json, '$.text'), json_extract(json, '$.refined'), \
- json_extract(json, '$.vocabulary') FROM entries \
+ json_extract(json, '$.vocabulary'), json_extract(json, '$.corrected.text') FROM entries \
WHERE kind = 'dictation' AND COALESCE(json_extract(json, '$.origin.kind'), '') NOT IN ('typed', 'clipboard') \
AND json_extract(json, '$.file') IS NULL {NEWEST_FIRST} LIMIT ?1"
))?;
@@ -305,11 +308,12 @@ impl HistoryReader {
row.get::<_, Option>(3)?,
row.get::<_, Option>(4)?,
row.get::<_, Option>(5)?,
+ row.get::<_, Option>(6)?,
))
})?;
let mut out = Vec::new();
for row in rows {
- let (id, at_ms, raw_text, text, refined, vocabulary) = row?;
+ let (id, at_ms, raw_text, text, refined, vocabulary, corrected) = row?;
let Ok(id) = id.parse::() else { continue };
out.push(HistoryText {
id,
@@ -318,6 +322,7 @@ impl HistoryReader {
text: text.unwrap_or_default(),
refined: refined == Some(1),
vocabulary: vocabulary.and_then(|v| serde_json::from_str(&v).ok()).unwrap_or_default(),
+ corrected,
});
}
Ok(out)
diff --git a/crates/voltip-core/src/history/revs.rs b/crates/voltip-core/src/history/revs.rs
index c71e8afb..70129edc 100644
--- a/crates/voltip-core/src/history/revs.rs
+++ b/crates/voltip-core/src/history/revs.rs
@@ -287,6 +287,7 @@ fn shallow(entry: &HistoryEntry) -> HistoryEntry {
preset: None,
origin: None,
processed: None,
+ corrected: None,
file: None,
timing: entry.timing,
glossary_sent: entry.glossary_sent,
diff --git a/crates/voltip-core/src/history/store.rs b/crates/voltip-core/src/history/store.rs
index ebd7a98e..ca041c13 100644
--- a/crates/voltip-core/src/history/store.rs
+++ b/crates/voltip-core/src/history/store.rs
@@ -23,7 +23,7 @@ use sha2::{Digest, Sha256};
use uuid::Uuid;
use super::revs::{self, ChangeBatch, Outbox, RECEIVED_KEEP_MS};
-use super::{HISTORY_DB_FILE_NAME, HISTORY_FILE_NAME, HISTORY_SCHEMA, HistoryEntry, MAX_ENTRIES, ProcessedText, derived};
+use super::{CorrectedText, HISTORY_DB_FILE_NAME, HISTORY_FILE_NAME, HISTORY_SCHEMA, HistoryEntry, MAX_ENTRIES, ProcessedText, derived};
use crate::CoreError;
/// A database the import is still writing (step 2); deleted when found at start (step 1).
@@ -321,6 +321,22 @@ impl HistoryStore {
Ok(true)
}
+ /// Keep the user's correction of entry `id` (docs/dictation.md §35), replacing an earlier one,
+ /// or clear it (`None`); the search finds its text too. `Ok(false)` when the id is unknown.
+ pub fn set_corrected(&mut self, id: Uuid, corrected: Option) -> Result {
+ let conn = self.conn_mut()?;
+ let key = id.to_string();
+ let json: Option = conn.query_row("SELECT json FROM entries WHERE id = ?1", [&key], |row| row.get(0)).optional().map_err(err)?;
+ let Some(mut entry) = json.as_deref().and_then(decode) else { return Ok(false) };
+ entry.corrected = corrected.map(Box::new);
+ let json = serde_json::to_string(&entry).map_err(err)?;
+ let tx = conn.transaction().map_err(err)?;
+ tx.execute("UPDATE entries SET search = ?2, json = ?3 WHERE id = ?1", params![key, derived::search_text(&entry), json]).map_err(err)?;
+ revs::record(&tx, &key, false).map_err(err)?;
+ tx.commit().map_err(err)?;
+ Ok(true)
+ }
+
fn conn_mut(&mut self) -> Result<&mut Connection, CoreError> {
let path = &self.path;
self.conn.as_mut().map(Mutex::get_mut).ok_or_else(|| CoreError::History(format!("{} cannot be opened", path.display())))
diff --git a/crates/voltip-core/src/history/tests.rs b/crates/voltip-core/src/history/tests.rs
index 213c4907..0d38d41a 100644
--- a/crates/voltip-core/src/history/tests.rs
+++ b/crates/voltip-core/src/history/tests.rs
@@ -41,6 +41,7 @@ pub(super) fn entry(text: &str) -> HistoryEntry {
preset: None,
origin: None,
processed: None,
+ corrected: None,
file: None,
timing: None,
glossary_sent: None,
@@ -769,6 +770,35 @@ fn twenty_thousand_entries_open_search_and_count_in_time() {
assert!(counted < Duration::from_millis(100), "stats {counted:?}");
}
+/// 从选中文字学习 (docs/dictation.md §35): the user's correction is stored beside the entry's own
+/// text (which stays what was inserted), replaces an earlier one, is searched, read back by
+/// `recent_texts`, cleared with `None`, and absent from the JSON until it exists; an unknown id
+/// stores nothing.
+#[test]
+fn a_correction_is_stored_beside_the_entry_searched_and_read_with_the_recent_texts() {
+ let dir = tempfile::tempdir().unwrap();
+ let mut store = HistoryStore::open(dir.path());
+ let take = entry("我在用 Quinn for max 做测试");
+ store.push(take.clone(), MAX_ENTRIES).unwrap();
+ assert!(!serde_json::to_string(&take).unwrap().contains("corrected"));
+ let corrected = |text: &str, at_ms| CorrectedText { text: text.into(), at_ms, via: CorrectionVia::Selection };
+ assert!(store.set_corrected(take.id, Some(corrected("我在用 Qwen max 做测试", 1))).unwrap());
+ assert!(store.set_corrected(take.id, Some(corrected("我在用 Qwen 4 max 做测试", 2))).unwrap(), "replaces the earlier one");
+ assert!(!store.set_corrected(Uuid::new_v4(), Some(corrected("x", 3))).unwrap());
+ let stored = HistoryStore::open(dir.path()).get(take.id).unwrap().unwrap();
+ assert_eq!(stored.text, take.text, "the inserted text stays");
+ assert_eq!(stored.corrected, Some(Box::new(corrected("我在用 Qwen 4 max 做测试", 2))));
+ let json = serde_json::to_value(&stored).unwrap();
+ assert_eq!(json["corrected"], serde_json::json!({ "text": "我在用 Qwen 4 max 做测试", "at_ms": 2, "via": "selection" }));
+ let reader = HistoryReader::new(dir.path());
+ assert_eq!(reader.query(&page(HistoryQuery { query: "qwen 4".into(), ..Default::default() })).unwrap().entries.len(), 1);
+ assert_eq!(reader.recent_texts(10).unwrap()[0].corrected.as_deref(), Some("我在用 Qwen 4 max 做测试"));
+ assert!(store.set_corrected(take.id, None).unwrap());
+ assert_eq!(store.get(take.id).unwrap().unwrap().corrected, None);
+ assert_eq!(reader.recent_texts(10).unwrap()[0].corrected, None);
+ assert!(reader.query(&page(HistoryQuery { query: "qwen 4".into(), ..Default::default() })).unwrap().entries.is_empty(), "cleared: not searched");
+}
+
/// 用 AI 预设处理 (docs/dictation.md §22): the result is stored beside the entry's own text (the
/// text is untouched), replaces an earlier one, is found by the search, survives a reopen and is
/// omitted from the JSON until it exists; an unknown id stores nothing.
@@ -1174,7 +1204,13 @@ fn recent_texts_are_the_newest_recognised_takes() {
assert_eq!(
texts[0],
HistoryText {
- id: rows[5].id, at_ms: base + 5, raw_text: "最新".into(), text: "最新。".into(), refined: true, vocabulary: VocabularyHits::default()
+ id: rows[5].id,
+ at_ms: base + 5,
+ raw_text: "最新".into(),
+ text: "最新。".into(),
+ refined: true,
+ vocabulary: VocabularyHits::default(),
+ corrected: None
}
);
// Whether the polish made the text, and what fired: what the correction summary leaves out (§25).
diff --git a/crates/voltip-core/src/import/run.rs b/crates/voltip-core/src/import/run.rs
index 40fa8f63..a1b22476 100644
--- a/crates/voltip-core/src/import/run.rs
+++ b/crates/voltip-core/src/import/run.rs
@@ -297,6 +297,7 @@ pub async fn run(job: Job, mut partial: Partial, report: Report) -> Outcome {
preset: refined.then(|| TakePreset::default().to_ref()),
origin: None,
processed: None,
+ corrected: None,
file: Some(ImportedFile { name, duration_ms }),
timing: None,
// docs/dictation.md §30.6: as a long take's, when any segment's request carried it.
diff --git a/crates/voltip-core/src/lib.rs b/crates/voltip-core/src/lib.rs
index b8cafc84..9768c0a6 100644
--- a/crates/voltip-core/src/lib.rs
+++ b/crates/voltip-core/src/lib.rs
@@ -66,8 +66,8 @@ pub use providers::{
key_env_var, load_user_secrets, merge_credentials, peek_user_secrets, stored_credential_set,
};
pub use runtime::{
- AUTO_WEEKLY_DELAY, AppCore, CoreCommand, CoreConfig, CoreEvent, CoreHandle, EXTRA_RECORDING_POLL, MAX_ACTIVATION_MS, PRESET_TRY_UNCONFIGURED,
- SERVE_UNAVAILABLE, TestHooks, now_ms,
+ AUTO_WEEKLY_DELAY, AppCore, CoreCommand, CoreConfig, CoreEvent, CoreHandle, EXTRA_RECORDING_POLL, LEARN_NOTICE_DWELL, LearnNotice, LearnOutcome,
+ MAX_ACTIVATION_MS, NoticeAccepted, PRESET_TRY_UNCONFIGURED, SERVE_UNAVAILABLE, TestHooks, now_ms,
};
pub use scenes::{AppRef, BuiltinScene, ContextSharing, Scene, SceneDraft, SceneError, SceneMatch, SceneOverrides, SceneRef, TakeContext};
pub use settings::{
diff --git a/crates/voltip-core/src/mcp/candidates.rs b/crates/voltip-core/src/mcp/candidates.rs
index 80c80c69..01b3e3cf 100644
--- a/crates/voltip-core/src/mcp/candidates.rs
+++ b/crates/voltip-core/src/mcp/candidates.rs
@@ -101,6 +101,12 @@ pub fn diff_tokens(text: &str) -> Vec<&str> {
/// `diffSegments`: the token-level LCS of `raw` → `text`, runs of one kind merged; ties go to the
/// deletion, as in `refine.ts`. `None` when the table would pass [`MAX_DIFF_CELLS`].
pub fn diff_segments(raw: &str, text: &str) -> Option> {
+ diff_segments_by(raw, text, |a, b| a == b)
+}
+
+/// [`diff_segments`] with `same` deciding which tokens are equal; a `Same` segment holds `raw`'s
+/// tokens.
+pub(crate) fn diff_segments_by(raw: &str, text: &str, same: impl Fn(&str, &str) -> bool) -> Option> {
let a = diff_tokens(raw);
let b = diff_tokens(text);
let width = b.len() + 1;
@@ -111,7 +117,7 @@ pub fn diff_segments(raw: &str, text: &str) -> Option> {
let mut table = vec![0u32; (a.len() + 1) * width];
for i in (0..a.len()).rev() {
for j in (0..b.len()).rev() {
- table[i * width + j] = if a[i] == b[j] { table[(i + 1) * width + j + 1] + 1 } else { table[(i + 1) * width + j].max(table[i * width + j + 1]) };
+ table[i * width + j] = if same(a[i], b[j]) { table[(i + 1) * width + j + 1] + 1 } else { table[(i + 1) * width + j].max(table[i * width + j + 1]) };
}
}
let mut out: Vec = Vec::new();
@@ -121,7 +127,7 @@ pub fn diff_segments(raw: &str, text: &str) -> Option> {
};
let (mut i, mut j) = (0, 0);
while i < a.len() && j < b.len() {
- if a[i] == b[j] {
+ if same(a[i], b[j]) {
push(SegmentKind::Same, a[i]);
i += 1;
j += 1;
@@ -169,8 +175,8 @@ fn trim_marks(text: &str) -> &str {
text.trim_matches(|c: char| c.is_whitespace() || js_space(c) || is_mark(c))
}
-/// A replacement inside one take: what was heard, what the final text has, and where the heard
-/// run starts in the recognised text (in characters).
+/// A replacement inside one take: what was heard, what the final text has, where the heard run
+/// starts in the recognised text, and where the two trimmed forms sit (all in characters).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Replacement {
/// Trimmed recognised run.
@@ -181,6 +187,15 @@ pub struct Replacement {
pub at: usize,
/// Its length in characters.
pub len: usize,
+ /// Character offset of the trimmed `heard` in `raw`.
+ pub heard_at: usize,
+ /// Character offset of the trimmed `polished` in `text`.
+ pub polished_at: usize,
+}
+
+/// The characters [`trim_marks`] takes off the front of `text`.
+fn leading_marks(text: &str) -> usize {
+ text.chars().take_while(|&c| c.is_whitespace() || js_space(c) || is_mark(c)).count()
}
/// The replacements of one take: each deleted run directly followed by an added run, trimmed;
@@ -189,7 +204,7 @@ pub struct Replacement {
pub fn replacements(raw: &str, text: &str) -> Option> {
let segments = diff_segments(raw, text)?;
let mut out = Vec::new();
- let mut at = 0usize;
+ let (mut at, mut to) = (0usize, 0usize);
for (i, segment) in segments.iter().enumerate() {
let len = segment.text.chars().count();
if segment.kind == SegmentKind::Del
@@ -199,49 +214,303 @@ pub fn replacements(raw: &str, text: &str) -> Option> {
let (heard, polished) = (trim_marks(&segment.text), trim_marks(&next.text));
let fits = |s: &str| !s.is_empty() && s.chars().count() <= MAX_TERM_CHARS;
if fits(heard) && fits(polished) && heard != polished {
- out.push(Replacement { heard: heard.to_owned(), polished: polished.to_owned(), at, len });
+ out.push(Replacement {
+ heard: heard.to_owned(),
+ polished: polished.to_owned(),
+ at,
+ len,
+ heard_at: at + leading_marks(&segment.text),
+ polished_at: to + leading_marks(&next.text),
+ });
}
}
- if segment.kind != SegmentKind::Add {
- at += len;
+ match segment.kind {
+ SegmentKind::Same => {
+ at += len;
+ to += len;
+ }
+ SegmentKind::Del => at += len,
+ SegmentKind::Add => to += len,
}
}
Some(out)
}
-/// The replacements of `found` the AI clean-up made in `take`: those the take's own corrections and
-/// rules (`take.vocabulary`, in their order then, on even if turned off since) account for are left
-/// out — they turn the heard wording into the polished one, or make the same replacement at the
-/// same place when run over the whole recognised text. They are replayed as `dictionary` and
-/// `rules` define them now, so a take one of whose fired entries or rules was deleted or changed
-/// after it (`updated_at_ms` past the take's `at_ms`) gives no pairs at all.
-fn polish_only(found: Vec, take: &HistoryText, dictionary: &[DictionaryEntry], rules: &[ReplacementRule]) -> Vec {
- let fired = &take.vocabulary;
- if found.is_empty() || fired.is_empty() {
- return found;
+/// A take's own corrections and rules (`take.vocabulary`, in their order then, on even if turned
+/// off since) replayed over its recognised text as `dictionary` and `rules` define them now
+/// (docs/dictation.md §16.9): what they turn a wording into, and where they changed the
+/// recognised text. [`Replay::of`] is `None` when one of them was deleted or changed after the take
+/// (`updated_at_ms` past the take's `at_ms`): their changes can no longer be told apart.
+pub(crate) struct Replay {
+ /// The fired entries and rules; `None` when nothing fired.
+ vocabulary: Option,
+ /// Their replacements over the whole recognised text.
+ explained: Vec,
+}
+
+impl Replay {
+ /// The replay of `take`'s fired vocabulary, or `None` when it changed since the take.
+ pub(crate) fn of(take: &HistoryText, dictionary: &[DictionaryEntry], rules: &[ReplacementRule]) -> Option {
+ let fired = &take.vocabulary;
+ if fired.is_empty() {
+ return Some(Self { vocabulary: None, explained: Vec::new() });
+ }
+ let mut entries = Vec::with_capacity(fired.corrections.len());
+ for hit in &fired.corrections {
+ match dictionary.iter().find(|e| e.id == hit.id) {
+ Some(e) if e.updated_at_ms <= take.at_ms => entries.push(DictionaryEntry { enabled: true, ..e.clone() }),
+ _ => return None,
+ }
+ }
+ let mut firing = Vec::with_capacity(fired.rules.len());
+ for hit in &fired.rules {
+ match rules.iter().find(|r| r.id == hit.id) {
+ Some(r) if r.updated_at_ms <= take.at_ms => firing.push(ReplacementRule { enabled: true, pending: false, ..r.clone() }),
+ _ => return None,
+ }
+ }
+ let mut replay = Self { vocabulary: Some(Vocabulary::compile(&entries, &firing)), explained: Vec::new() };
+ replay.explained = replacements(&take.raw_text, &replay.run(&take.raw_text)).unwrap_or_default();
+ Some(replay)
+ }
+
+ /// `text` through the fired corrections, then the fired rules.
+ pub(crate) fn run(&self, text: &str) -> String {
+ match &self.vocabulary {
+ None => text.to_owned(),
+ Some(vocabulary) => vocabulary.apply_rules(&vocabulary.correct(text).text).text,
+ }
+ }
+
+ /// The fired vocabulary accounts for `r`: it turns the heard wording into the polished one, or
+ /// makes the same replacement at the same place.
+ fn explains(&self, r: &Replacement) -> bool {
+ self.run(&r.heard) == r.polished
+ || self.explained.iter().any(|q| q.heard == r.heard && q.polished == r.polished && q.at < r.at + r.len && r.at < q.at + q.len)
+ }
+
+ /// One of the fired vocabulary's replacements overlaps `len` characters at `at` of the
+ /// recognised text.
+ pub(crate) fn touches(&self, at: usize, len: usize) -> bool {
+ self.explained.iter().any(|q| q.at < at + len && at < q.at + q.len)
+ }
+}
+
+/// One pair of one take before the counting: the take (an index into the history), the two forms,
+/// where they sit (characters into the recognised text, and into the text the right form comes
+/// from), and whether the user's own correction made it (docs/dictation.md §35) rather than the AI
+/// clean-up.
+#[derive(Clone, Debug, PartialEq, Eq)]
+pub(crate) struct Found {
+ pub(crate) take: usize,
+ pub(crate) heard: String,
+ pub(crate) right: String,
+ pub(crate) heard_at: usize,
+ pub(crate) right_at: usize,
+ pub(crate) edit: bool,
+}
+
+/// What [`scan`] read.
+#[derive(Clone, Debug, Default)]
+pub(crate) struct Scan {
+ /// The pairs, take by take, newest first.
+ pub(crate) found: Vec,
+ /// Takes looked at.
+ pub(crate) scanned: u32,
+ /// Of those, the takes left out as too long to compare.
+ pub(crate) skipped: u32,
+ /// Of those, the takes the user corrected (`HistoryText::corrected`).
+ pub(crate) edited: u32,
+ /// The takes compared, as indices into the history.
+ pub(crate) compared: Vec,
+}
+
+/// The diff table of two texts, in cells.
+fn cells_of(a: &str, b: &str) -> usize {
+ (diff_tokens(a).len() + 1).saturating_mul(diff_tokens(b).len() + 1)
+}
+
+/// The pairs of `history` (newest first) at or after `since_ms`: the AI clean-up's
+/// ([`Replay::explains`] leaves out what the take's own corrections and rules did) and, with
+/// `edits`, the user's corrections (`crate::vocabulary::learn`). A take whose diff would pass
+/// [`MAX_DIFF_CELLS`] is skipped; past [`MAX_SCAN_CELLS`] in all, older takes are not read.
+pub(crate) fn scan(dictionary: &[DictionaryEntry], rules: &[ReplacementRule], history: &[HistoryText], since_ms: Option, edits: bool) -> Scan {
+ let mut out = Scan::default();
+ let mut cells = 0usize;
+ for (index, take) in history.iter().enumerate().filter(|(_, t)| since_ms.is_none_or(|since| t.at_ms >= since)) {
+ let corrected = if edits { take.corrected.as_deref() } else { None };
+ if !take.refined && corrected.is_none() {
+ // Not polished and not corrected: every change in it is a correction's or a rule's.
+ out.scanned = out.scanned.saturating_add(1);
+ continue;
+ }
+ let polish = take.refined.then(|| cells_of(&take.raw_text, &take.text));
+ let edit = corrected.map(|c| (cells_of(&take.text, c), cells_of(&take.raw_text, &take.text)));
+ if polish.is_some_and(|size| size > MAX_DIFF_CELLS) || edit.is_some_and(|(a, b)| a > MAX_DIFF_CELLS || b > MAX_DIFF_CELLS) {
+ out.scanned = out.scanned.saturating_add(1);
+ out.skipped = out.skipped.saturating_add(1);
+ continue;
+ }
+ // A take whose corrections or rules fired is diffed once more for the replay (counted as
+ // the polish's diff again, as before the corrections were read).
+ let replay_cost = if take.vocabulary.is_empty() { 0 } else { polish.unwrap_or_else(|| cells_of(&take.raw_text, &take.raw_text)) };
+ let cost = polish.unwrap_or(0).saturating_add(edit.map_or(0, |(a, b)| a.saturating_add(b))).saturating_add(replay_cost);
+ if cells.saturating_add(cost) > MAX_SCAN_CELLS {
+ break;
+ }
+ cells += cost;
+ out.scanned = out.scanned.saturating_add(1);
+ out.compared.push(index);
+ if corrected.is_some() {
+ out.edited = out.edited.saturating_add(1);
+ }
+ let Some(replay) = Replay::of(take, dictionary, rules) else { continue };
+ if take.refined {
+ let Some(found) = replacements(&take.raw_text, &take.text) else {
+ out.skipped = out.skipped.saturating_add(1);
+ continue;
+ };
+ out.found.extend(found.into_iter().filter(|r| !replay.explains(r)).map(|r| Found {
+ take: index,
+ heard: r.heard,
+ right: r.polished,
+ heard_at: r.heard_at,
+ right_at: r.polished_at,
+ edit: false,
+ }));
+ }
+ if let Some(corrected) = corrected {
+ out.found.extend(crate::vocabulary::learn::edit_changes(take, corrected, &replay).into_iter().map(|c| Found {
+ take: index,
+ heard: c.heard,
+ right: c.right,
+ heard_at: c.heard_at,
+ right_at: c.right_at,
+ edit: true,
+ }));
+ }
}
- let mut entries = Vec::with_capacity(fired.corrections.len());
- for hit in &fired.corrections {
- match dictionary.iter().find(|e| e.id == hit.id) {
- Some(e) if e.updated_at_ms <= take.at_ms => entries.push(DictionaryEntry { enabled: true, ..e.clone() }),
- _ => return Vec::new(),
+ out
+}
+
+/// A character of the scripts written without spaces whose single characters the dictionary's
+/// literal matcher finds inside any word (Han, kana, Hangul).
+pub(crate) fn is_cjk_letter(c: char) -> bool {
+ c.is_alphanumeric()
+ && matches!(
+ c as u32,
+ 0x1100..=0x11FF
+ | 0x3005..=0x3007
+ | 0x3040..=0x30FF
+ | 0x3130..=0x318F
+ | 0x31F0..=0x31FF
+ | 0x3400..=0x4DBF
+ | 0x4E00..=0x9FFF
+ | 0xA960..=0xA97F
+ | 0xAC00..=0xD7FF
+ | 0xF900..=0xFAFF
+ | 0xFF66..=0xFF9F
+ | 0xFFA0..=0xFFDC
+ | 0x20000..=0x3134F
+ )
+}
+
+/// `text` is one CJK character.
+fn single_cjk(text: &str) -> bool {
+ let mut chars = text.chars();
+ chars.next().is_some_and(is_cjk_letter) && chars.next().is_none()
+}
+
+/// A pair as it is counted: widened when it was a single character (docs/dictation.md §16.10).
+#[derive(Clone, Debug, PartialEq, Eq)]
+pub(crate) struct Counted {
+ pub(crate) found: Found,
+ /// The pair before it was widened, when it was.
+ pub(crate) single: Option<(String, String)>,
+ /// Characters of the (widened) heard form.
+ pub(crate) heard_len: usize,
+}
+
+/// A way to widen one pair: the forms with the neighbour, where the heard form starts.
+struct Widening {
+ heard: String,
+ right: String,
+ heard_at: usize,
+ right_at: usize,
+}
+
+/// The pairs of `scan` with every pair whose heard or right form is a single CJK character
+/// widened by the same unchanged neighbour on both sides (docs/dictation.md §16.10): 非 → 飞 in
+/// 「非书」 becomes 非书 → 飞书. With a neighbour on both sides, the side whose widened right form
+/// appears more often in the compared takes' final texts and corrections wins, the right one on a
+/// tie; with none, the pair stays as it is.
+pub(crate) fn widen(history: &[HistoryText], scan: &Scan) -> Vec {
+ let chars = |text: &str| text.chars().collect::>();
+ let mut texts: HashMap<(usize, bool), (Vec, Vec)> = HashMap::new();
+ let mut options: Vec> = Vec::with_capacity(scan.found.len());
+ for f in &scan.found {
+ if !single_cjk(&f.heard) && !single_cjk(&f.right) {
+ options.push(Vec::new());
+ continue;
+ }
+ let take = &history[f.take];
+ let (raw, right) = texts.entry((f.take, f.edit)).or_insert_with(|| {
+ let right = if f.edit { take.corrected.as_deref().unwrap_or(&take.text) } else { &take.text };
+ (chars(&take.raw_text), chars(right))
+ });
+ let (heard_len, right_len) = (f.heard.chars().count(), f.right.chars().count());
+ let mut found = Vec::new();
+ // Right first: it wins a tie.
+ if let (Some(&a), Some(&b)) = (raw.get(f.heard_at + heard_len), right.get(f.right_at + right_len))
+ && a == b
+ && is_cjk_letter(a)
+ {
+ found.push(Widening { heard: format!("{}{a}", f.heard), right: format!("{}{a}", f.right), heard_at: f.heard_at, right_at: f.right_at });
+ }
+ if f.heard_at > 0
+ && f.right_at > 0
+ && let (Some(&a), Some(&b)) = (raw.get(f.heard_at - 1), right.get(f.right_at - 1))
+ && a == b
+ && is_cjk_letter(a)
+ {
+ found.push(Widening { heard: format!("{a}{}", f.heard), right: format!("{a}{}", f.right), heard_at: f.heard_at - 1, right_at: f.right_at - 1 });
}
+ options.push(found);
}
- let mut firing = Vec::with_capacity(fired.rules.len());
- for hit in &fired.rules {
- match rules.iter().find(|r| r.id == hit.id) {
- Some(r) if r.updated_at_ms <= take.at_ms => firing.push(ReplacementRule { enabled: true, pending: false, ..r.clone() }),
- _ => return Vec::new(),
+ let mut patterns: Vec = options.iter().filter(|o| o.len() > 1).flat_map(|o| o.iter().map(|w| w.right.clone())).collect();
+ patterns.sort();
+ patterns.dedup();
+ let mut counts: HashMap<&str, usize> = HashMap::new();
+ if !patterns.is_empty()
+ && let Ok(ac) = aho_corasick::AhoCorasick::new(&patterns)
+ {
+ for &index in &scan.compared {
+ let take = &history[index];
+ for text in std::iter::once(take.text.as_str()).chain(take.corrected.as_deref()) {
+ for m in ac.find_overlapping_iter(text) {
+ *counts.entry(patterns[m.pattern().as_usize()].as_str()).or_default() += 1;
+ }
+ }
}
}
- let vocabulary = Vocabulary::compile(&entries, &firing);
- let run = |text: &str| vocabulary.apply_rules(&vocabulary.correct(text).text).text;
- let explained = replacements(&take.raw_text, &run(&take.raw_text)).unwrap_or_default();
- found
- .into_iter()
- .filter(|r| {
- run(&r.heard) != r.polished
- && !explained.iter().any(|q| q.heard == r.heard && q.polished == r.polished && q.at < r.at + r.len && r.at < q.at + q.len)
+ scan.found
+ .iter()
+ .zip(options)
+ .map(|(f, found)| {
+ let count = |w: &Widening| counts.get(w.right.as_str()).copied().unwrap_or(0);
+ let chosen = match found.as_slice() {
+ [] => None,
+ [one] => Some(one),
+ [right, left, ..] => Some(if count(left) > count(right) { left } else { right }),
+ };
+ match chosen {
+ None => Counted { found: f.clone(), single: None, heard_len: f.heard.chars().count() },
+ Some(w) => Counted {
+ found: Found { heard: w.heard.clone(), right: w.right.clone(), heard_at: w.heard_at, right_at: w.right_at, ..f.clone() },
+ single: Some((f.heard.clone(), f.right.clone())),
+ heard_len: w.heard.chars().count(),
+ },
+ }
})
.collect()
}
@@ -266,10 +535,18 @@ pub struct Pair {
pub count: u32,
/// The newest take it happened in.
pub last_at_ms: u64,
- /// `heard` is already a form an enabled dictionary entry corrects (ignoring ASCII case).
+ /// `heard` is already a form an enabled dictionary entry corrects (ignoring ASCII case), or
+ /// its single character before the pair was widened is.
pub known: bool,
/// Up to [`MAX_EXAMPLES`] snippets, newest first.
pub examples: Vec,
+ /// Of `count`, the times the user's own correction made it (docs/dictation.md §35); never on
+ /// the MCP tool's wire, whose scan reads no corrections.
+ #[serde(skip)]
+ pub from_edits: u32,
+ /// The pair before it was widened (docs/dictation.md §16.10), when it was.
+ #[serde(skip)]
+ pub single: Option<(String, String)>,
}
/// What `vocabulary_candidates` answers.
@@ -292,67 +569,56 @@ fn snippet(raw: &str, at: usize, len: usize) -> String {
if start > 0 {
out.push('…');
}
- out.extend(&chars[start..end]);
+ out.extend(&chars[start.min(end)..end]);
if end < chars.len() {
out.push('…');
}
out
}
-/// Count the pairs of `history` (newest first) at or after `since_ms`, the `limit` most frequent:
-/// only those the AI clean-up made ([`polish_only`]; `rules` are the current rules).
-pub fn candidates(dictionary: &[DictionaryEntry], rules: &[ReplacementRule], history: &[HistoryText], since_ms: Option, limit: usize) -> Candidates {
+/// The widened pairs counted: one per form pair, with how often, how often from the user's
+/// corrections, the newest take, and up to [`MAX_EXAMPLES`] snippets (one per take, newest
+/// first); most frequent first, then the most recent.
+pub(crate) fn tally(dictionary: &[DictionaryEntry], history: &[HistoryText], counted: Vec) -> Vec {
let known: Vec<&str> = dictionary.iter().filter(|e| e.enabled).flat_map(|e| e.heard_as.iter().map(String::as_str)).collect();
- let mut out = Candidates::default();
+ let is_known = |heard: &str| known.iter().any(|k| k.eq_ignore_ascii_case(heard));
let mut index: HashMap<(String, String), usize> = HashMap::new();
let mut pairs: Vec = Vec::new();
- let mut cells = 0usize;
- for take in history.iter().filter(|t| since_ms.is_none_or(|since| t.at_ms >= since)) {
- if !take.refined {
- // Not polished: every change in it is a correction's or a rule's.
- out.scanned = out.scanned.saturating_add(1);
- continue;
- }
- let size = (diff_tokens(&take.raw_text).len() + 1).saturating_mul(diff_tokens(&take.text).len() + 1);
- if size > MAX_DIFF_CELLS {
- out.scanned = out.scanned.saturating_add(1);
- out.skipped = out.skipped.saturating_add(1);
- continue;
- }
- // A take whose corrections or rules fired is diffed a second time (`polish_only`).
- let cost = if take.vocabulary.is_empty() { size } else { size.saturating_mul(2) };
- if cells.saturating_add(cost) > MAX_SCAN_CELLS {
- break;
- }
- cells += cost;
- out.scanned = out.scanned.saturating_add(1);
- let Some(found) = replacements(&take.raw_text, &take.text) else {
- out.skipped = out.skipped.saturating_add(1);
- continue;
- };
- for r in polish_only(found, take, dictionary, rules) {
- let key = (r.heard.clone(), r.polished.clone());
- let slot = *index.entry(key).or_insert_with(|| {
- pairs.push(Pair {
- known: known.iter().any(|k| k.eq_ignore_ascii_case(&r.heard)),
- heard: r.heard.clone(),
- polished: r.polished.clone(),
- count: 0,
- last_at_ms: take.at_ms,
- examples: Vec::new(),
- });
- pairs.len() - 1
+ for Counted { found, single, heard_len } in counted {
+ let take = &history[found.take];
+ let key = (found.heard.clone(), found.right.clone());
+ let slot = *index.entry(key).or_insert_with(|| {
+ pairs.push(Pair {
+ known: is_known(&found.heard) || single.as_ref().is_some_and(|(h, _)| is_known(h)),
+ heard: found.heard.clone(),
+ polished: found.right.clone(),
+ count: 0,
+ last_at_ms: take.at_ms,
+ examples: Vec::new(),
+ from_edits: 0,
+ single: single.clone(),
});
- let pair = &mut pairs[slot];
- pair.count = pair.count.saturating_add(1);
- pair.last_at_ms = pair.last_at_ms.max(take.at_ms);
- if pair.examples.len() < MAX_EXAMPLES && !pair.examples.iter().any(|e| e.history_id == take.id) {
- pair.examples.push(PairExample { history_id: take.id, snippet: snippet(&take.raw_text, r.at, r.len) });
- }
+ pairs.len() - 1
+ });
+ let pair = &mut pairs[slot];
+ pair.count = pair.count.saturating_add(1);
+ if found.edit {
+ pair.from_edits = pair.from_edits.saturating_add(1);
+ }
+ pair.last_at_ms = pair.last_at_ms.max(take.at_ms);
+ if pair.examples.len() < MAX_EXAMPLES && !pair.examples.iter().any(|e| e.history_id == take.id) {
+ pair.examples.push(PairExample { history_id: take.id, snippet: snippet(&take.raw_text, found.heard_at, heard_len) });
}
}
pairs.sort_by(|a, b| b.count.cmp(&a.count).then(b.last_at_ms.cmp(&a.last_at_ms)));
+ pairs
+}
+
+/// Count the pairs of `history` (newest first) at or after `since_ms`, the `limit` most frequent:
+/// only those the AI clean-up made (`rules` are the current rules), single characters widened.
+pub fn candidates(dictionary: &[DictionaryEntry], rules: &[ReplacementRule], history: &[HistoryText], since_ms: Option, limit: usize) -> Candidates {
+ let read = scan(dictionary, rules, history, since_ms, false);
+ let mut pairs = tally(dictionary, history, widen(history, &read));
pairs.truncate(limit);
- out.pairs = pairs;
- out
+ Candidates { scanned: read.scanned, skipped: read.skipped, pairs }
}
diff --git a/crates/voltip-core/src/mcp/tests.rs b/crates/voltip-core/src/mcp/tests.rs
index fb33697a..4c7b2a71 100644
--- a/crates/voltip-core/src/mcp/tests.rs
+++ b/crates/voltip-core/src/mcp/tests.rs
@@ -51,7 +51,7 @@ fn rule(name: &str, source: EntrySource, pending: bool) -> ReplacementRule {
}
fn take(raw: &str, text: &str, at_ms: u64) -> HistoryText {
- HistoryText { id: Uuid::new_v4(), at_ms, raw_text: raw.into(), text: text.into(), refined: true, vocabulary: VocabularyHits::default() }
+ HistoryText { id: Uuid::new_v4(), at_ms, raw_text: raw.into(), text: text.into(), refined: true, vocabulary: VocabularyHits::default(), corrected: None }
}
#[test]
@@ -306,8 +306,9 @@ fn replacements_are_trimmed_pairs_without_punctuation_only_changes() {
#[test]
fn candidates_are_counted_known_and_sorted() {
let dictionary = vec![entry("Voltip", &["维他命"])];
- // The diff works on single CJK characters, as the history page's: 非书 → 飞书 is 非 → 飞, and
- // the snippets show the word around it.
+ // The diff works on single CJK characters, as the history page's; a pair of single characters
+ // is counted with its unchanged neighbour (docs/dictation.md §16.10): 非书 → 飞书, not 非 → 飞.
+ // The snippets show the word around it.
let history = vec![
take("我们用维他命开会", "我们用 Voltip 开会。", 50),
take("发到非书群", "发到飞书群。", 40),
@@ -318,7 +319,7 @@ fn candidates_are_counted_known_and_sorted() {
let out = candidates(&dictionary, &[], &history, None, 10);
assert_eq!((out.scanned, out.skipped), (5, 0));
let top: Vec<(&str, &str, u32, bool, u64)> = out.pairs.iter().map(|p| (p.heard.as_str(), p.polished.as_str(), p.count, p.known, p.last_at_ms)).collect();
- assert_eq!(top, [("非", "飞", 4, false, 40), ("维他命", "Voltip", 2, true, 50)], "every occurrence counts");
+ assert_eq!(top, [("非书", "飞书", 4, false, 40), ("维他命", "Voltip", 2, true, 50)], "every occurrence counts");
let examples = &out.pairs[0].examples;
assert_eq!(examples.iter().map(|e| e.history_id).collect::>(), [history[1].id, history[2].id, history[4].id], "one per take, newest first");
assert_eq!(examples[0].snippet, "发到非书群");
diff --git a/crates/voltip-core/src/runtime.rs b/crates/voltip-core/src/runtime.rs
index 6374f2c8..d4388a63 100644
--- a/crates/voltip-core/src/runtime.rs
+++ b/crates/voltip-core/src/runtime.rs
@@ -49,6 +49,8 @@ pub use auto_backup::AUTO_WEEKLY_DELAY;
mod backup;
mod check;
mod import;
+mod learn;
+pub use learn::{LEARN_NOTICE_DWELL, LearnNotice, LearnOutcome, NoticeAccepted};
mod listing;
mod mcp;
mod models_move;
@@ -130,6 +132,9 @@ pub struct CoreConfig {
/// 窗口已切换 and the failures on screen until they are closed. Off (the default; the phone,
/// the local speech service), every terminal state goes after its dwell.
pub result_cards: bool,
+ /// How long the pill's notice after a 从选中文字学习 stays once it shows, and again after each
+ /// of its buttons (docs/dictation.md §35): [`LEARN_NOTICE_DWELL`]; tests shorten it.
+ pub learn_notice_dwell: Duration,
/// Make the weekly automatic backup and show the automatic backups (docs/dictation.md
/// §4.6.1): the desktop yes, the phone and the local speech service no. The backups before an
/// update and before a replacing import are made wherever they are asked for.
@@ -247,6 +252,7 @@ impl CoreConfig {
accepts_phone_takes: true,
keeps_audio: true,
result_cards: false,
+ learn_notice_dwell: LEARN_NOTICE_DWELL,
auto_backup: false,
shows_live_preview: true,
builtin_scenes: true,
@@ -321,6 +327,9 @@ pub enum CoreCommand {
/// Choose (`Some`) or clear (`None`) the 复制上一条结果 hotkey (docs/dictation.md §28); validated
/// like the dictation hotkey and refused when it is the dictation's or the voice edit's chord.
SetCopyLastHotkey(Option),
+ /// 从选中文字学习 hotkey (docs/dictation.md §35): a chord, or `None` for none (the default);
+ /// never one of the other three chords.
+ SetLearnHotkey(Option),
/// Choose (`Some`) or switch off (`None`) the lone-key trigger (docs/dictation.md §13.1);
/// persisted, the desktop shell (un)installs its input hook on `Settings`.
SetSoloKey(Option),
@@ -412,6 +421,30 @@ pub enum CoreCommand {
/// 复制上一条结果 (docs/dictation.md §28): the newest result's text onto the clipboard, on a
/// blocking thread; `reply` gets what happened.
CopyLastResult(crate::last_result::CopyLastReply),
+ /// 从选中文字学习 (docs/dictation.md §35): copy the selection of the application in front and
+ /// learn from the takes it corrects; what came of it shows as `UiState.learn_notice`. Ignored
+ /// while a take is under way. `source` decides which modifiers the user may still hold.
+ LearnSelection {
+ /// The shortcut, or `voltip --learn-selection`.
+ source: EdgeSource,
+ },
+ /// The learn notice's 加入词典 (docs/dictation.md §35), for notice `id`.
+ LearnNoticeAccept {
+ /// `LearnNotice::id`.
+ id: u64,
+ },
+ /// The learn notice's 撤销.
+ LearnNoticeUndo {
+ /// `LearnNotice::id`.
+ id: u64,
+ },
+ /// The learn notice's ×.
+ LearnNoticeDismiss {
+ /// `LearnNotice::id`.
+ id: u64,
+ },
+ /// Forget the user's correction of a history entry (its detail's 移除, docs/dictation.md §35).
+ HistoryClearCorrected(Uuid),
/// A hotkey / CLI key transition (docs/dictation.md §13): the activation machine turns it into
/// start / stop / cancel / lock according to `Settings.activation`.
HotkeyEdge {
@@ -774,6 +807,7 @@ struct Inbox {
usage_rx: mpsc::Receiver,
import_rx: mpsc::Receiver,
auto_rx: mpsc::Receiver,
+ learn_rx: mpsc::Receiver,
}
/// Events to the UI.
@@ -870,6 +904,8 @@ pub enum CoreEvent {
/// The pairs the learning suggestions leave out (docs/dictation.md §16.10), oldest first; on
/// `Ready` and after every change.
VocabularyIgnored(Vec),
+ /// The pill's notice after a 从选中文字学习 changed or went (docs/dictation.md §35).
+ LearnNotice(Option),
/// Scenes in matching order (full replacement); on `Ready` and after every change.
Scenes(Vec),
/// Custom presets, full replacement (docs/dictation.md §21).
@@ -1059,6 +1095,7 @@ impl AppCore {
let (mcp, mcp_notice) = mcp::McpRuntime::new(config.mcp_host.clone(), &config.data_dir, mcp_tx);
let (imports, import_rx) = import::ImportRuntime::new(config.media_decoder.clone(), &config.data_dir);
let (auto, auto_rx) = auto_backup::AutoBackups::new(&config.data_dir, now_ms());
+ let (learn, learn_rx) = learn::LearnRuntime::new(&config.data_dir);
let mut rt = Runtime {
config,
manager,
@@ -1137,11 +1174,27 @@ impl AppCore {
last_result: None,
imports,
auto,
+ learn,
};
rt.system_fingerprint = rt.config.network.system_fingerprint();
rt.connect_relay()?;
- let inbox =
- Inbox { cmd_rx, link_rx, dict_rx, model_rx, act_rx, phone_rx, check_rx, process_rx, listed_rx, mcp_rx, recover_rx, usage_rx, import_rx, auto_rx };
+ let inbox = Inbox {
+ cmd_rx,
+ link_rx,
+ dict_rx,
+ model_rx,
+ act_rx,
+ phone_rx,
+ check_rx,
+ process_rx,
+ listed_rx,
+ mcp_rx,
+ recover_rx,
+ usage_rx,
+ import_rx,
+ auto_rx,
+ learn_rx,
+ };
Ok((rt, inbox, CoreHandle { cmd: cmd_tx, levels: levels_tx }, evt_rx))
}
}
@@ -1453,6 +1506,8 @@ struct Runtime {
last_result: Option,
/// The import queue (docs/dictation.md §31).
imports: import::ImportRuntime,
+ /// 从选中文字学习 and its notice (docs/dictation.md §35).
+ learn: learn::LearnRuntime,
/// The automatic backups (docs/dictation.md §4.6.1).
auto: auto_backup::AutoBackups,
}
@@ -1581,6 +1636,7 @@ impl Runtime {
mut usage_rx,
mut import_rx,
mut auto_rx,
+ mut learn_rx,
} = inbox;
self.emit(CoreEvent::Ready {
identity: self.identity.public(),
@@ -1639,6 +1695,7 @@ impl Runtime {
Some(inbound) = usage_rx.recv() => self.on_usage(inbound),
Some(inbound) = import_rx.recv() => self.on_import(inbound),
Some(inbound) = auto_rx.recv() => self.on_auto_backup(inbound, now_ms()),
+ Some(inbound) = learn_rx.recv() => self.on_learn(inbound),
Ok(()) = quota_rx.changed() => self.emit(CoreEvent::Engines(self.engine_status())),
_ = ticker.tick() => self.tick().await,
}
@@ -1694,6 +1751,7 @@ impl Runtime {
CoreCommand::SetHotkey(text) => self.set_hotkey(&text),
CoreCommand::SetEditHotkey(text) => self.set_edit_hotkey(text.as_deref()),
CoreCommand::SetCopyLastHotkey(text) => self.set_copy_last_hotkey(text.as_deref()),
+ CoreCommand::SetLearnHotkey(text) => self.set_learn_hotkey(text.as_deref()),
CoreCommand::SetSoloKey(key) => self.set_solo_key(key),
CoreCommand::SetMicrophone(device) => self.set_microphone(device),
CoreCommand::SetRecording(recording) => self.set_recording(recording),
@@ -1751,6 +1809,23 @@ impl Runtime {
self.copy_last_result(reply);
Ok(())
}
+ CoreCommand::LearnSelection { source } => {
+ self.learn_selection(source);
+ Ok(())
+ }
+ CoreCommand::LearnNoticeAccept { id } => {
+ self.learn_notice_accept(id);
+ Ok(())
+ }
+ CoreCommand::LearnNoticeUndo { id } => {
+ self.learn_notice_undo(id);
+ Ok(())
+ }
+ CoreCommand::LearnNoticeDismiss { id } => {
+ self.learn_notice_dismiss(id);
+ Ok(())
+ }
+ CoreCommand::HistoryClearCorrected(id) => self.clear_correction(id),
CoreCommand::HotkeyEdge { pressed, at_ms, source, purpose, chorded } => self.hotkey_edge(Edge { pressed, at_ms, source }, purpose, chorded),
CoreCommand::SetActivation { activation, hold_threshold_ms, extra_recording_ms } => {
self.set_activation(activation, hold_threshold_ms, extra_recording_ms)
@@ -2896,7 +2971,10 @@ impl Runtime {
self.last_result = Some(text.to_owned());
}
self.follow_refine_notice(&status.phase);
+ let phase = status.phase.clone();
self.emit(CoreEvent::Dictation(status));
+ // docs/dictation.md §35: after the phase, so the pill sees it idle first.
+ self.follow_learn_notice(&phase);
// A phone's text waits for the take to end (docs/dictation.md §20.6).
if hint == PhaseHint::Idle {
self.deliver_next_text();
@@ -3233,6 +3311,9 @@ impl Runtime {
if self.copy_last_chord_is(&hotkey) {
return Err(CoreError::Invalid(format!("hotkey: {} 已用作「复制上一条结果」的快捷键", hotkey.display())));
}
+ if self.learn_chord_is(&hotkey) {
+ return Err(CoreError::Invalid(format!("hotkey: {} 已用作「从选中文字学习」的快捷键", hotkey.display())));
+ }
self.settings.hotkey = hotkey.display();
self.save_settings()
}
@@ -3242,6 +3323,36 @@ impl Runtime {
self.settings.copy_last_hotkey.as_deref().is_some_and(|copy| crate::Hotkey::same_chord(&hotkey.display(), copy))
}
+ /// `hotkey` is the 从选中文字学习 chord (docs/dictation.md §35).
+ fn learn_chord_is(&self, hotkey: &crate::Hotkey) -> bool {
+ self.settings.learn_hotkey.as_deref().is_some_and(|learn| crate::Hotkey::same_chord(&hotkey.display(), learn))
+ }
+
+ /// `SetLearnHotkey` (docs/dictation.md §35): `None` assigns none; a chord is validated like the
+ /// dictation hotkey and refused when it is the dictation's, the voice edit's or 复制上一条结果's.
+ fn set_learn_hotkey(&mut self, text: Option<&str>) -> Result<(), CoreError> {
+ let chord = match text {
+ None => None,
+ Some(text) => {
+ let hotkey = crate::Hotkey::parse(text)?;
+ if crate::Hotkey::same_chord(&hotkey.display(), &self.settings.hotkey) {
+ return Err(CoreError::Invalid(format!("learn_hotkey: {} 已用作听写快捷键", hotkey.display())));
+ }
+ if let Some(edit) = self.settings.edit_hotkey.as_deref()
+ && crate::Hotkey::same_chord(&hotkey.display(), edit)
+ {
+ return Err(CoreError::Invalid(format!("learn_hotkey: {} 已用作「编辑选中文本」的快捷键", hotkey.display())));
+ }
+ if self.copy_last_chord_is(&hotkey) {
+ return Err(CoreError::Invalid(format!("learn_hotkey: {} 已用作「复制上一条结果」的快捷键", hotkey.display())));
+ }
+ Some(hotkey.display())
+ }
+ };
+ self.settings.learn_hotkey = chord;
+ self.save_settings()
+ }
+
/// `SetCopyLastHotkey` (docs/dictation.md §28): `None` assigns none; a chord is validated like
/// the dictation hotkey and refused when it is the dictation's or the voice edit's.
fn set_copy_last_hotkey(&mut self, text: Option<&str>) -> Result<(), CoreError> {
@@ -3257,6 +3368,9 @@ impl Runtime {
{
return Err(CoreError::Invalid(format!("copy_last_hotkey: {} 已用作「编辑选中文本」的快捷键", hotkey.display())));
}
+ if self.learn_chord_is(&hotkey) {
+ return Err(CoreError::Invalid(format!("copy_last_hotkey: {} 已用作「从选中文字学习」的快捷键", hotkey.display())));
+ }
Some(hotkey.display())
}
};
@@ -3303,6 +3417,9 @@ impl Runtime {
if self.copy_last_chord_is(&hotkey) {
return Err(CoreError::Invalid(format!("edit_hotkey: {} 已用作「复制上一条结果」的快捷键", hotkey.display())));
}
+ if self.learn_chord_is(&hotkey) {
+ return Err(CoreError::Invalid(format!("edit_hotkey: {} 已用作「从选中文字学习」的快捷键", hotkey.display())));
+ }
Some(hotkey.display())
}
};
diff --git a/crates/voltip-core/src/runtime/learn.rs b/crates/voltip-core/src/runtime/learn.rs
new file mode 100644
index 00000000..c5bc0552
--- /dev/null
+++ b/crates/voltip-core/src/runtime/learn.rs
@@ -0,0 +1,413 @@
+//! 从选中文字学习 (`CoreCommand::LearnSelection`, docs/dictation.md §35): the user corrected a take
+//! in the application it went to, selected the corrected text and pressed the shortcut (or ran
+//! `voltip --learn-selection`). On a task: the probe names the application in front (a terminal's
+//! selection is program output: no key is pressed), the copy chord reads the selection (§19.2, the
+//! clipboard put back afterwards), and the newest [`LEARN_TAKES`] recognised takes are compared with
+//! it (`crate::vocabulary::learn`). Back here the corrections are stored with their entries; then
+//! the learning suggestions are counted over the history once more for the pairs the corrected
+//! takes give, and the notice ([`LearnNotice`], `UiState.learn_notice`) offers them on the pill.
+//!
+//! The notice is shown while no take uses the pill: one that comes while a take's result is still
+//! on it waits, without a timer, until the pill is idle again; then it stays
+//! [`LEARN_NOTICE_DWELL`], again after each of its buttons; a new take clears it. With the pill off
+//! there is no notice: the outcome is logged and the pairs wait in the learning suggestions.
+//! Nothing here logs a text.
+
+use std::sync::Arc;
+use std::time::Duration;
+
+use serde::{Deserialize, Serialize};
+use tokio::sync::mpsc;
+use uuid::Uuid;
+
+use super::{CoreEvent, Runtime, now_ms};
+use crate::dictation::activation::EdgeSource;
+use crate::dictation::{DictationPhase, ForegroundApp};
+use crate::history::{CorrectedText, CorrectionVia, HistoryReader, MAX_RECENT_TEXTS};
+use crate::settings::OverlayPlacement;
+use crate::vocabulary::learn::{LEARN_TAKES, MAX_LEARN_SELECTION_CHARS, match_selection, storable};
+use crate::vocabulary::{BatchItem, BatchUndoOutcome, SuggestionPick, edit_picks};
+
+/// How long the notice stays on the pill, counted from when it shows and again after each button.
+pub const LEARN_NOTICE_DWELL: Duration = Duration::from_secs(8);
+
+/// What came of one 从选中文字学习 (docs/dictation.md §35).
+#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(tag = "kind", rename_all = "snake_case")]
+pub enum LearnOutcome {
+ /// The corrections give these pairs; `safe`: none has a short heard form (one character, or one
+ /// or two Latin letters), so the notice may offer 加入词典 at once.
+ Learned {
+ /// The pairs, in the order the takes come.
+ pairs: Vec,
+ /// No pair is short enough to change other words.
+ safe: bool,
+ },
+ /// The selection corrects takes, but no change is one to put in the dictionary (only
+ /// punctuation, new or deleted words, what the clean-up, the dictionary or a rule wrote, or a
+ /// pair known or ignored already).
+ NoPairs,
+ /// The selection is what was inserted (or what it was put back to).
+ Unchanged,
+ /// None of the newest takes is like the selection.
+ NoMatch,
+ /// Nothing was selected (or the application did not copy).
+ NoSelection,
+ /// The selection is longer than `max` characters.
+ TooLong {
+ /// [`MAX_LEARN_SELECTION_CHARS`].
+ max: u32,
+ },
+ /// A terminal is in front: its selection is program output, so nothing was copied.
+ Terminal,
+ /// The history is off: there is nothing to compare with, and nothing would be kept.
+ HistoryOff,
+ /// The selection could not be read, or the history could not be read or written.
+ Failed {
+ /// Why (no text of the user's).
+ message: String,
+ },
+}
+
+/// What 加入词典 on the notice did.
+#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
+pub struct NoticeAccepted {
+ /// Pairs that went into the dictionary.
+ pub added: u32,
+ /// Pairs the dictionary or a rule refused (the dictionary page says why).
+ pub refused: u32,
+ /// The batch 撤销 takes back, when anything went in.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub undo: Option,
+}
+
+/// The pill's notice after a 从选中文字学习 (`UiState.learn_notice`, docs/dictation.md §35).
+#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
+pub struct LearnNotice {
+ /// Which notice: the buttons name it, and a newer one replaces it.
+ pub id: u64,
+ /// What came of it.
+ pub outcome: LearnOutcome,
+ /// On the pill now; a notice that waits behind a take's result is not (yet).
+ pub shown: bool,
+ /// 加入词典 ran.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub accepted: Option,
+ /// 撤销 ran.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub undone: Option,
+}
+
+/// A heard form short enough to change other words (the dictionary page's warning, `shortForm`
+/// in `packages/shared/src/quick-fix.ts`): one character, or one or two Latin letters.
+fn short_form(heard: &str) -> bool {
+ let form = heard.trim();
+ let n = form.chars().count();
+ n == 1 || (n <= 2 && !form.is_empty() && form.chars().all(|c| c.is_ascii_alphabetic()))
+}
+
+/// One take a selection corrects, as the task found it.
+#[derive(Debug)]
+pub(super) struct Correction {
+ id: Uuid,
+ /// The whole corrected text to keep, or `None` to clear an earlier one.
+ keep: Option,
+ /// The take had a correction before.
+ had: bool,
+ /// The selection is what was inserted.
+ unchanged: bool,
+}
+
+/// What the tasks send back.
+#[derive(Debug)]
+pub(super) enum LearnInbound {
+ /// The selection was read and compared, or the attempt ended early.
+ Compared { request: u64, result: Result, LearnOutcome> },
+ /// The pairs of the corrected takes are counted.
+ Counted { request: u64, picks: Result, String> },
+ /// A notice's time is up.
+ Expired { id: u64, generation: u64 },
+}
+
+/// The runtime's part of 从选中文字学习.
+pub(super) struct LearnRuntime {
+ tx: mpsc::Sender,
+ reader: Arc,
+ /// The newest request; answers to older ones are dropped.
+ request: u64,
+ notice: Option,
+ next_id: u64,
+ /// Restarts of the notice's timer; an older timer's expiry is dropped.
+ generation: u64,
+}
+
+impl LearnRuntime {
+ pub(super) fn new(data_dir: &std::path::Path) -> (Self, mpsc::Receiver) {
+ let (tx, rx) = mpsc::channel(8);
+ (Self { tx, reader: Arc::new(HistoryReader::new(data_dir)), request: 0, notice: None, next_id: 0, generation: 0 }, rx)
+ }
+}
+
+/// A take is under way: not idle and not yet finished.
+fn take_running(phase: &DictationPhase) -> bool {
+ !matches!(phase, DictationPhase::Idle) && !phase.is_terminal()
+}
+
+impl Runtime {
+ /// `LearnSelection`: read the selection and learn from it, unless a take is under way (the key
+ /// belongs to it then) or the history is off.
+ pub(super) fn learn_selection(&mut self, source: EdgeSource) {
+ if take_running(&self.dictation.status().phase) {
+ tracing::info!("learn selection ignored: a take is under way");
+ return;
+ }
+ self.learn.request += 1;
+ let request = self.learn.request;
+ if !self.settings.history.enabled {
+ tracing::info!(request, "learn selection: the history is off");
+ self.show_learned(LearnOutcome::HistoryOff);
+ return;
+ }
+ // The user may still hold the shortcut's modifiers when the copy chord goes out (§19.2).
+ let held = match source {
+ EdgeSource::Cli => Vec::new(),
+ EdgeSource::Hotkey | EdgeSource::Ui => {
+ self.settings.learn_hotkey.as_deref().and_then(|h| crate::Hotkey::parse(h).ok()).map(|h| h.modifiers).unwrap_or_default()
+ }
+ };
+ let (probe, injector, reader, tx) = (self.dictation.probe(), self.dictation.injector(), self.learn.reader.clone(), self.learn.tx.clone());
+ tokio::spawn(async move {
+ let result = read_and_compare(probe, injector, reader, held).await;
+ let _ = tx.send(LearnInbound::Compared { request, result }).await;
+ });
+ }
+
+ /// An answer from a task, or a notice's timer.
+ pub(super) fn on_learn(&mut self, inbound: LearnInbound) {
+ match inbound {
+ LearnInbound::Compared { request, result } if request == self.learn.request => match result {
+ Err(outcome) => self.show_learned(outcome),
+ Ok(corrections) => self.keep_corrections(request, corrections),
+ },
+ LearnInbound::Counted { request, picks } if request == self.learn.request => match picks {
+ Ok(picks) if picks.is_empty() => self.show_learned(LearnOutcome::NoPairs),
+ Ok(pairs) => {
+ let safe = !pairs.iter().any(|p| short_form(&p.heard));
+ self.show_learned(LearnOutcome::Learned { pairs, safe });
+ }
+ Err(message) => self.show_learned(LearnOutcome::Failed { message }),
+ },
+ LearnInbound::Expired { id, generation } => {
+ if self.learn.notice.as_ref().is_some_and(|n| n.id == id) && generation == self.learn.generation {
+ self.set_learn_notice(None);
+ }
+ }
+ LearnInbound::Compared { .. } | LearnInbound::Counted { .. } => tracing::debug!("an older learn selection answered; dropped"),
+ }
+ }
+
+ /// Store what the selection corrects, then count the pairs those takes give.
+ fn keep_corrections(&mut self, request: u64, corrections: Vec) {
+ if corrections.is_empty() {
+ self.show_learned(LearnOutcome::NoMatch);
+ return;
+ }
+ let mut written = false;
+ let mut kept: Vec = Vec::new();
+ for c in &corrections {
+ let result = match &c.keep {
+ Some(text) => {
+ kept.push(c.id);
+ self.history.set_corrected(c.id, Some(CorrectedText { text: text.clone(), at_ms: now_ms(), via: CorrectionVia::Selection }))
+ }
+ None if c.had => self.history.set_corrected(c.id, None),
+ None => Ok(false),
+ };
+ match result {
+ Ok(changed) => written |= changed,
+ Err(e) => {
+ tracing::warn!(error = %e, "learn selection: the correction could not be stored");
+ self.show_learned(LearnOutcome::Failed { message: e.to_string() });
+ return;
+ }
+ }
+ }
+ if written {
+ self.emit_history();
+ }
+ tracing::info!(request, takes = corrections.len(), kept = kept.len(), "learn selection: compared");
+ if kept.is_empty() {
+ let unchanged = corrections.iter().all(|c| c.unchanged);
+ self.show_learned(if unchanged { LearnOutcome::Unchanged } else { LearnOutcome::NoPairs });
+ return;
+ }
+ let (dictionary, rules, ignored) = (self.dictionary.entries().to_vec(), self.rules.rules().to_vec(), self.ignored.list().to_vec());
+ let (reader, tx) = (self.learn.reader.clone(), self.learn.tx.clone());
+ tokio::spawn(async move {
+ let picks = tokio::task::spawn_blocking(move || {
+ let history = reader.recent_texts(MAX_RECENT_TEXTS).map_err(|e| e.to_string())?;
+ Ok(edit_picks(&dictionary, &rules, &history, &ignored, &kept))
+ })
+ .await
+ .unwrap_or_else(|e| Err(format!("learn task failed: {e}")));
+ let _ = tx.send(LearnInbound::Counted { request, picks }).await;
+ });
+ }
+
+ /// Put `outcome` on the pill as a new notice (none with the pill off), waiting while a take's
+ /// result is still there.
+ fn show_learned(&mut self, outcome: LearnOutcome) {
+ tracing::info!(outcome = learned_name(&outcome), "learn selection");
+ // No pill to show it on, or a take began meanwhile and has the pill: the pairs wait in the
+ // learning suggestions.
+ if self.settings.overlay == OverlayPlacement::Off || take_running(&self.dictation.status().phase) {
+ return;
+ }
+ self.learn.next_id += 1;
+ let shown = matches!(self.dictation.status().phase, DictationPhase::Idle);
+ self.set_learn_notice(Some(LearnNotice { id: self.learn.next_id, outcome, shown, accepted: None, undone: None }));
+ }
+
+ /// Replace the notice, restart its timer when it is on the pill, and tell the UI.
+ fn set_learn_notice(&mut self, notice: Option) {
+ self.learn.generation += 1;
+ if let Some(n) = notice.as_ref().filter(|n| n.shown) {
+ let (id, generation, tx, dwell) = (n.id, self.learn.generation, self.learn.tx.clone(), self.config.learn_notice_dwell);
+ tokio::spawn(async move {
+ tokio::time::sleep(dwell).await;
+ let _ = tx.send(LearnInbound::Expired { id, generation }).await;
+ });
+ }
+ self.learn.notice = notice;
+ self.emit(CoreEvent::LearnNotice(self.learn.notice.clone()));
+ }
+
+ /// The dictation phase moved (docs/dictation.md §35): a take that starts clears the notice; a
+ /// pill that is idle again shows a waiting one.
+ pub(super) fn follow_learn_notice(&mut self, phase: &DictationPhase) {
+ let Some(notice) = self.learn.notice.clone() else { return };
+ if take_running(phase) {
+ self.set_learn_notice(None);
+ } else if !notice.shown && matches!(phase, DictationPhase::Idle) {
+ self.set_learn_notice(Some(LearnNotice { shown: true, ..notice }));
+ }
+ }
+
+ /// The notice's 加入词典: its pairs through the batch accept of the learning suggestions.
+ pub(super) fn learn_notice_accept(&mut self, id: u64) {
+ let Some(notice) = self.learn.notice.clone().filter(|n| n.id == id && n.accepted.is_none()) else { return };
+ let LearnOutcome::Learned { pairs, safe: true } = ¬ice.outcome else {
+ tracing::info!(id, "learn notice: nothing to add at once");
+ return;
+ };
+ let outcome = self.accept_suggestions(pairs);
+ let added = outcome.items.iter().filter(|i| matches!(i, BatchItem::Accepted { .. })).count();
+ let accepted = NoticeAccepted {
+ added: u32::try_from(added).unwrap_or(u32::MAX),
+ refused: u32::try_from(outcome.items.len() - added).unwrap_or(u32::MAX),
+ undo: outcome.undo,
+ };
+ self.set_learn_notice(Some(LearnNotice { accepted: Some(accepted), ..notice }));
+ }
+
+ /// The notice's 撤销: the batch its 加入词典 made, taken back.
+ pub(super) fn learn_notice_undo(&mut self, id: u64) {
+ let Some(notice) = self.learn.notice.clone().filter(|n| n.id == id && n.undone.is_none()) else { return };
+ let Some(batch) = notice.accepted.as_ref().and_then(|a| a.undo) else { return };
+ let outcome = self.undo_suggestions(batch);
+ self.set_learn_notice(Some(LearnNotice { undone: Some(outcome), ..notice }));
+ }
+
+ /// The notice's ×.
+ pub(super) fn learn_notice_dismiss(&mut self, id: u64) {
+ if self.learn.notice.as_ref().is_some_and(|n| n.id == id) {
+ self.set_learn_notice(None);
+ }
+ }
+
+ /// The history detail's 移除: forget the user's correction of entry `id`.
+ pub(super) fn clear_correction(&mut self, id: Uuid) -> Result<(), crate::CoreError> {
+ if self.history.set_corrected(id, None)? {
+ self.emit_history();
+ }
+ Ok(())
+ }
+}
+
+/// The outcome's name for the log.
+fn learned_name(outcome: &LearnOutcome) -> &'static str {
+ match outcome {
+ LearnOutcome::Learned { .. } => "learned",
+ LearnOutcome::NoPairs => "no_pairs",
+ LearnOutcome::Unchanged => "unchanged",
+ LearnOutcome::NoMatch => "no_match",
+ LearnOutcome::NoSelection => "no_selection",
+ LearnOutcome::TooLong { .. } => "too_long",
+ LearnOutcome::Terminal => "terminal",
+ LearnOutcome::HistoryOff => "history_off",
+ LearnOutcome::Failed { .. } => "failed",
+ }
+}
+
+/// The task: the application in front, the selection, the takes it corrects.
+async fn read_and_compare(
+ probe: Option>,
+ injector: Arc,
+ reader: Arc,
+ held: Vec,
+) -> Result, LearnOutcome> {
+ // §19.2: no key is pressed before the probe said which application is in front; a terminal's
+ // selection is program output. No answer (pure Wayland, a slow probe): copied anyway.
+ if let Some(probe) = probe {
+ let deadline = probe.deadline();
+ let app = match tokio::time::timeout(deadline, tokio::task::spawn_blocking(move || probe.foreground())).await {
+ Ok(Ok(Ok(app))) => app.and_then(ForegroundApp::sanitized),
+ _ => None,
+ };
+ if let Some(app) = app
+ && injector.is_terminal_app(&app.app_id)
+ {
+ tracing::info!(app = %app.app_id, "learn selection refused: a terminal is in front");
+ return Err(LearnOutcome::Terminal);
+ }
+ }
+ let copied = tokio::task::spawn_blocking(move || injector.copy_selection(&held)).await;
+ let selection = match copied {
+ Ok(Ok(Some(text))) if !text.trim().is_empty() => text,
+ Ok(Ok(_)) => return Err(LearnOutcome::NoSelection),
+ Ok(Err(e)) => return Err(LearnOutcome::Failed { message: e.to_string() }),
+ Err(e) => return Err(LearnOutcome::Failed { message: format!("copy task failed: {e}") }),
+ };
+ if selection.chars().count() > MAX_LEARN_SELECTION_CHARS {
+ return Err(LearnOutcome::TooLong { max: u32::try_from(MAX_LEARN_SELECTION_CHARS).unwrap_or(u32::MAX) });
+ }
+ tracing::debug!(chars = selection.chars().count(), "learn selection: copied");
+ tokio::task::spawn_blocking(move || {
+ let takes = reader.recent_texts(LEARN_TAKES).map_err(|e| LearnOutcome::Failed { message: e.to_string() })?;
+ Ok(match_selection(&selection, &takes)
+ .into_iter()
+ .map(|m| {
+ let take = &takes[m.index];
+ let keep = storable(take, &m.corrected).then_some(m.corrected.clone());
+ Correction { id: take.id, keep, had: take.corrected.is_some(), unchanged: m.corrected == take.text }
+ })
+ .collect())
+ })
+ .await
+ .unwrap_or_else(|e| Err(LearnOutcome::Failed { message: format!("learn task failed: {e}") }))
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn short_forms_are_the_dictionary_pages() {
+ for short in ["非", "a", "ab", " AB "] {
+ assert!(short_form(short), "{short}");
+ }
+ for long in ["非书", "abc", "a1", "Quinn for"] {
+ assert!(!short_form(long), "{long}");
+ }
+ }
+}
diff --git a/crates/voltip-core/src/runtime/takes.rs b/crates/voltip-core/src/runtime/takes.rs
index e04846ef..fbe15db6 100644
--- a/crates/voltip-core/src/runtime/takes.rs
+++ b/crates/voltip-core/src/runtime/takes.rs
@@ -545,6 +545,7 @@ impl Runtime {
preset: None,
origin: Some(EntryOrigin { device: computer, kind: OriginKind::Sent }),
processed: None,
+ corrected: None,
file: None,
timing: None,
glossary_sent: None,
diff --git a/crates/voltip-core/src/runtime/texts.rs b/crates/voltip-core/src/runtime/texts.rs
index f824b200..c2476245 100644
--- a/crates/voltip-core/src/runtime/texts.rs
+++ b/crates/voltip-core/src/runtime/texts.rs
@@ -166,6 +166,7 @@ impl Runtime {
preset: None,
origin: Some(EntryOrigin { device: text.name, kind: origin_kind(text.source) }),
processed: None,
+ corrected: None,
file: None,
timing: None,
glossary_sent: None,
diff --git a/crates/voltip-core/src/settings.rs b/crates/voltip-core/src/settings.rs
index aceb58c3..ed4e0abb 100644
--- a/crates/voltip-core/src/settings.rs
+++ b/crates/voltip-core/src/settings.rs
@@ -261,6 +261,10 @@ pub struct Settings {
/// files written before it) assigns none.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub copy_last_hotkey: Option,
+ /// The 从选中文字学习 hotkey (docs/dictation.md §35) in display form; `None` (the default, and
+ /// files written before it) assigns none.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub learn_hotkey: Option,
/// The lone-key trigger (docs/dictation.md §13.1): a right-hand modifier, Fn or a mouse button
/// that drives a take on its own, next to [`Settings::hotkey`]; `None` (the default, and files
/// written before it) = off.
@@ -508,6 +512,7 @@ impl Default for Settings {
context_sharing: ContextSharing::default(),
edit_hotkey: default_edit_hotkey(),
copy_last_hotkey: None,
+ learn_hotkey: None,
solo_key: None,
pairing_always_on: false,
pinned_scene: None,
@@ -607,7 +612,12 @@ mod tests {
let text = std::fs::read_to_string(root.join(script)).unwrap();
let start = text.find("{\n \"schema\": 1").unwrap_or_else(|| panic!("{script}: no settings heredoc"));
let len = text[start..].find("\n}\n").unwrap() + 2;
- let json = text[start..start + len].replace("$2", "http://127.0.0.1:9").replace("${4:-false}", "false").replace("${3:-paste}", "paste");
+ // `${learn}`: smoke-dictation.sh's optional 从选中文字学习 shortcut (docs/dictation.md §35).
+ let json = text[start..start + len]
+ .replace("$2", "http://127.0.0.1:9")
+ .replace("${4:-false}", "false")
+ .replace("${3:-paste}", "paste")
+ .replace("${learn}", r#""learn_hotkey": "Ctrl+Alt+L","#);
let written: serde_json::Value = serde_json::from_str(&json).unwrap_or_else(|e| panic!("{script}: {e}"));
let settings: Settings = serde_json::from_value(written.clone()).unwrap();
assert_eq!(settings.engines.asr_provider, provider, "{script}");
diff --git a/crates/voltip-core/src/ui.rs b/crates/voltip-core/src/ui.rs
index c2681c0e..4ef42db9 100644
--- a/crates/voltip-core/src/ui.rs
+++ b/crates/voltip-core/src/ui.rs
@@ -97,6 +97,12 @@ pub enum UiEvent {
/// The ignored pairs.
pairs: Vec,
},
+ /// The pill's notice after a 从选中文字学习 (docs/dictation.md §35), full replacement; `None`
+ /// when it went.
+ LearnNotice {
+ /// The notice.
+ notice: Option,
+ },
/// Scenes, full replacement, in matching order (docs/dictation.md §18).
Scenes {
/// Scenes in order.
@@ -451,6 +457,12 @@ pub struct HotkeyStatus {
/// Why the 复制上一条结果 chord is not registered (conflict, pure Wayland, another chord's).
#[serde(default, skip_serializing_if = "Option::is_none")]
pub copy_last_error: Option,
+ /// The 从选中文字学习 chord actually registered (docs/dictation.md §35), if any.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub learn_registered: Option,
+ /// Why the 从选中文字学习 chord is not registered (conflict, pure Wayland, another chord's).
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub learn_error: Option,
/// What the hotkey can do in the session the shell runs in (all `false` until it reports).
#[serde(default)]
pub capabilities: HotkeyCapabilities,
@@ -565,6 +577,10 @@ pub struct UiState {
/// (and left off the wire) until the user ignores one.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub vocabulary_ignored: Vec,
+ /// The pill's notice after a 从选中文字学习 (docs/dictation.md §35); left off the wire while
+ /// there is none.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub learn_notice: Option,
/// Scenes in matching order (docs/dictation.md §18).
#[serde(default)]
pub scenes: Vec,
@@ -654,6 +670,7 @@ impl Default for UiState {
dictionary: Vec::new(),
rules: Vec::new(),
vocabulary_ignored: Vec::new(),
+ learn_notice: None,
scenes: Vec::new(),
presets: Vec::new(),
phone_take: None,
@@ -763,6 +780,10 @@ impl UiState {
self.vocabulary_ignored = pairs.clone();
UiEvent::VocabularyIgnored { pairs }
}
+ CoreEvent::LearnNotice(notice) => {
+ self.learn_notice = notice.clone();
+ UiEvent::LearnNotice { notice }
+ }
CoreEvent::Scenes(scenes) => {
self.scenes = scenes.clone();
UiEvent::Scenes { scenes }
@@ -984,6 +1005,7 @@ mod tests {
preset: None,
origin: None,
processed: None,
+ corrected: None,
file: None,
timing: None,
glossary_sent: None,
diff --git a/crates/voltip-core/src/vocabulary/backtest.rs b/crates/voltip-core/src/vocabulary/backtest.rs
index 72c0fe78..f6aef551 100644
--- a/crates/voltip-core/src/vocabulary/backtest.rs
+++ b/crates/voltip-core/src/vocabulary/backtest.rs
@@ -191,7 +191,15 @@ mod tests {
use crate::vocabulary::{RuleKind, VocabularyHits};
fn take(raw: &str, text: &str, at_ms: u64) -> HistoryText {
- HistoryText { id: Uuid::new_v4(), at_ms, raw_text: raw.into(), text: text.into(), refined: true, vocabulary: VocabularyHits::default() }
+ HistoryText {
+ id: Uuid::new_v4(),
+ at_ms,
+ raw_text: raw.into(),
+ text: text.into(),
+ refined: true,
+ vocabulary: VocabularyHits::default(),
+ corrected: None,
+ }
}
fn entry(term: &str, heard: &[&str]) -> DictionaryEntry {
diff --git a/crates/voltip-core/src/vocabulary/learn.rs b/crates/voltip-core/src/vocabulary/learn.rs
new file mode 100644
index 00000000..0a1ee20a
--- /dev/null
+++ b/crates/voltip-core/src/vocabulary/learn.rs
@@ -0,0 +1,548 @@
+//! 从修改中学习 (docs/dictation.md §35): the user corrected a take in the application it went to,
+//! selected the corrected text and asked Voltip to learn from it. [`match_selection`] finds the
+//! takes the selection is the corrected form of and builds each one's whole corrected text;
+//! [`storable`] says whether a correction holds anything to learn; [`edit_changes`] is what the
+//! learning suggestions take from it: the replacements of the inserted text whose wording the
+//! recogniser wrote and that neither the AI clean-up, the dictionary nor a rule touched — the
+//! errors the clean-up's own changes cannot show (§16.10). Nothing here calls an AI service.
+
+use std::collections::HashSet;
+
+use crate::dictation::insert_check::normalize;
+use crate::history::HistoryText;
+use crate::mcp::candidates::{MAX_DIFF_CELLS, MAX_SCAN_CELLS, Replacement, Replay, SegmentKind, diff_segments_by, diff_tokens, replacements};
+
+/// Recognised takes a selection is compared with, newest first.
+pub const LEARN_TAKES: usize = 50;
+/// The longest selection learned from, in characters.
+pub const MAX_LEARN_SELECTION_CHARS: usize = 8000;
+/// The least similarity at which a selection counts as the corrected form of a take.
+pub const MATCH_MIN: f64 = 0.4;
+/// Takes this close in similarity to the best one count as tied; the newer one wins.
+pub const MATCH_TIE: f64 = 0.05;
+/// Most takes one selection corrects (it may cover a few takes dictated one after another).
+pub const MAX_TAKES_PER_SELECTION: usize = 5;
+/// The longest run, in characters other than white space, compared character by character inside
+/// one replacement; a longer one adds nothing to the similarity.
+const INNER_MAX: usize = 64;
+
+/// One replacement the user made that the learning suggestions learn from: the recogniser's
+/// wording and the user's, where the first sits in the recognised text and the second in the
+/// corrected text (characters).
+#[derive(Clone, Debug, PartialEq, Eq)]
+pub(crate) struct EditChange {
+ pub(crate) heard: String,
+ pub(crate) right: String,
+ pub(crate) heard_at: usize,
+ pub(crate) right_at: usize,
+}
+
+/// The replacements of `take.text` → `corrected` whose inserted wording is the recogniser's: it
+/// lies where `raw_text` → `text` keeps the tokens (ASCII case aside, so a clean-up that only
+/// changed the case still counts), with the recognised wording and its offset in `raw_text`.
+fn recognised_changes(take: &HistoryText, corrected: &str) -> Vec<(Replacement, usize)> {
+ let Some(found) = replacements(&take.text, corrected) else { return Vec::new() };
+ if found.is_empty() {
+ return Vec::new();
+ }
+ let Some(segments) = diff_segments_by(&take.raw_text, &take.text, |a, b| a.eq_ignore_ascii_case(b)) else { return Vec::new() };
+ // For each character of `text`, the character of `raw_text` it is, where the two keep it.
+ let mut to_raw: Vec