fix(voice): browser speech recognition on Android no longer repeats phrases
MindSift CI & Docker Release / Build, Test in Container & Push (push) Successful in 1m40s
MindSift CI & Docker Release / Build, Test in Container & Push (push) Successful in 1m40s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
32bb92ecf8
commit
8c2bfe2cee
7 files changed
+63
-8
No files matched your search
Generated
+2
-2
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "mindsift-frontend",
|
||||
"version": "0.22.1",
|
||||
"version": "0.22.2",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "mindsift-frontend",
|
||||
"version": "0.22.1",
|
||||
"version": "0.22.2",
|
||||
"license": "Unlicense",
|
||||
"dependencies": {
|
||||
"@fontsource-variable/inter": "5.3.0",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "mindsift-frontend",
|
||||
"private": true,
|
||||
"version": "0.22.1",
|
||||
"version": "0.22.2",
|
||||
"license": "Unlicense",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { joinSpeechResults } from '../voice';
|
||||
|
||||
describe('joinSpeechResults (browser speech recognition)', () => {
|
||||
it('Android: growing interim states of one phrase count once', () => {
|
||||
expect(joinSpeechResults(['Test', 'Test', 'Test', 'Test', 'Test', 'Test 1 2 3'])).toBe('Test 1 2 3');
|
||||
expect(joinSpeechResults(['Test', 'Test 1', 'Test 1 2', 'Test 1 2 3'])).toBe('Test 1 2 3');
|
||||
});
|
||||
|
||||
it('desktop: consecutive pieces are joined', () => {
|
||||
expect(joinSpeechResults(['Milch kaufen', ' und Brot'])).toBe('Milch kaufen und Brot');
|
||||
});
|
||||
|
||||
it('Android: a new phrase after a finished one is appended', () => {
|
||||
expect(joinSpeechResults(['Milch', 'Milch kaufen', ' und Brot', ' und Brot holen'])).toBe('Milch kaufen und Brot holen');
|
||||
});
|
||||
|
||||
it('ignores empty results and earlier shorter states', () => {
|
||||
expect(joinSpeechResults(['', 'Test 1 2', 'Test 1', ''])).toBe('Test 1 2');
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,6 @@
|
||||
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||
import { api } from './api';
|
||||
import { browserRecognition, currentVoiceConfig, EffectiveVoiceMode, loadVoiceConfig, VoiceRecorder } from './voice';
|
||||
import { browserRecognition, currentVoiceConfig, EffectiveVoiceMode, joinSpeechResults, loadVoiceConfig, VoiceRecorder } from './voice';
|
||||
|
||||
export type VoiceState = 'idle' | 'listening' | 'transcribing';
|
||||
|
||||
@@ -234,8 +234,9 @@ export function useVoiceInput({ onText, onError, continuous = false, live = fals
|
||||
recognition.interimResults = true;
|
||||
recognition.continuous = continuous || live;
|
||||
recognition.onresult = (event: any) => {
|
||||
let sessionText = '';
|
||||
for (let i = 0; i < event.results.length; i++) sessionText += event.results[i][0].transcript;
|
||||
const parts: string[] = [];
|
||||
for (let i = 0; i < event.results.length; i++) parts.push(event.results[i][0].transcript);
|
||||
const sessionText = joinSpeechResults(parts);
|
||||
if (live && baseTranscriptRef.current) {
|
||||
const base = baseTranscriptRef.current.trimEnd();
|
||||
const next = sessionText.trimStart();
|
||||
|
||||
@@ -186,3 +186,27 @@ export class VoiceRecorder {
|
||||
this.analyser = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Text of one speech recognition session from its results. Desktop browsers deliver consecutive
|
||||
* pieces ("Test 1 2 3", " und weiter"); Chrome on Android also keeps the growing interim states of
|
||||
* the same phrase as results of their own ("Test", "Test", "Test 1 2 3"). A result that only
|
||||
* continues the one before it therefore replaces it instead of being appended.
|
||||
*/
|
||||
export function joinSpeechResults(transcripts: string[]): string {
|
||||
const norm = (s: string) => s.trim().toLowerCase();
|
||||
let text = '';
|
||||
let last = '';
|
||||
for (const t of transcripts) {
|
||||
if (!t) continue;
|
||||
if (last && norm(t).startsWith(norm(last))) {
|
||||
text = text.slice(0, text.length - last.length) + t; // grown version of the same phrase
|
||||
} else if (last && norm(last).startsWith(norm(t))) {
|
||||
continue; // an earlier, shorter state of it
|
||||
} else {
|
||||
text += t;
|
||||
}
|
||||
last = t;
|
||||
}
|
||||
return text;
|
||||
}
|
||||
Reference in new issue
Block a user