Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { describe, expect, it } from '@jest/globals';
import { appendAutoTranscription } from './appendAutoTranscription';

describe('appendAutoTranscription', () => {
it('returns undefined when the cleaned text is already in the pane', () => {
expect(appendAutoTranscription('hello', 'hello world')).toBeUndefined();
});

it('strips timestamp prefixes without clipping a longer digit run', () => {
expect(appendAutoTranscription('0:01.200: hello 1:02.300: world', '')).toBe(
' hello world'
);
expect(appendAutoTranscription('12345678901:1.2: hi', '')).toBe(
' 12345678901:1.2: hi'
);
});

it('skips a leading single-verse marker already in the pane', () => {
expect(appendAutoTranscription('\\v 3 hello', '\\v 3 ')).toBe('hello');
});

// Pre-inserted range marker (Mark Verses → Transcribe) must not be half-stripped
// into "1-5 -5" when ASR returns the same range plus later verses.
it('keeps a verse-range marker intact when that range is already in the pane', () => {
const added = appendAutoTranscription(
'\\v 1-5 one two \\v 6-7 three',
'\\v 1-5 '
);
expect(added).toBe('one two \\v 6-7 three');
expect(added).not.toMatch(/-5/);
});

it('keeps a letter-suffix marker intact when already in the pane', () => {
expect(appendAutoTranscription('\\v 1a hello', '\\v 1a ')).toBe('hello');
});

it('adds a leading space when the pane text does not end with one', () => {
expect(appendAutoTranscription('more', 'hi')).toBe(' more');
});

it('adds no leading space when the pane text already ends with one', () => {
expect(appendAutoTranscription('more', 'hi ')).toBe('more');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Text to append from an ASR result, or undefined when curTrans already has it.
* Strips timestamp prefixes and skips a leading verse marker already in the pane.
*/
export function appendAutoTranscription(
trans: string,
curTrans: string
): string | undefined {
// {1,10} keeps this unanchored replace linear. (?<!\d) stops a
// longer digit run from matching at a suffix and leaving a stray digit.
const cleanTrans = trans
.replace(/(?<!\d)\d{1,10}:\d{1,10}\.\d{1,10}: /g, '')
.trim();
if (curTrans.includes(cleanTrans)) return undefined;
// Match ranges/suffixes (1-5, 1a) so a pre-inserted \v 1-5 is not
// treated as \v 1 and left as a stray "-5" in the pane.
const m = /\\v (\d+[a-f]?-?\d*[a-f]?)\s?/.exec(cleanTrans);
const index = m && curTrans.includes(m[0]) ? m[0].length : 0;
const space = /\s$/.test(curTrans) ? '' : ' ';
return space + cleanTrans.substring(index);
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ describe('useTranscribeAsr', () => {
expect(result.current.asrProgressVisible).toBe(false);
});

it('strips timestamp prefixes without clipping a longer digit run', async () => {
it('forwards cleaned ASR text through onTextAdd', async () => {
getAsrSettings.mockResolvedValue({
target: 'alphabet',
language: { bcp47: 'en', languageName: 'English' },
Expand All @@ -211,13 +211,35 @@ describe('useTranscribeAsr', () => {
await waitFor(() => expect(result.current.asrSettings?.asrIso).toBe('eng'));

act(() => {
result.current.handleAutoTranscribe('0:01.200: hello 1:02.300: world');
result.current.handleAutoTranscribe('0:01.200: hello');
});
act(() => {
result.current.handleAutoTranscribe('12345678901:1.2: hi');

expect(onTextAdd).toHaveBeenCalledWith(' hello', true);
});

it('derives phonetic from the active ASR target', async () => {
getAsrSettings.mockResolvedValue({
target: 'Phonetic',
language: { bcp47: 'en', languageName: 'English' },
asrIso: 'eng',
dialect: undefined,
});
const { result } = renderHook(() =>
useTranscribeAsr({
team: undefined,
sharedStr: { mustBeOnline: 'online' } as never,
tPlayer: { recognizeSpeech: 'Recognize {0}' } as never,
showMessage: jest.fn(),
onTextAdd: jest.fn(),
getCurrentText: () => '',
})
);
await waitFor(() => expect(result.current.asrSettings?.asrIso).toBe('eng'));
expect(result.current.phonetic).toBe(true);

act(() => result.current.handleTranscribe());

expect(onTextAdd).toHaveBeenNthCalledWith(1, ' hello world', true);
expect(onTextAdd).toHaveBeenNthCalledWith(2, ' 12345678901:1.2: hi', true);
expect(result.current.asrOverride?.target).toBe('Phonetic');
expect(result.current.phonetic).toBe(true);
});
});
29 changes: 8 additions & 21 deletions web/src/components/PassageDetail/transcribe/useTranscribeAsr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { useGlobal } from '../../../context/useGlobal';
import { transcriberSelector } from '../../../selector';
import { AsrTarget } from '../../../business/asr/AsrTarget';
import { IAsrState, asrStatesEqual } from '../../../business/asr/asrState';
import { appendAutoTranscription } from './appendAutoTranscription';

export interface UseTranscribeAsrProps {
team: OrganizationD | undefined;
Expand Down Expand Up @@ -94,10 +95,10 @@ export function useTranscribeAsr({

const [asrProgressVisible, setAsrProgressVisible] = useState(false);
const [asrLangVisible, setAsrLangVisible] = useState(false);
const [phonetic, setPhonetic] = useState(false);
const [asrOverride, setAsrOverride] = useState<IAsrState | undefined>(
undefined
);
const phonetic = (asrOverride ?? asrSettings)?.target === AsrTarget.phonetic;

const asrTip = useMemo(() => {
return (tPlayer.recognizeSpeech + '\u00A0\u00A0').replace(
Expand All @@ -111,15 +112,10 @@ export function useTranscribeAsr({
);
}, [tPlayer.recognizeSpeech, asrSettings, getName]);

const startAsr = useCallback(
(asrOverrideState?: IAsrState) => {
const asr = asrOverrideState ?? asrSettings;
setAsrOverride(asrOverrideState);
setPhonetic(asr?.target === AsrTarget.phonetic);
setAsrProgressVisible(true);
},
[asrSettings]
);
const startAsr = useCallback((asrOverrideState?: IAsrState) => {
setAsrOverride(asrOverrideState);
setAsrProgressVisible(true);
}, []);

const openAsrLanguageSettings = useCallback(() => {
setAsrLangVisible(true);
Expand Down Expand Up @@ -163,17 +159,8 @@ export function useTranscribeAsr({

const handleAutoTranscribe = useCallback(
(trans: string) => {
// {1,10} keeps this unanchored replace linear. (?<!\d) stops a
// longer digit run from matching at a suffix and leaving a stray digit.
const cleanTrans = trans
.replace(/(?<!\d)\d{1,10}:\d{1,10}\.\d{1,10}: /g, '')
.trim();
const curTrans = getCurrentText();
if (curTrans.includes(cleanTrans)) return;
const m = /\\v (\d+)\s?/.exec(cleanTrans);
const index = m && curTrans.includes(m[0]) ? m[0].length : 0;
const space = /\s$/.test(curTrans) ? '' : ' ';
onTextAdd(space + cleanTrans.substring(index), true);
const added = appendAutoTranscription(trans, getCurrentText());
if (added !== undefined) onTextAdd(added, true);
},
[getCurrentText, onTextAdd]
);
Expand Down
Loading