Skip to content

Commit 2c859cc

Browse files
fix(voice): insert Unicode word boundaries for dictation
Dictation only added a space around the inserted transcript for English or autodetected recordings, and only next to ASCII letters, so Korean, Hindi, German and other spaced languages ran into the surrounding draft text. Any language that writes words with spaces now gets a boundary next to a letter, mark or digit on either side of the caret. Japanese, Chinese, Thai and the other spaceless scripts stay joined, and an autodetected recording still only spaces next to Latin text. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 3d78e91 commit 2c859cc

2 files changed

Lines changed: 35 additions & 14 deletions

File tree

‎packages/client-runtime/src/voice-input/controller.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ describe("resolveTranscriptCommit", () => {
234234
it.each([
235235
["Hello", "world", "Hello world"],
236236
["Hello", "世界", "Hello世界"],
237+
["Hello", "𝟚", "Hello 𝟚"],
237238
["你好", "世界", "你好世界"],
238239
])(
239240
"preserves script boundaries with an autodetected language: %s + %s",
@@ -246,6 +247,24 @@ describe("resolveTranscriptCommit", () => {
246247
},
247248
);
248249

250+
it.each([
251+
["Korean", "코드를", 3, "검토해 주세요", "ko_KR", "코드를 검토해 주세요"],
252+
["a supplementary letter", "𞤀", 2, "𞤦", "ff-Adlm", "𞤀 𞤦"],
253+
["a trailing combining mark", "नमस्ते", 6, "दुनिया", "hi-IN", "नमस्ते दुनिया"],
254+
["a non-ASCII word after the caret", "Änderungen", 0, "Weitere", "de_DE", "Weitere Änderungen"],
255+
["spaceless Japanese", "修正キャッシュ", 7, "テストも", "ja-JP", "修正キャッシュテストも"],
256+
["spaceless Thai", "โลก", 0, "สวัสดี", "th-TH", "สวัสดีโลก"],
257+
])(
258+
"inserts at a caret with the recorded language's word spacing: %s",
259+
(_case, text, caret, spoken, locale, expected) => {
260+
const captured = draft({ text, selection: { start: caret, end: caret } });
261+
expect(resolveTranscriptCommit(captured, captured, spoken, locale)).toMatchObject({
262+
kind: "commit",
263+
text: expected,
264+
});
265+
},
266+
);
267+
249268
it("replaces the recorded UTF-16 selection around emoji and composer tokens", () => {
250269
const text = "Fix 🧪 then $review please";
251270
const tokenStart = text.indexOf("$review");

‎packages/client-runtime/src/voice-input/controller.ts‎

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ type TranscriptCommitResult =
107107
| { readonly kind: "stale" }
108108
| { readonly kind: "empty" };
109109

110+
const WORD_SPACELESS_LANGUAGES = new Set(["ja", "zh", "yue", "th", "lo", "km", "my"]);
111+
110112
export function resolveTranscriptCommit(
111113
captured: VoiceDraftSnapshot,
112114
current: VoiceDraftSnapshot | null,
@@ -128,23 +130,23 @@ export function resolveTranscriptCommit(
128130
}
129131

130132
const isEmptySelection = captured.selection.start === captured.selection.end;
131-
const normalizedLocale = locale.replaceAll("_", "-").toLowerCase();
132-
const usesEnglishSpacing = normalizedLocale === "en" || normalizedLocale.startsWith("en-");
133+
const language = locale.split(/[-_]/)[0]!.toLowerCase();
133134
let insertion = replacement;
134-
const autoSpacing = normalizedLocale === "auto";
135-
if (isEmptySelection && (usesEnglishSpacing || autoSpacing)) {
136-
const left = captured.text[captured.selection.start - 1];
137-
const right = captured.text[captured.selection.start];
135+
if (isEmptySelection && !WORD_SPACELESS_LANGUAGES.has(language)) {
136+
const auto = language === "auto";
137+
const caret = captured.selection.start;
138+
const left = captured.text.slice(Math.max(0, caret - 2), caret);
139+
const right = captured.text.slice(caret, caret + 2);
140+
const leftEdge = auto ? /[A-Za-z0-9.!?,:;)\]}'"]$/ : /(?:\p{L}|\p{M}|\p{N}|[.!?,:;)\]}'"])$/u;
141+
const rightEdge = auto ? /^[A-Za-z0-9([{'"]/ : /^[\p{L}\p{M}\p{N}([{'"]/u;
138142
const leftNeedsBoundary =
139-
(!autoSpacing || /^[\p{Script=Latin}\p{N}]/u.test(replacement)) &&
140-
left !== undefined &&
141-
/[A-Za-z0-9.!?,:;)\]}'"]/.test(left) &&
142-
(right === undefined || /\s/.test(right));
143+
(!auto || /^[\p{Script=Latin}\p{N}]/u.test(replacement)) &&
144+
leftEdge.test(left) &&
145+
(!right || /^\s/.test(right));
143146
const rightNeedsBoundary =
144-
(!autoSpacing || /[\p{Script=Latin}\p{N}]$/u.test(replacement)) &&
145-
right !== undefined &&
146-
/[A-Za-z0-9([{'"]/.test(right) &&
147-
(left === undefined || /\s/.test(left));
147+
(!auto || /(?:\p{Script=Latin}|\p{N})$/u.test(replacement)) &&
148+
rightEdge.test(right) &&
149+
(!left || /\s$/.test(left));
148150
insertion = `${leftNeedsBoundary ? " " : ""}${replacement}${rightNeedsBoundary ? " " : ""}`;
149151
}
150152

0 commit comments

Comments
 (0)