I'm using iOS Speech API.
I'm trying to do some math on output of speech-to-text framework.
There are several problems.. First of all, user can say something stupid, so we have to check if it is word, or math expression. So i thought that we could use something like :
if string.contains("×") || string.contains("+") || string.contains("-")
But it looks awful and what if user says for example "+2"
..
Then I thought that, maybe we could check if input is Int, so I thought about something like:
guard let stringResult = result?.bestTranscription.formattedString else {
return
}
let convertedNumber = Int(stringResult)
if let stringResult = convertedNumber {
print(stringResult)
print("Everything works Fine")
} else {
print("nope..")
}
and it keeps failing..
I tryed several more strange methods to solve this error handling, but I don't have any other ideas.
Input, as piece of startRecording Function:
if result != nil {
guard let stringResult = result?.bestTranscription.formattedString else {
return
}
self.inputLabel.text = stringResult
And calculate function that just looks totally wrong:
private func calculate(string: String) {
if string.contains("×") || string.contains("+") || string.contains("-") {
let stringToCalculate = string.replacingOccurrences(of: "×", with: "*")
guard let finalScore = NSExpression(format: stringToCalculate).expressionValue(with: nil, context: nil) else { return }
outputLabel.text = String(describing: finalScore)
} else {
outputLabel.text = "Are you sure it's mathematical evaluation ?"
}
}
Good input Example : 7 + 7 x 2 -> 21
Bad: "Pirates are drinking rum!" or "Pirates"-> outputLabel.text = "Are you sure that's mathematical evaluation?"
So the question is how should I handle errors to get know that input is mathematical evaluation and do math on it, and not the word/words?