feat: add dictation
This commit is contained in:
37
apps/web/src/lib/natural-input/dictation.test.ts
Normal file
37
apps/web/src/lib/natural-input/dictation.test.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
import { splitDictation } from './dictation'
|
||||
|
||||
describe('splitDictation', () => {
|
||||
it('splits on "nuevo ítem" and its variants', () => {
|
||||
expect(
|
||||
splitDictation([
|
||||
'leche descremada nuevo ítem pan Nuevo item 2 kilos de papa, otro artículo yerba',
|
||||
]),
|
||||
).toEqual(['leche descremada', 'pan', '2 kilos de papa', 'yerba'])
|
||||
})
|
||||
|
||||
it('takes every segment and line as a separate phrase', () => {
|
||||
expect(splitDictation(['Leche.', ' pan\nqueso rallado '])).toEqual([
|
||||
'Leche',
|
||||
'pan',
|
||||
'queso rallado',
|
||||
])
|
||||
})
|
||||
|
||||
it('collapses spaces and drops empty phrases', () => {
|
||||
expect(splitDictation([' dulce de leche ', '', 'nuevo ítem', '...'])).toEqual([
|
||||
'dulce de leche',
|
||||
])
|
||||
})
|
||||
|
||||
it('keeps a repeated phrase once, ignoring case and accents', () => {
|
||||
expect(splitDictation(['jamón', 'Jamon nuevo ítem pan', 'pan'])).toEqual(['jamón', 'pan'])
|
||||
})
|
||||
|
||||
it('does not split words that only contain the keywords', () => {
|
||||
expect(splitDictation(['productos nuevos de limpieza'])).toEqual([
|
||||
'productos nuevos de limpieza',
|
||||
])
|
||||
})
|
||||
})
|
||||
35
apps/web/src/lib/natural-input/dictation.ts
Normal file
35
apps/web/src/lib/natural-input/dictation.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { normalizeWord } from './text'
|
||||
|
||||
/**
|
||||
* What the user says to start another item: "nuevo ítem", "otro artículo", "nuevo
|
||||
* producto". The punctuation the recognizer puts around it goes with it.
|
||||
*/
|
||||
const NEXT_ITEM =
|
||||
/[\s,.;:]*(?<!\p{L})(?:nuevo|otro)\s+(?:[íi]tem|art[íi]culo|producto)(?!\p{L})[\s,.;:]*/giu
|
||||
|
||||
/** Punctuation the recognizer adds around a phrase ("Leche."), not part of the item. */
|
||||
const EDGE_PUNCTUATION = /^[\s,.;:]+|[\s,.;:]+$/g
|
||||
|
||||
/**
|
||||
* The phrases of a dictation, one per item said: each segment (a pause or a line break,
|
||||
* as the recognizer delivers them) is also split on "nuevo ítem". Spaces are collapsed,
|
||||
* empty phrases dropped and a phrase repeated in the same dictation kept once (ignoring
|
||||
* case and accents); the words themselves are left as heard.
|
||||
*/
|
||||
export function splitDictation(segments: string[]): string[] {
|
||||
const phrases: string[] = []
|
||||
const seen = new Set<string>()
|
||||
|
||||
for (const segment of segments) {
|
||||
for (const part of segment.split(/\r?\n/).flatMap((line) => line.split(NEXT_ITEM))) {
|
||||
const phrase = part.replace(/\s+/g, ' ').replace(EDGE_PUNCTUATION, '')
|
||||
const key = normalizeWord(phrase)
|
||||
if (!phrase || seen.has(key)) continue
|
||||
|
||||
seen.add(key)
|
||||
phrases.push(phrase)
|
||||
}
|
||||
}
|
||||
|
||||
return phrases
|
||||
}
|
||||
34
apps/web/src/lib/speech-recognition.ts
Normal file
34
apps/web/src/lib/speech-recognition.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
/** The slice of the Web Speech API the app uses (TypeScript's DOM lib does not ship it). */
|
||||
export type SpeechRecognitionResultLike = { isFinal: boolean; 0: { transcript: string } }
|
||||
|
||||
export type SpeechRecognitionLike = {
|
||||
lang: string
|
||||
interimResults: boolean
|
||||
continuous: boolean
|
||||
maxAlternatives: number
|
||||
onresult: ((event: { results: ArrayLike<SpeechRecognitionResultLike> }) => void) | null
|
||||
onerror: ((event: { error: string }) => void) | null
|
||||
onend: (() => void) | null
|
||||
start: () => void
|
||||
stop: () => void
|
||||
abort: () => void
|
||||
}
|
||||
|
||||
export type SpeechRecognitionConstructor = new () => SpeechRecognitionLike
|
||||
|
||||
/** `SpeechRecognition`, or Safari's `webkitSpeechRecognition`, or `null` when there is none. */
|
||||
export function getSpeechRecognition(): SpeechRecognitionConstructor | null {
|
||||
if (typeof window === 'undefined') return null
|
||||
|
||||
const speechWindow = window as Window & {
|
||||
SpeechRecognition?: SpeechRecognitionConstructor
|
||||
webkitSpeechRecognition?: SpeechRecognitionConstructor
|
||||
}
|
||||
|
||||
return speechWindow.SpeechRecognition ?? speechWindow.webkitSpeechRecognition ?? null
|
||||
}
|
||||
|
||||
/** Whether the page runs as an app installed on the iOS home screen. */
|
||||
export function isIosStandalone(): boolean {
|
||||
return (navigator as Navigator & { standalone?: boolean }).standalone === true
|
||||
}
|
||||
Reference in New Issue
Block a user