How to make iOS Speech API read only numbers and recognize "one" as "1"

I want to use the iOS Speech API to recognize math expressions. It works fine for things like two plus four times three

- reads it like 2+4*3

, but when I start an expression with 1, it always reads it as "One". When "One" is in the middle of the expression, it works as expected.

I realized that when I set the SFSpeechAudioBufferRecognitionRequest

property taskHint

to .search

when displaying live results, it first recognizes 1 as "1" and at the end changes it to "One"

Is there a way to configure it to recognize only numbers? Or just make "One" read "1" always? Or the only way to fix this is to format the result string yourself?

+3


source to share


1 answer


I have the same problem, but it looks like there is no way to configure it. I am writing this extension for my code, I am 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

}

      

0


source







All Articles