Skip to content

Commit

Permalink
fix: web events (#58)
Browse files Browse the repository at this point in the history
* fix(web): interim and final results weren't properly handled

* add changeset

* remove console logs
  • Loading branch information
jamsch authored Nov 19, 2024
1 parent d3d3d63 commit e4c54f1
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/odd-eels-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"expo-speech-recognition": patch
---

Fixed handling of interim and final results on web
68 changes: 55 additions & 13 deletions src/ExpoSpeechRecognitionModule.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
ExpoSpeechRecognitionNativeEventMap,
ExpoSpeechRecognitionNativeEvents,
ExpoSpeechRecognitionOptions,
ExpoSpeechRecognitionResultSegment,
} from "./ExpoSpeechRecognitionModule.types";

let _speechRecognitionRef: SpeechRecognition | null = null;
Expand Down Expand Up @@ -312,22 +313,63 @@ const webToNativeEventMap: {
end: (ev) => null,
error: (ev) => ({ error: ev.error, message: ev.message }),
nomatch: (ev) => null,
result: (ev) => {
const nativeResults: ExpoSpeechRecognitionNativeEventMap["result"]["results"] =
[];

for (let i = 0; i < ev.results[ev.resultIndex].length; i++) {
const result = ev.results[ev.resultIndex][i];
nativeResults.push({
transcript: result.transcript,
confidence: result.confidence,
segments: [],
});
result: (ev): ExpoSpeechRecognitionNativeEventMap["result"] => {
const isFinal = Boolean(ev.results[ev.resultIndex]?.isFinal);

if (isFinal) {
const results: ExpoSpeechRecognitionNativeEventMap["result"]["results"] =
[];

for (let i = 0; i < ev.results[ev.resultIndex].length; i++) {
const result = ev.results[ev.resultIndex][i];
results.push({
transcript: result.transcript,
confidence: result.confidence,
segments: [],
});
}
return {
isFinal: true,
results,
};
}

// Interim results: Append to the transcript
let transcript = "";
const segments: ExpoSpeechRecognitionResultSegment[] = [];

for (let i = ev.resultIndex; i < ev.results.length; i++) {
const resultList = ev.results[i];

for (let j = 0; j < resultList.length; j++) {
const result = resultList[j];
if (!result) {
continue;
}
segments.push({
confidence: result.confidence,
segment: result.transcript,
startTimeMillis: 0,
endTimeMillis: 0,
});

if (!isFinal) {
transcript += result.transcript;
}
}
}

return {
isFinal: Boolean(ev.results[ev.resultIndex]?.isFinal),
results: nativeResults,
isFinal: false,
results: [
{
transcript,
confidence:
segments.reduce((acc, curr) => acc + curr.confidence, 0) /
segments.length,
segments,
},
],
};
},
soundstart: (ev) => null,
Expand Down

0 comments on commit e4c54f1

Please sign in to comment.