4

I want to use iOS Speech API to recognize mathematical expressions. It works okay for things like two plus four times three - reads it as 2+4*3, but when I start expression with 1 it always reads it as "One". When "One" is in the middle of expression it works as expected.

I figured out that when I set SFSpeechAudioBufferRecognitionRequest property taskHint to .search when displaying live results it recognizes 1 as "1" properly at first but at the end changes it to "One"

Is there a way to configure it to recognize only numbers? Or just force to read "One" as "1" always? Or the only way to fix it is to format result string on my own?

szooky
  • 177
  • 1
  • 12
  • Also asked at https://stackoverflow.com/questions/41855848/does-the-ios-speech-api-support-grammar, you can use something like openears or tlsphinx – Nikolay Shmyrev Jun 18 '17 at 12:52

1 Answers1

3

I have the same problem, but looks like there is no way to configure it. I write this extension for my code, I'm checking every segment with this.

extension String {

    var numericValue: NSNumber? {

        //init number formater
        let numberFormater = NumberFormatter()

        //check if string is numeric
        numberFormater.numberStyle = .decimal

        guard let number = numberFormater.number(from: self.lowercased()) else {

            //check if string is spelled number
            numberFormater.numberStyle = .spellOut

            //change language to spanish
            //numberFormater.locale = Locale(identifier: "es")

            return numberFormater.number(from: self.lowercased())
        }

        // return converted numeric value
        return number
    }
}

For example

{

    let numString = "1.5"
    let number = numString.numericValue //1.5
    // or 
    let numString = "Seven"
    let number = numString.numericValue //7

}
Hakob
  • 71
  • 5