听写(语音识别)文本与 Swift 中的字符串不匹配 [英] Dictation (Speech Recognition) text does not match a string in Swift

查看:37
本文介绍了听写(语音识别)文本与 Swift 中的字符串不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have developed a speech recognition function that can detect Arabic audio and return a string, I assign it to a var called speechRecogText.

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

Within the code I also have a text field which I store the input in another string var called textFieldText.

目标我希望能够检查 speechRecogText 是否包含 textFieldText 中的需求,这通常适用于英语,但适用于阿拉伯语不起作用.

Goal I want to be able to check if the speechRecogText contains wants in the textFieldText, this works normally with English but for Arabic it does not work.

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

However when I try the opposite way speechRecogText.contains(textFieldText) it works. Code below

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

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

Speech Recognition functions and Textfield... After I enter the inputs I call the function 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")
    }
}

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

For example both would print word "قل" in Arabic. But when I call the function, the console shows (answer):

Console 
//matching texts
//not matching

我希望第二个也能工作/匹配和打印匹配的文本.

I want the second one to also work/match and print matching texts.

虽然两个变量都包含相似的文本,但结果有点令人困惑.它们是不是存储相同的值,还是它们显示相同的字符串但在代码深处它们以某种方式有所不同?

Although both variables contain similar text the result is a bit confusing. Do they not store same values or is it they show the same stringa but deep in code they vary somehow?

非常感谢您的支持:)

这是完整的代码

这是完整的代码.

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

推荐答案

因此,在测试代码后,我发现语音识别文本比硬编码字符串包含更多字符.当我将 .count() 用于 textField 文本قل"时;它打印了 2,但对于语音识别,它打印了 3.

So after testing the code I figured out that the speech recognition text had more characters than the hardcoded string. When I used .count() for the textField text "قل" it printed 2, but for the speech recognition, it had printed 3.

我使用映射方法查看多余字符.

I used the mapping method to view the extra character.

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

看起来第一个字符是一个空格.所以我用了

It looked like the first character was an empty space. So I used

speechRecogText.removeFirst()

然后比较.结果如我所愿.

and then compared. And the result was as desired.

这篇关于听写(语音识别)文本与 Swift 中的字符串不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆