expo-speech-recognition 0.2.19 → 0.2.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -36,6 +36,11 @@ public class ExpoSpeechRecognitionModule: Module {
36
36
 
37
37
  var speechRecognizer: ExpoSpeechRecognizer?
38
38
 
39
+ // Hack for iOS 18 to detect final results
40
+ // See: https://forums.developer.apple.com/forums/thread/762952 for more info
41
+ // This is a temporary workaround until the issue is fixed in a future iOS release
42
+ var hasSeenFinalResult: Bool = false
43
+
39
44
  public func definition() -> ModuleDefinition {
40
45
  // Sets the name of the module that JavaScript code will use to refer to the module. Takes a string as an argument.
41
46
  // Can be inferred from module's class name, but it's recommended to set it explicitly for clarity.
@@ -348,11 +353,13 @@ public class ExpoSpeechRecognitionModule: Module {
348
353
  }
349
354
 
350
355
  func sendErrorAndStop(error: String, message: String) {
356
+ hasSeenFinalResult = false
351
357
  sendEvent("error", ["error": error, "message": message])
352
358
  sendEvent("end")
353
359
  }
354
360
 
355
361
  func handleEnd() {
362
+ hasSeenFinalResult = false
356
363
  sendEvent("end")
357
364
  }
358
365
 
@@ -362,7 +369,25 @@ public class ExpoSpeechRecognitionModule: Module {
362
369
  // Limit the number of transcriptions to the maxAlternatives
363
370
  let transcriptionSubsequence = result.transcriptions.prefix(maxAlternatives)
364
371
 
372
+ var isFinal = result.isFinal
373
+
374
+ // Hack for iOS 18 to detect final results
375
+ // See: https://forums.developer.apple.com/forums/thread/762952 for more info
376
+ // This is a temporary workaround until the issue is fixed in a future iOS release
377
+ if #available(iOS 18.0, *), !isFinal {
378
+ isFinal = result.speechRecognitionMetadata?.speechDuration ?? 0 > 0
379
+ }
380
+
365
381
  for transcription in transcriptionSubsequence {
382
+ var transcript = transcription.formattedString
383
+
384
+ // Prepend an empty space if the hacky workaround is applied
385
+ // So that the user can append the transcript to the previous result,
386
+ // matching the behavior of Android & Web Speech API
387
+ if hasSeenFinalResult {
388
+ transcript = " " + transcription.formattedString
389
+ }
390
+
366
391
  let segments = transcription.segments.map { segment in
367
392
  return Segment(
368
393
  startTimeMillis: segment.timestamp * 1000,
@@ -377,7 +402,7 @@ public class ExpoSpeechRecognitionModule: Module {
377
402
  / Float(transcription.segments.count)
378
403
 
379
404
  let item = TranscriptionResult(
380
- transcript: transcription.formattedString,
405
+ transcript: transcript,
381
406
  confidence: confidence,
382
407
  segments: segments
383
408
  )
@@ -386,9 +411,15 @@ public class ExpoSpeechRecognitionModule: Module {
386
411
  results.append(item)
387
412
  }
388
413
  }
389
- if result.isFinal && results.count == 0 {
414
+
415
+ // Apply the "workaround"
416
+ if #available(iOS 18.0, *), !result.isFinal, isFinal {
417
+ hasSeenFinalResult = true
418
+ }
419
+
420
+ if isFinal && results.isEmpty {
390
421
  // https://developer.mozilla.org/en-US/docs/Web/API/SpeechRecognition/nomatch_event
391
- // The nomatch event of the Web Speech API is fired
422
+ // The nomatch event of the Web Speech API is fired22A3351
392
423
  // when the speech recognition service returns a final result with no significant recognition.
393
424
  sendEvent("nomatch")
394
425
  return
@@ -397,7 +428,7 @@ public class ExpoSpeechRecognitionModule: Module {
397
428
  sendEvent(
398
429
  "result",
399
430
  [
400
- "isFinal": result.isFinal,
431
+ "isFinal": isFinal,
401
432
  "results": results.map { $0.toDictionary() },
402
433
  ]
403
434
  )
@@ -640,8 +640,6 @@ actor ExpoSpeechRecognizer: ObservableObject {
640
640
  }
641
641
 
642
642
  if receivedFinalResult || receivedError {
643
- // audioEngine?.stop()
644
- // audioEngine?.inputNode.removeTap(onBus: 0)
645
643
  Task { @MainActor in
646
644
  await reset()
647
645
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "expo-speech-recognition",
3
- "version": "0.2.19",
3
+ "version": "0.2.20",
4
4
  "description": "Speech Recognition for React Native Expo projects",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",