fix(voice_worker): a hinted wake word may replace a near miss the plain decode wrote

Live 2026-11-19 "비서야" was decoded as "기사야" (two of six letters off, more than call
detection allows) while the hinted decode of the same audio gave "비서야"; the hint was
dropped because it changed the wake word count. The hint may now add one wake word where
the plain text is within a third of that word's letters (NFKD letters, Hangul by jamo).
On the user's 100 recordings: 5 fixed, 0 wrong; 0 of 216 noise decodes gained a wake word.

The live round trip test gets a going-on case: the person's own recordings of one thing
said in pieces, played into Discord with their real pauses.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-19 21:55:10 +09:00
parent 41f539dd48
commit e716065538
5 changed files with 197 additions and 33 deletions

View File

@@ -6,6 +6,7 @@ import glob
import os
import re
import sys
import unicodedata
import zlib
import numpy as np
@@ -61,20 +62,50 @@ def _mentions(text: str, word: str) -> int:
return _letters(text).count(w) if w else 0
def _calls(text: str, hotwords: tuple[str, ...]) -> list[str]:
"""The wake words in ``text``, longest first at each place, without overlaps ("김비서야" is one)."""
s = _letters(text)
words = sorted({_letters(w) for w in hotwords if _letters(w)}, key=len, reverse=True)
found, i = [], 0
while i < len(s):
w = next((w for w in words if s.startswith(w, i)), None)
if w:
found.append(w)
i += len(w)
else:
i += 1
return found
def keeps_wake_words(text: str, base: str, hotwords: tuple[str, ...]) -> bool:
"""Whether ``text`` (a hinted or prompted decode) holds the wake words of ``base`` (the decode it is
checked against): every hotword exactly as often, or one wake word more where ``base`` has a near
miss of it. Near: the whole text is at most a third of that wake word's letters from ``base``,
compared as Unicode NFKD letters (Hangul by its letters: "기사야" is two of six off "비서야")."""
if all(_mentions(text, w) == _mentions(base, w) for w in hotwords):
return True
have, had = _calls(text, hotwords), _calls(base, hotwords)
if len(have) != len(had) + 1 or any(_mentions(text, w) < _mentions(base, w) for w in hotwords):
return False
nfkd = lambda t: unicodedata.normalize("NFKD", _letters(t))
return _edit_distance(nfkd(text), nfkd(base)) <= min(len(nfkd(w)) for w in have) // 3
def choose_with_hint(hint_text: str, hint_lp: float, plain_text: str, plain_lp: float,
hotwords: tuple[str, ...]) -> str:
"""Pick between the decode that had the hotwords as a hint and the plain decode of the same audio.
The hint can make Whisper write a wake word that nobody said (on laughter, tacked onto a real
sentence, or in place of a similar word), so the plain decode decides: no plain text means nothing
was said, and the hint's text is used only when it stays close to the plain one and contains every
hotword exactly as often. It may change other words, never how many wake words were heard.
sentence, or in place of another word), so the plain decode decides: no plain text means nothing
was said, and the hint's text is used only when it stays close to the plain one and keeps its wake
words (``keeps_wake_words``): it may change other words, and turn a near miss of a wake word into it.
"""
if not keep_transcript(plain_text, plain_lp):
return ""
close = _edit_distance(_letters(hint_text), _letters(plain_text)) <= len(_letters(plain_text)) // 2
if close and keep_transcript(hint_text, hint_lp) and all(
_mentions(hint_text, w) == _mentions(plain_text, w) for w in hotwords):
if close and keep_transcript(hint_text, hint_lp) and keeps_wake_words(hint_text, plain_text, hotwords):
if len(_calls(hint_text, hotwords)) != len(_calls(plain_text, hotwords)):
debug_log("hint turned a near miss into a wake word", "stt")
return hint_text
return plain_text

View File

@@ -145,11 +145,22 @@ text is empty:
- Its compression ratio (UTF-8 bytes over zlib bytes) is at most 2.4, which catches repetition loops.
- With hotwords: the clip is decoded once with them and once without, from the same encoder pass.
The plain decode must pass the checks above, otherwise nothing was said. The hinted text is used
only when it stays within half the plain text's letters of it and contains every hotword exactly
as often as the plain one (ignoring case, spacing and punctuation, no fuzzy matching), so the hint
can never add, drop or create a wake word: not from laughter, not after a real sentence, not from
a one-letter wake word, and not by turning a similar word ("서비스", or the near miss "자비수")
into it. Otherwise the plain text is used; matching near misses is left to call detection.
only when it stays within half the plain text's letters of it and keeps the plain one's wake
words (ignoring case, spacing and punctuation): every hotword exactly as often, or exactly one
wake word more where the plain text has a near miss of it, that is when the whole hinted text is
at most a third of that wake word's letters from the plain one, compared as Unicode NFKD letters
(Hangul by its letters). So the hint never drops a wake word, and never adds one from laughter,
after a real sentence, from a one-letter wake word or in place of a different word ("기구" heard
for "디코" became "김비서" with the hint: five letters off). It does turn a near miss the plain
decode wrote into the wake word the hinted decode heard: live 2026-11-19 "비서야" was decoded as
"기사야" (two of six letters off, more than call detection allows), and the hinted decode of the
same audio gave "비서야". Otherwise the plain text is used; other near misses are left to call
detection. Measured on the user's 100 recordings and 108 noise clips (CPU int8, 2026-11-19): the
hint added a wake word to 8 recordings and 15 noise clips; this rule takes 5 (all said: 자비스 or
비서야 heard as 라비스, 아비스, 기사야, 기서야) and rejects the 3 others (two said, one "디코") and all
15 noise clips. Of 15 synthetic sentences with words near the wake words, the hint added none
where the word was heard as said (서비스, 기사, 이사야, 비상약, 미소야, 시사); two where the voice
itself said "비사(야)" for "기사(야)" became "비서(야)".
`no_speech_prob` is not used: large-v3-turbo reports 0.0 for silence and noise alike.