How to provide hint to iOS speech recognition API?
Asked Answered
N

1

6

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.

Noachian answered 19/3, 2017 at 4:37 Comment(2)
I am interested in this as well. I'll share anything I find.Connolly
Same here, I adde the word list in a String array to speechContext, but the app crashes.Kilar
K
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.

Kilar answered 8/2, 2018 at 14:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.