Skip to content
Merged
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
Expand Up @@ -22,6 +22,10 @@ internal class VoiceTranscriptionException(
cause: Throwable? = null
) : Exception(message, cause)

// ML Kit reports every mode UNAVAILABLE for tags with Unicode extensions, which Android adds
// for regional preferences such as the temperature unit (en-US-u-mu-celsius).
internal fun speechLocale(systemLocale: Locale): Locale = systemLocale.stripExtensions()

private val recognitionModes = listOf(
SpeechRecognizerOptions.Mode.MODE_ADVANCED,
SpeechRecognizerOptions.Mode.MODE_BASIC
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class T3VoiceTranscriptionModule : Module() {
} else {
withContext(Dispatchers.IO) {
val context = requireNotNull(appContext.reactContext)
val locale = Resources.getSystem().configuration.locales[0]
val locale = speechLocale(Resources.getSystem().configuration.locales[0])
isGoogleVoiceTranscriptionSupported { MlKitSpeechClient(context, locale, it) }
}
}
Expand All @@ -47,7 +47,7 @@ class T3VoiceTranscriptionModule : Module() {
)
}
val context = requireNotNull(appContext.reactContext)
val locale = Resources.getSystem().configuration.locales[0]
val locale = speechLocale(Resources.getSystem().configuration.locales[0])
val mode = prepareGoogleVoiceTranscription { MlKitSpeechClient(context, locale, it) }
mapOf("locale" to locale.toLanguageTag(), "mode" to mode)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.google.mlkit.genai.common.FeatureStatus
import com.google.mlkit.genai.common.GenAiException
import com.google.mlkit.genai.speechrecognition.SpeechRecognizerOptions
import com.google.mlkit.genai.speechrecognition.SpeechRecognizerResponse
import java.util.Locale
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.cancelAndJoin
import kotlinx.coroutines.flow.flow
Expand All @@ -14,6 +15,18 @@ import org.junit.Assert.assertTrue
import org.junit.Test

class GoogleVoiceTranscriberTest {
@Test
fun dropsRegionalPreferenceExtensionsFromTheSpeechLocale() {
for ((system, expected) in listOf(
"en-US-u-mu-celsius" to "en-US",
"en-GB-u-fw-mon-mu-celsius" to "en-GB",
"ar-EG-u-nu-latn" to "ar-EG",
"zh-Hant-TW" to "zh-Hant-TW"
)) {
assertEquals(expected, speechLocale(Locale.forLanguageTag(system)).toLanguageTag())
}
}

@Test
fun preservesUtterancesAndTheirSpacingWithoutDuplicatingHypotheses() = runBlocking {
for ((finals, trailing, expected) in listOf(
Expand Down
Loading