5

I want to create an app that receive voice input using iOS speech API. In google's API, there is an option for speechContext which I can provide hint or bias to some uncommon words.

Do iOS API provide this feature? I've been searching the site for a while but din't find any.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Thanon K
  • 177
  • 8

1 Answers1

0

there is no sample code about implementing hints for Google Speech Clouds for Swift online, so I made it up!

Open this class: SpeechRecognitionService.swift

You have to add your hint list array to the SpeechContext, add the SpeechContext to RecognitionConfig, and finally add RecognitionConfig to Streaming recognition config. Like this:

            let recognitionConfig = RecognitionConfig()
            recognitionConfig.encoding =  .linear16
            recognitionConfig.sampleRateHertz = Int32(sampleRate)
            recognitionConfig.languageCode = "en-US"
            recognitionConfig.maxAlternatives = 3
            recognitionConfig.enableWordTimeOffsets = true        
            let streamingRecognitionConfig = StreamingRecognitionConfig()
            streamingRecognitionConfig.singleUtterance = true
            streamingRecognitionConfig.interimResults = true


            //Custom vocabulary (Hints) code
            var phraseArray=NSMutableArray(array: ["my donkey is yayeerobee", "my horse is tekkadan", "bet four for kalamazoo"])
            var mySpeechContext = SpeechContext.init()
            mySpeechContext.phrasesArray=phraseArray
            recognitionConfig.speechContextsArray = NSMutableArray(array: [mySpeechContext])
            streamingRecognitionConfig.config = recognitionConfig
            //Custom vocabulary (Hints) code

            let streamingRecognizeRequest = StreamingRecognizeRequest()
            streamingRecognizeRequest.streamingConfig = streamingRecognitionConfig

Bonus: Adding your custom words mixed inside a simple phrase instead of adding the word alone gave me better results.

Josh
  • 6,251
  • 2
  • 46
  • 73