don't rely on token offsets from speech_to_text endpoint for creating text spans
This commit is contained in:
parent
6299fa38bf
commit
544a2e61c4
1 changed files with 18 additions and 21 deletions
|
|
@ -72,12 +72,11 @@ class MessageSpeechToTextCardState extends State<MessageSpeechToTextCard> {
|
|||
TextSpan _buildTranscriptText(BuildContext context) {
|
||||
final Transcript transcript = speechToTextResponse!.transcript;
|
||||
final List<InlineSpan> spans = [];
|
||||
final String fullText = transcript.text;
|
||||
int lastEnd = 0;
|
||||
String remainingFullText = transcript.text;
|
||||
|
||||
if (transcript.sttTokens.isEmpty) {
|
||||
return TextSpan(
|
||||
text: fullText,
|
||||
text: remainingFullText,
|
||||
style: BotStyle.text(
|
||||
context,
|
||||
existingStyle: TextStyle(
|
||||
|
|
@ -89,19 +88,26 @@ class MessageSpeechToTextCardState extends State<MessageSpeechToTextCard> {
|
|||
}
|
||||
|
||||
for (final token in transcript.sttTokens) {
|
||||
if (token.offset > lastEnd) {
|
||||
final offset = remainingFullText.indexOf(token.token.text.content);
|
||||
final length = token.length;
|
||||
|
||||
if (remainingFullText.substring(0, offset).trim().isNotEmpty) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (offset > 0) {
|
||||
// Add any plain text before the token
|
||||
spans.add(
|
||||
TextSpan(
|
||||
text: fullText.substring(lastEnd, token.offset),
|
||||
),
|
||||
TextSpan(text: remainingFullText.substring(0, offset)),
|
||||
);
|
||||
// debugPrint('Pre: ${fullText.substring(lastEnd, token.offset)}');
|
||||
}
|
||||
|
||||
spans.add(
|
||||
TextSpan(
|
||||
text: fullText.substring(token.offset, token.offset + token.length),
|
||||
text: remainingFullText.substring(
|
||||
offset,
|
||||
offset + token.length,
|
||||
),
|
||||
style: BotStyle.text(
|
||||
context,
|
||||
existingStyle: TextStyle(color: token.color(context)),
|
||||
|
|
@ -125,21 +131,12 @@ class MessageSpeechToTextCardState extends State<MessageSpeechToTextCard> {
|
|||
),
|
||||
);
|
||||
|
||||
// debugPrint(
|
||||
// 'Main: ${fullText.substring(token.offset, token.offset + token.length)}',
|
||||
// );
|
||||
|
||||
lastEnd = token.offset + token.length;
|
||||
remainingFullText = remainingFullText.substring(offset + length);
|
||||
}
|
||||
|
||||
if (lastEnd < fullText.length) {
|
||||
if (remainingFullText.isNotEmpty) {
|
||||
// Add any remaining text after the last token
|
||||
spans.add(
|
||||
TextSpan(
|
||||
text: fullText.substring(lastEnd),
|
||||
),
|
||||
);
|
||||
// debugPrint('Post: ${fullText.substring(lastEnd)}');
|
||||
spans.add(TextSpan(text: remainingFullText));
|
||||
}
|
||||
|
||||
return TextSpan(children: spans);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue