feat: improve dialogue speech recognition and AI intervention

This commit is contained in:
shen
2026-09-19 17:45:25 -07:00
parent 5b9cba793e
commit 335d0304b1
6 changed files with 704 additions and 12 deletions
+37 -1
View File
@@ -14,6 +14,42 @@ class VoiceService {
VoiceService._();
static final instance = VoiceService._();
/// Reverses the inverse text normalization (ITN) performed by system speech
/// recognisers, which convert spoken numbers like "one" into "1".
/// For an English-learning app the learner needs the spelled-out words.
static String reverseItn(String text) {
// Standalone digit → word. Uses word-boundary anchors so "12" or "100" are
// left alone (those are unlikely to be single-word utterances the learner
// intended as words).
const digitToWord = {
'0': 'zero',
'1': 'one',
'2': 'two',
'3': 'three',
'4': 'four',
'5': 'five',
'6': 'six',
'7': 'seven',
'8': 'eight',
'9': 'nine',
'10': 'ten',
'11': 'eleven',
'12': 'twelve',
'13': 'thirteen',
'14': 'fourteen',
'15': 'fifteen',
'16': 'sixteen',
'17': 'seventeen',
'18': 'eighteen',
'19': 'nineteen',
'20': 'twenty',
};
return text.replaceAllMapped(
RegExp(r'\b(\d{1,2})\b'),
(m) => digitToWord[m.group(1)!] ?? m.group(0)!,
);
}
final FlutterTts _tts = FlutterTts();
final SpeechToText _stt = SpeechToText();
final AudioRecorder _recorder = AudioRecorder();
@@ -228,7 +264,7 @@ class VoiceService {
await _stt.listen(
onResult: (result) =>
onResult(result.recognizedWords, result.finalResult),
onResult(reverseItn(result.recognizedWords), result.finalResult),
listenOptions: SpeechListenOptions(
localeId: targetLocaleId,
listenFor: const Duration(seconds: 30),