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-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/shared/dictation-transcript-spacing.ts b/src/shared/dictation-transcript-spacing.ts index 8af1a19cf3d..2c2a98ff874 100644 --- a/src/shared/dictation-transcript-spacing.ts +++ b/src/shared/dictation-transcript-spacing.ts @@ -2,8 +2,8 @@ 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 +// Chinese and Japanese don't space words; Korean does, so Hangul spaces like Latin text. +const UNSPACED_SCRIPT_CHAR_RE = /^[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}]$/u const NO_SPACE_BEFORE_CHAR_RE = /^[,.;:!?%。,、!?;:))\]}”’»]$/u const NO_SPACE_AFTER_CHAR_RE = /^[([{(《「『“‘«„]$/u const STRAIGHT_QUOTE_RE = /["'`]/ @@ -20,7 +20,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 +28,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..51ab7fae18b --- /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: '他们来自欧洲' }, + { type: 'partial', text: '然后' } + ]) + ).toMatchObject({ + committed: '西班牙人开始了殖民时期。他们来自欧洲', + text: '西班牙人开始了殖民时期。他们来自欧洲然后' + }) + 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()) } } }