Wake phrases the model can hear: greeting x name, generated pieces, sensitivity
CI / check (push) Canceled after 0s

"Hey Lembas" could not work well: Lembas is not a word the model knows.

- The phrase is a greeting (Hey, OK, Hello) and a name (Assistant, Computer,
  Google, and ten ordinary names); the default is Hey Assistant and an old
  saved id reads as it.
- Token pieces are generated, not hand-written: fetch-wakeword.py also takes
  the model's bpe.model from the same pinned archive, gen-wake-phrases.py
  encodes every word with it into wake/WakeWords.kt, CI checks the committed
  table against the tokenizer, and WakeWordsTest checks every piece against
  tokens.txt. "OK" is spelled as the model's speech wrote it, OKAY.
  Elizabeth and Benjamin are dropped (four or more single-letter pieces).
- Each keyword line carries a boosting score and a trigger threshold
  (sherpa-onnx's ":boost #threshold"), chosen by a Sensitivity setting:
  Low 1.0/0.35, Normal 1.5/0.25, High 2.0/0.15; short phrases 0.05 stricter.
  numTrailingBlanks is the engine's own 1.
- Settings says the phrase is English whatever the app's language is, and
  that a Google device nearby answers to Google too.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
HomerandClaude Fable 5.2 committed 2026-11-12 09:49:47 +00:00
1 parent 374574b9ce
commit 3046ad154a
26 files changed
+691 -41

No files matched your search

@@ -206,7 +206,7 @@ class WakeListenerTest {
)
val plans = MutableStateFlow(plan)
val job = launch(Dispatchers.Default) { listener.follow(plans) }
val other = WakePhrases.all.first { it.id != assistant.id }
val other = WakePhrases.byId("ok_computer")
repeat(5) { round ->
val phrase = if (round % 2 == 0) other else assistant
said(now, WakeState.Listening(if (round % 2 == 0) assistant.words else other.words))
@@ -1,9 +1,13 @@
package eu.llembas.android
import eu.llembas.android.api.LembasJson
import eu.llembas.android.auth.Prefs
import eu.llembas.android.wake.WakeError
import eu.llembas.android.wake.WakeFailure
import eu.llembas.android.wake.WakePause
import eu.llembas.android.wake.WakePhrases
import eu.llembas.android.wake.WakeSensitivity
import eu.llembas.android.wake.WakeWords
import eu.llembas.android.wake.checkKeywords
import eu.llembas.android.wake.unpackWakeModel
import eu.llembas.android.wake.wakeBackoff
@@ -19,6 +23,7 @@ import org.junit.rules.TemporaryFolder
import java.io.File
import java.io.FileNotFoundException
import java.io.InputStream
import java.util.Locale
class WakeTest {
@get:Rule
@@ -79,10 +84,47 @@ class WakeTest {
}
@Test
fun theKeywordsFileNamesThePhraseForTheEngine() {
val phrase = WakePhrases.byId("hey_assistant")
assertTrue(wakeKeywords(phrase).startsWith("▁HE Y ▁AS S IST ANT "))
assertTrue(wakeKeywords(phrase).endsWith("@hey_assistant\n"))
fun theKeywordsFileCarriesBoostAndThresholdInTheEnginesSyntax() {
val assistant = WakePhrases.byId("hey_assistant")
assertEquals("▁HE Y ▁AS S IST ANT :1.5 #0.25 @hey_assistant\n", wakeKeywords(assistant))
assertEquals("▁HE Y ▁AS S IST ANT :1.0 #0.35 @hey_assistant\n", wakeKeywords(assistant, WakeSensitivity.Low))
assertEquals("▁HE Y ▁AS S IST ANT :2.0 #0.15 @hey_assistant\n", wakeKeywords(assistant, WakeSensitivity.High))
// Four pieces: short enough to turn up inside other speech, so it must be a little surer.
assertEquals("▁OKAY ▁COMP U TER :1.5 #0.30 @ok_computer\n", wakeKeywords(WakePhrases.byId("ok_computer")))
// A phone set to a language with a decimal comma still writes numbers the engine reads.
val phone = Locale.getDefault()
try {
Locale.setDefault(Locale.forLanguageTag("sk-SK"))
assertEquals("▁HE Y ▁AS S IST ANT :1.5 #0.25 @hey_assistant\n", wakeKeywords(assistant))
} finally {
Locale.setDefault(phone)
}
assertEquals(WakeSensitivity.Normal, WakeSensitivity.byId("nonsense"))
}
@Test
fun aPhraseIsAGreetingAndAName() {
assertEquals("Hey Assistant", WakePhrases.default.words)
assertEquals(WakeWords.greetings.size * WakeWords.names.size, WakePhrases.all.size)
assertEquals(WakePhrases.all.size, WakePhrases.all.map { it.id }.toSet().size)
assertEquals("OK Google", WakePhrases.byId("ok_google").words)
assertEquals("Hello Samantha", WakePhrases.byId("hello_samantha").words)
// Nothing the model cannot hear is offered.
assertFalse(WakePhrases.all.any { "lembas" in it.id })
}
@Test
fun aSavedPhraseThatIsGoneBecomesHeyAssistant() {
for (old in listOf("hey_lembas", "ok_lembas", "hello_lembas", "nonsense", "")) assertEquals("hey_assistant", WakePhrases.byId(old).id)
assertEquals("hey_assistant", WakePhrases.byId("hey_assistant").id)
// Settings as beta.7 saved them: the old phrase id, no sensitivity yet.
val saved = LembasJson.decodeFromString(Prefs.serializer(), """{"wakeWord":true,"wakePhrase":"hey_lembas","wakeScreenOnly":false}""")
assertTrue(saved.wakeWord)
assertFalse(saved.wakeScreenOnly)
assertEquals("Hey Assistant", WakePhrases.byId(saved.wakePhrase).words)
assertEquals(WakeSensitivity.Normal, WakeSensitivity.byId(saved.wakeSensitivity))
// And a phone that never had the wake word starts on the same phrase.
assertEquals("hey_assistant", Prefs().wakePhrase)
}
@Test
@@ -104,7 +146,6 @@ class WakeTest {
@Test
fun phrasesAreSpelledInTheModelsPieces() {
assertEquals("hey_lembas", WakePhrases.byId("nonsense").id)
// Every piece of the spelling starts a word with ▁ or continues one; no spaces inside a word.
for (phrase in WakePhrases.all) {
assertTrue(phrase.tokens.startsWith("▁"))
@@ -0,0 +1,53 @@
package eu.llembas.android
import eu.llembas.android.wake.WakePhrases
import eu.llembas.android.wake.WakeSensitivity
import eu.llembas.android.wake.WakeWords
import eu.llembas.android.wake.checkKeywords
import eu.llembas.android.wake.wakeKeywords
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
import java.io.File
/**
* The generated table (`wake/WakeWords.kt`, by `scripts/gen-wake-phrases.py`) against the model
* that will hear it: every piece of every phrase must be in the model's `tokens.txt` -- the
* engine ends the app on one that is not. The table is the model's, so the test reads the very
* file the build puts into the APK (`scripts/fetch-wakeword.py` fetches it before Gradle).
*/
class WakeWordsTest {
private val tokens: String by lazy {
val file = File("src/full/assets/wakeword/tokens.txt")
check(file.isFile) { "no ${file.path}: run scripts/fetch-wakeword.py before the tests" }
file.readText()
}
@Test
fun everyPieceOfEveryPhraseIsInTheModel() {
val known = tokens.lineSequence().map { it.substringBeforeLast(' ') }.toSet()
assertTrue(known.size > 400)
for (word in WakeWords.greetings + WakeWords.names) {
for (piece in word.pieces.split(' ')) assertTrue("${word.words}: the model has no piece \"$piece\"", piece in known)
}
// And as the engine gets them: the whole keywords line of every phrase, at every sensitivity.
for (phrase in WakePhrases.all) {
for (sensitivity in WakeSensitivity.entries) checkKeywords(wakeKeywords(phrase, sensitivity), tokens)
}
}
@Test
fun theTableIsWhatTheSettingsOffer() {
assertEquals(listOf("Hey", "OK", "Hello"), WakeWords.greetings.map { it.words })
assertEquals(listOf("Assistant", "Computer", "Google"), WakeWords.names.take(3).map { it.words })
// Ids are what Settings saves: lower case, one word, unique.
val ids = (WakeWords.greetings + WakeWords.names).map { it.id }
assertEquals(ids.size, ids.toSet().size)
assertTrue(ids.all { id -> id.isNotEmpty() && id.all { it in 'a'..'z' } })
// Written as the model's speech was: capitals only, each word starting with the word mark.
for (word in WakeWords.greetings + WakeWords.names) {
assertTrue(word.pieces.startsWith("▁"))
assertTrue(word.pieces.all { it == '▁' || it == ' ' || it in 'A'..'Z' })
}
}
}