Skip to content

armgit / mimik

Public Imported from GitHub
0

🎬 A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots 📖

feat(voice): reject hallucinated transcript segments

Luis Ortizcaf6b7d779b4
diff --git a/src/core/capture/voice/__tests__/filter.test.ts b/src/core/capture/voice/__tests__/filter.test.ts
new file mode 100644
index 0000000..c90d734
--- /dev/null
+++ b/src/core/capture/voice/__tests__/filter.test.ts
@@ -0,0 +1,51 @@
+import { describe, expect, it } from 'vitest';
+import { rejectReason } from '../filter';
+import type { TranscriptSegment } from '../types';
+
+const scored = (text: string, over: Partial<TranscriptSegment> = {}): TranscriptSegment => ({
+ start: 0,
+ end: 1,
+ text,
+ no_speech_prob: 0.01,
+ avg_logprob: -0.2,
+ compression_ratio: 1.4,
+ ...over,
+});
+
+describe('rejectReason', () => {
+ it('accepts a normal segment', () => {
+ expect(rejectReason(scored('Click the save button.'))).toBeNull();
+ });
+
+ it('rejects empty text', () => {
+ expect(rejectReason(scored(' '))).toBe('empty');
+ });
+
+ it('rejects a segment missing its scores, failing closed', () => {
+ expect(rejectReason({ start: 0, end: 1, text: 'hello' })).toBe('unscored');
+ });
+
+ it('rejects on high no_speech_prob', () => {
+ expect(rejectReason(scored('hello', { no_speech_prob: 0.7 }))).toContain('no_speech_prob');
+ });
+
+ it('rejects on low avg_logprob', () => {
+ expect(rejectReason(scored('hello', { avg_logprob: -1.5 }))).toContain('avg_logprob');
+ });
+
+ it('rejects on high compression_ratio, which catches repeat loops', () => {
+ expect(rejectReason(scored('a a a a', { compression_ratio: 3 }))).toContain('compression_ratio');
+ });
+
+ it('rejects a blocklisted hallucination that has clean scores', () => {
+ expect(rejectReason(scored('Thanks for watching!'))).toContain('blocklist');
+ });
+
+ it('rejects a filler word when it is the whole segment', () => {
+ expect(rejectReason(scored('You'))).toContain('solo-filler');
+ });
+
+ it('keeps a filler word appearing inside a real sentence', () => {
+ expect(rejectReason(scored('You should click save.'))).toBeNull();
+ });
+});
diff --git a/src/core/capture/voice/filter.ts b/src/core/capture/voice/filter.ts
new file mode 100644
index 0000000..2296744
--- /dev/null
+++ b/src/core/capture/voice/filter.ts
@@ -0,0 +1,53 @@
+import type { ScoredTranscriptSegment, TranscriptSegment } from './types';
+
+export const BLOCKLIST = new Set([
+ 'thank you',
+ 'thanks for watching',
+ 'thank you for watching',
+ 'thanks for listening',
+ 'please subscribe',
+ "don't forget to subscribe",
+ 'like and subscribe',
+ 'subtitles by the amara.org community',
+ 'transcription by castingwords',
+ 'amara.org',
+ '[music]',
+ '[applause]',
+ 'music',
+ 'applause',
+ 'продолжение следует',
+ 'субтитры сделал dimatorzok',
+ "sous-titres réalisés par la communauté d'amara.org",
+ '由 amara.org 社群提供的字幕',
+]);
+
+export const SOLO_ONLY = new Set(['you', 'so', 'the', 'oh', 'okay', 'ok', 'yeah', 'hmm']);
+
+export function normalise(text: string): string {
+ return text
+ .toLowerCase()
+ .replace(/[^\p{L}\p{N}\s.]/gu, '')
+ .replace(/\s+/g, ' ')
+ .trim()
+ .replace(/\.$/, '');
+}
+
+export function isScored(segment: TranscriptSegment): segment is ScoredTranscriptSegment {
+ return (
+ typeof segment.no_speech_prob === 'number' &&
+ typeof segment.avg_logprob === 'number' &&
+ typeof segment.compression_ratio === 'number'
+ );
+}
+
+export function rejectReason(segment: TranscriptSegment): string | null {
+ const text = normalise(segment.text ?? '');
+ if (!text) return 'empty';
+ if (!isScored(segment)) return 'unscored';
+ if (segment.no_speech_prob > 0.6) return `no_speech_prob=${segment.no_speech_prob}`;
+ if (segment.avg_logprob < -1) return `avg_logprob=${segment.avg_logprob}`;
+ if (segment.compression_ratio > 2.4) return `compression_ratio=${segment.compression_ratio}`;
+ if (BLOCKLIST.has(text)) return `blocklist:${text}`;
+ if (SOLO_ONLY.has(text)) return `solo-filler:${text}`;
+ return null;
+}