首页 > 解决方案 > 听写(语音识别)文本与 Swift 中的字符串不匹配

问题描述

我开发了一个语音识别功能,可以检测阿拉伯语音频并返回一个字符串,我将它分配给一个名为speechRecogText的变量。

在代码中,我还有一个文本字段,我将输入存储在另一个名为textFieldText的字符串 var 中。

目标我希望能够检查speechRecogText是否在textFieldText中包含想要的内容,这通常适用于英语,但对于阿拉伯语则不起作用。

但是,当我尝试相反的方式speechRecogText.contains(textFieldText)时,它会起作用。下面的代码

    // the two variables
    var speechRecogText: String = ""
    var textFieldText: String = ""

语音识别函数和文本字段...输入输入后,我调用函数compareTexts()

func compareTexts() {
    
    // Checking the textField text
    if speechRecogText.contains(textFieldText) {
        print("matching texts")
    } else {
        print("not matching")
    }
    
    // Checking the speech recog text
    if textFieldText.contains(speechRecogText) {
        print("matching texts")
    } else {
        print("not matching")
    }
}

例如,两者都会用阿拉伯语打印单词“قل”。但是当我调用该函数时,控制台显示(答案):

Console 
//matching texts
//not matching

我希望第二个也可以工作/匹配并打印匹配的文本。

尽管两个变量都包含相似的文本,但结果有点令人困惑。它们是否存储相同的值,或者它们是否显示相同的字符串但在代码深处它们以某种方式变化?

我真的很感谢你的支持:)

这是整个代码

这是整个代码。

import SwiftUI
import SwiftSpeech

struct ContentView: View {
    
    @State var speechRecogText = "قل"
    @State var textFieldText = "قل"
    
    var body: some View {
        
        VStack {
            
            //MARK: - Text Field
                        
            TextField("textfield text", text: $textFieldText)
                .padding()
            
            Button(action: {
                compareTexts()
            } ) {
                ZStack {
                    Color.blue.clipShape(Circle()).frame(width: 70, height: 70)
                    Text("Check").foregroundColor(.white)
                }
            }
            
            
            //MARK: - Speech Recognition
            
            
            Text("speechRecogText: " + speechRecogText)
            
            SwiftSpeech.RecordButton()
                .swiftSpeechRecordOnHold(locale: Locale(identifier: "ar"),
                                         animation: .spring(),
                                         distanceToCancel: 50)

                .onRecognizeLatest { result in
                    self.speechRecogText = result.bestTranscription.formattedString
                    if result.isFinal {
                        print("last transcript")
                    }
                } handleError: { error in
                    print("Failed recognizing the audio. Retry: \(error)")
                }
        }
        
    }
    

    func compareTexts() {
        // Checking the textField text
        if speechRecogText.contains(textFieldText) {
            print("---> speechRecogText contains textFieldText")
        } else {
            print("---> speechRecogText DOES NOT contains textFieldText")
        }

        // Checking the speech recog text
        if textFieldText.contains(speechRecogText) {
            print("---> textFieldText contains speechRecogText")
        } else {
            print("---> textFieldText DOES NOT contains speechRecogText")
        }
    }
}

标签: swiftxcodespeech-recognitionspeech-to-text

解决方案


所以在测试了代码之后,我发现语音识别文本的字符比硬编码的字符串多。当我将 .count() 用于 textField 文本“قل”时,它打印了 2,但对于语音识别,它打印了 3。

我使用映射的方法来查看多余的字符。

speechRecogText.map({ print($0) })

看起来第一个字符是一个空白区域。所以我用

speechRecogText.removeFirst()

然后比较。结果如愿。


推荐阅读