从语音到文本的数学评估

Math evaluation from speech to text

我正在使用 iOS 语音 API。

我正在尝试对语音到文本框架的输出做一些数学运算。

有几个问题.. 首先,用户可以说一些愚蠢的东西,所以我们必须检查它是单词还是数学表达式。所以我认为我们可以使用类似的东西:

if string.contains("×") || string.contains("+") || string.contains("-")

但它看起来很糟糕,如果用户说例如 "+2"..

然后我想,也许我们可以检查输入是否为 Int,所以我想到了类似的东西:

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..")                    
        }

而且一直失败..

我尝试了几种比较奇怪的方法来解决这个错误处理,但我没有任何其他想法。

输入,作为 startRecording 函数的一部分:

if result != nil {
            guard let stringResult = result?.bestTranscription.formattedString else {
                return
            }
            self.inputLabel.text = stringResult

并计算看起来完全错误的函数:

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 ?"
    }
}

良好的输入示例:7 + 7 x 2 -> 21

差:"Pirates are drinking rum!" 或 "Pirates"-> outputLabel.text = "Are you sure that's mathematical evaluation?"

所以问题是我应该如何处理错误以了解输入是数学评估并对其进行数学运算,而不是 word/words?

自然语言解析是一项复杂的任务,当然它可以通过简单的子字符串匹配甚至正则表达式来完成,但现在有更先进的算法使用机器学习来分类更复杂的情况。这样的系统是基于示例的,这意味着您可以向它们提交示例,它们将从中正确地识别意图。这种基于范例的系统可以解析 "multiply the result by three" 之类的东西,并理解 "result" 是之前的结果,你需要将它相乘。他们还为您提供解析信心

例如,您可以查看 RASA NLU based on SPACY and MITIE 此类工具的示例,还有 Microsoft 的 LIUS 等服务。

在 iOS 上 运行 这样的工具并不容易,您可能希望 运行 通过 REST API 在服务器上使用它们。但是编译MITIE理论上是可以的

另见

How to proceed with NLP task for recognizing intent and slots

Converting natural language to a math equation