From 1bf2276bdd8630106d7c92cef6633a1eae2e285f Mon Sep 17 00:00:00 2001 From: Kelvin Amoaba <97001695+AmoabaKelvin@users.noreply.github.com> Date: Mon, 9 Nov 2026 19:39:34 +0000 Subject: [PATCH] fix(dictation): match the pill's Chinese and Japanese phrases to the inserted text, and space Korean ones (#40455) * fix(dictation): join Chinese and Japanese phrases without a space, and space Korean ones The shared transcript fold joined every phrase with a space, so the pill and phone replies showed Chinese and Japanese text with spaces the inserted text doesn't have. It now joins with the same rule as insertion. That rule treated Hangul like Chinese and Japanese, so Korean phrases were inserted with no space between sentences; Korean spaces words, so Hangul now spaces like Latin text. * fix(dictation): don't space after Chinese and Japanese full-width punctuation * fix(dictation): join retried phrases with the same spacing rule * fix(dictation): escape the spacing rule's non-ASCII marks for the orcad bundle The shared fold now pulls the spacing rule into the orcad server bundle, which must stay ASCII. A rich-draft test pinned Hangul joining without a space; it now expects the Korean spacing. --- .../dictation/DictationIndicator.tsx | 3 +- .../dictation/dictation-kept-recording.ts | 6 ++- .../dictation-transcript-delivery.ts | 6 +-- .../inline-rich-draft-consumers.test.tsx | 2 +- src/shared/dictation-transcript-spacing.ts | 23 ++++++---- src/shared/dictation-transcript.test.ts | 44 +++++++++++++++++++ src/shared/dictation-transcript.ts | 11 ++--- 7 files changed, 76 insertions(+), 19 deletions(-) create mode 100644 src/shared/dictation-transcript.test.ts diff --git a/src/renderer/src/components/dictation/DictationIndicator.tsx b/src/renderer/src/components/dictation/DictationIndicator.tsx index 9edba96cef8..f8559b598ec 100644 --- a/src/renderer/src/components/dictation/DictationIndicator.tsx +++ b/src/renderer/src/components/dictation/DictationIndicator.tsx @@ -10,6 +10,7 @@ import { cn } from '@/lib/utils' import { useAppStore } from '@/store' import { ArrowUp, Check, CornerDownLeft, Loader2 } from 'lucide-react' import { isDictationPillShown } from '../../../../shared/dictation-session-machine' +import { dictationSeparators } from '../../../../shared/dictation-transcript-spacing' import { CLOUD_DICTATION_MAX_SECONDS } from '../../../../shared/rpc-contract/speech-params' import type { SpeechModelManifest } from '../../../../shared/speech-types' import { dispatchDictationControl } from './dictation-control-events' @@ -207,7 +208,7 @@ function DictationPill({ captureLive, anchor, failure, queued }: DictationIndica > {committed} - {committed && tentative ? ' ' : null} + {dictationSeparators(committed, tentative, '').leading} {tentative} diff --git a/src/renderer/src/components/dictation/dictation-kept-recording.ts b/src/renderer/src/components/dictation/dictation-kept-recording.ts index f0b7ca42b0d..7981c21c90f 100644 --- a/src/renderer/src/components/dictation/dictation-kept-recording.ts +++ b/src/renderer/src/components/dictation/dictation-kept-recording.ts @@ -6,6 +6,7 @@ import type { DictationContext } from '../../../../shared/speech-types' import type { DictationInsertionTarget } from './dictation-insertion-target' import type { DictationTranscriptDelivery } from './dictation-transcript-delivery' import { trimLandedOverlap } from './dictation-retry-overlap' +import { appendDictationText } from '../../../../shared/dictation-transcript-spacing' import { showRetryOverlapUnmatchedToast } from './dictation-toasts' // The cloud cap, ten minutes, is about 38 MB of 16 kHz float audio. @@ -92,7 +93,10 @@ function* joinIntoBlocks(chunks: Float32Array[]): Generator { // The retry repeats words that landed before the failure; only what follows them is typed. function insertAfterLanded(recording: DictationKeptRecording, retried: string[]): void { - const { text, matched } = trimLandedOverlap(recording.deliver.landedText(), retried.join(' ')) + const { text, matched } = trimLandedOverlap( + recording.deliver.landedText(), + retried.reduce(appendDictationText, '') + ) if (text) { recording.deliver(text, recording.target) } diff --git a/src/renderer/src/components/dictation/dictation-transcript-delivery.ts b/src/renderer/src/components/dictation/dictation-transcript-delivery.ts index 9680bded9e1..0b890d3bf31 100644 --- a/src/renderer/src/components/dictation/dictation-transcript-delivery.ts +++ b/src/renderer/src/components/dictation/dictation-transcript-delivery.ts @@ -1,7 +1,7 @@ import { toast } from 'sonner' import { translate } from '@/i18n/i18n' import { describeClipboardWriteFailure } from '@/lib/clipboard-write-failure' -import { dictationSeparators } from '../../../../shared/dictation-transcript-spacing' +import { appendDictationText } from '../../../../shared/dictation-transcript-spacing' import { insertText, type DictationInsertionTarget } from './dictation-insertion-target' export type DictationTranscriptDelivery = (( @@ -25,10 +25,10 @@ export function createDictationTranscriptDelivery(): DictationTranscriptDelivery const landed = target ? await insertText(text, target, lastLanded).catch(() => false) : false if (landed) { lastLanded = text - allLanded += `${dictationSeparators(allLanded, text, '').leading}${text}` + allLanded = appendDictationText(allLanded, text) return } - missed += `${dictationSeparators(missed, text, '').leading}${text}` + missed = appendDictationText(missed, text) await copyMissedDictation(missed, target !== null) }) } diff --git a/src/renderer/src/components/native-chat/composer/editor/inline-rich-draft-consumers.test.tsx b/src/renderer/src/components/native-chat/composer/editor/inline-rich-draft-consumers.test.tsx index fa1ef624ae0..7cb534c294d 100644 --- a/src/renderer/src/components/native-chat/composer/editor/inline-rich-draft-consumers.test.tsx +++ b/src/renderer/src/components/native-chat/composer/editor/inline-rich-draft-consumers.test.tsx @@ -563,7 +563,7 @@ it.each(['mounted', 'success', 'unmounted', 'ime', 'cancel'] as const)( act(() => bottom.view.dispatch(bottom.state.tr.insertText('가', bottom.state.doc.content.size - 1)) ) - expect(nextSend().blocks).toEqual([{ type: 'text', text: 'first\n\necho가echo' }]) + expect(nextSend().blocks).toEqual([{ type: 'text', text: 'first\n\necho가 echo' }]) } else { expect(nextSend().blocks).toEqual([ { diff --git a/src/shared/dictation-transcript-spacing.ts b/src/shared/dictation-transcript-spacing.ts index 8af1a19cf3d..8aefa5f3374 100644 --- a/src/shared/dictation-transcript-spacing.ts +++ b/src/shared/dictation-transcript-spacing.ts @@ -2,10 +2,12 @@ export const DICTATION_TEXT_BLOCK_SELECTOR = 'p, li, h1, h2, h3, h4, h5, h6, pre, blockquote, td, th, div' -const CJK_BOUNDARY_CHAR_RE = - /^[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}]$/u -const NO_SPACE_BEFORE_CHAR_RE = /^[,.;:!?%。,、!?;:))\]}”’»]$/u -const NO_SPACE_AFTER_CHAR_RE = /^[([{(《「『“‘«„]$/u +// Chinese and Japanese don't space words or after their full-width punctuation; Korean does, so Hangul spaces like Latin text. +const UNSPACED_SCRIPT_CHAR_RE = + /^[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\u3000-\u303f\u30fb\u30fc\uff00-\uff9f\uffe0-\uffef]$/u +const NO_SPACE_BEFORE_CHAR_RE = + /^[,.;:!?%\u3002\uff0c\u3001\uff01\uff1f\uff1b\uff1a\uff09)\]}\u201d\u2019\u00bb]$/u +const NO_SPACE_AFTER_CHAR_RE = /^[([{\uff08\u300a\u300c\u300e\u201c\u2018\u00ab\u201e]$/u const STRAIGHT_QUOTE_RE = /["'`]/ // A straight quote opens after a space or the start, and closes before a space, punctuation or the end; anything else is an apostrophe. const opensQuote = (before: string): boolean => /(^|[\s([{])["'`]$/.test(before) @@ -20,7 +22,7 @@ function getLastChar(text: string): string { return Array.from(text.slice(-2)).at(-1) ?? '' } -// Dictated text and its neighbour touch with no space between: one is empty or already spaced, either is CJK, or punctuation binds them. +// Dictated text and its neighbour touch with no space between: one is empty or already spaced, either is Chinese or Japanese, or punctuation binds them. function dictationJoinsWithoutSpace(previousText: string, nextText: string): boolean { if (!previousText || !nextText || /\s$/.test(previousText) || /^\s/.test(nextText)) { return true @@ -28,14 +30,19 @@ function dictationJoinsWithoutSpace(previousText: string, nextText: string): boo const previousChar = getLastChar(previousText) const nextChar = getFirstChar(nextText) return ( - CJK_BOUNDARY_CHAR_RE.test(previousChar) || - CJK_BOUNDARY_CHAR_RE.test(nextChar) || + UNSPACED_SCRIPT_CHAR_RE.test(previousChar) || + UNSPACED_SCRIPT_CHAR_RE.test(nextChar) || NO_SPACE_BEFORE_CHAR_RE.test(nextChar) || NO_SPACE_AFTER_CHAR_RE.test(previousChar) ) } -/** The spaces dictated text needs between the text before and after where it lands, unless punctuation, CJK or a quote it sits just inside joins them. */ +/** `before` and `text` joined with the space dictation puts between them, if any. */ +export function appendDictationText(before: string, text: string): string { + return `${before}${dictationSeparators(before, text, '').leading}${text}` +} + +/** The spaces dictated text needs between the text before and after where it lands, unless punctuation, Chinese or Japanese, or a quote it sits just inside joins them. */ export function dictationSeparators( before: string, text: string, diff --git a/src/shared/dictation-transcript.test.ts b/src/shared/dictation-transcript.test.ts new file mode 100644 index 00000000000..2857d42a824 --- /dev/null +++ b/src/shared/dictation-transcript.test.ts @@ -0,0 +1,44 @@ +import { describe, expect, it } from 'vitest' +import { DictationTranscript } from './dictation-transcript' + +function fold(events: { type: 'partial' | 'final'; text: string }[]) { + const transcript = new DictationTranscript() + events.forEach((event) => transcript.accept(event)) + return transcript.snapshot() +} + +describe('DictationTranscript', () => { + it('joins Chinese and Japanese phrases without a space, as they are inserted', () => { + expect( + fold([ + { type: 'final', text: '西班牙人开始了殖民时期。' }, + { type: 'final', text: '2023年他们来自欧洲' }, + { type: 'partial', text: '然后' } + ]) + ).toMatchObject({ + committed: '西班牙人开始了殖民时期。2023年他们来自欧洲', + text: '西班牙人开始了殖民时期。2023年他们来自欧洲然后' + }) + expect( + fold([ + { type: 'final', text: '今日は晴れです。' }, + { type: 'final', text: '散歩に行きます。' } + ]).committed + ).toBe('今日は晴れです。散歩に行きます。') + }) + + it('spaces Korean and Latin phrases', () => { + expect( + fold([ + { type: 'final', text: '안녕하세요.' }, + { type: 'final', text: '감사합니다.' } + ]).committed + ).toBe('안녕하세요. 감사합니다.') + expect( + fold([ + { type: 'final', text: 'Open the file.' }, + { type: 'partial', text: 'then save' } + ]).text + ).toBe('Open the file. then save') + }) +}) diff --git a/src/shared/dictation-transcript.ts b/src/shared/dictation-transcript.ts index 2117e82fee8..1f85170f433 100644 --- a/src/shared/dictation-transcript.ts +++ b/src/shared/dictation-transcript.ts @@ -1,3 +1,5 @@ +import { appendDictationText } from './dictation-transcript-spacing' + /** The text so far mid-dictation, for the desktop pill and phone replies: committed text never changes, tentative text may still be revised. */ export type DictationTranscriptSnapshot = { /** Grows with every change, so a client keeps the newest snapshot when replies arrive out of order. */ @@ -10,7 +12,7 @@ export type DictationTranscriptSnapshot = { /** One dictation's speech events folded into the snapshot its chunk replies report. */ export class DictationTranscript { - private readonly committed: string[] = [] + private committed = '' private tentative = '' private revisionCount = 0 @@ -25,7 +27,7 @@ export class DictationTranscript { } else if (event.type === 'final') { const text = event.text?.trim() if (text) { - this.committed.push(text) + this.committed = appendDictationText(this.committed, text) this.tentative = '' this.revisionCount += 1 } @@ -33,12 +35,11 @@ export class DictationTranscript { } snapshot(): DictationTranscriptSnapshot { - const committed = this.committed.join(' ') return { revision: this.revisionCount, - committed, + committed: this.committed, tentative: this.tentative, - text: [committed, this.tentative].join(' ').trim() + text: appendDictationText(this.committed, this.tentative.trim()) } } }