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.
This commit is contained in:
Kelvin Amoaba
2026-11-09 19:27:51 +00:00
parent 58733086c9
commit 2cd6d7b7fa
5 changed files with 66 additions and 15 deletions
@@ -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
>
<span className="shrink-0">
{committed}
{committed && tentative ? ' ' : null}
{dictationSeparators(committed, tentative, '').leading}
<span className="text-muted-foreground">{tentative}</span>
</span>
</span>
@@ -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)
})
}
+11 -6
View File
@@ -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,
+44
View File
@@ -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')
})
})
+6 -5
View File
@@ -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())
}
}
}