【问题标题】:Swift - How Speech basic operator - Symbol (+, -, ×)?Swift - 如何语音基本运算符 - 符号(+,-,×)?
【发布时间】:2019-05-20 22:07:23
【问题描述】:

我尝试设置一个按钮来对标签中的文本进行语音(使用不同的声音/语言),但是当有运算符符号(+、-、×)时语音效果不佳,任何解决此问题的想法有问题吗?

我试过了:

//Option 1
TextLabel.text = "1 + 2 - 3 × 4" // Result: "+" = "and" other voice "plus" (ok), "-" = mute, "×" = mute, other voice "X" (letter)

//Option 2
TextLabel.text = "1 ➕ 2 ➖ 3 ✖️ 4" // Result: "+" = plus symbol, "-" = minus symbol, "×" = multiplication symbol
import UIKit
import AVFoundation
import Speech

class ViewController: UIViewController {
    let synth = AVSpeechSynthesizer()

    @IBAction func speaker(_ sender: UIButton) {
        if (!synth.isSpeaking) {
            let speech = AVSpeechUtterance(string: TextLabel.text!)

            speech.voice = AVSpeechSynthesisVoice(language: "en-US")
            speech.rate = 0.5
            speech.pitchMultiplier = 1
            speech.volume = 1
            synth.speak(speech)
        }
    }

    @IBOutlet weak var TextLabel: UILabel!

    //Option 1
    TextLabel.text = "1 + 2 - 3 × 4" // "+" = "and" other voice "plus" (ok), "-" = mute, "×" = mute, other voice "X" (letter)

    //Option 2
    TextLabel.text = "1 ➕ 2 ➖ 3 ✖️ 4" // "+" = plus symbol, "-" = minus symbol, "×" = multiplication symbol
}

我希望使用 AVSpeechSynthesisVoice 在不同的语言中正确地语音符号 +(加号)、-(减号)、×(次),但选项 1 不正确或使某些符号静音......并且选项 2 更好但是再现“符号”一词

【问题讨论】:

    标签: swift speech-recognition avspeechsynthesizer


    【解决方案1】:

    ...当有运算符符号(+、-、×)时,语音效果不太好,有什么办法解决这个问题吗?

    为了获得最准确的结果,您应该删除任何模棱两可的符号 (不可能可靠地检测到必须读出的上下文)并替换它们带有拼写形式。

    我希望符号+(加号),-(减号),×(次)在不同的语言中正确...

    我建议使用NSLocalizedString(, comment:),以便您的每个符号都以不同的语言读出。

    下面提供一个非常简单的例子(去掉和替换符号)

    class ViewController: UIViewController {
    
        var synthesizer = AVSpeechSynthesizer()
    
        override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
    
            let string1 = "1"
            let string2 = "5"
            let result = "6"
    
            let finalString = string1 + NSLocalizedString("MyPlus", comment: "") + string2 + NSLocalizedString("MyEqual", comment: "") + result
    
            let utterance = AVSpeechUtterance(string: finalString)
            let synthesizer = AVSpeechSynthesizer()
            synthesizer.speak(utterance)
        }
    }
    

    为您用英语定义以下术语的每种语言创建一个Localizable.strings

    "MyPlus" = " plus ";
    "MyEqual" = " is equal to ";
    

    【讨论】:

      【解决方案2】:

      您可以使用符号,只需使用自定义发音来代替说话。

      import AVFoundation
      
      let text = "1 ➕ 2 ➖ 3 ✖️ 4"
      let rangeOne = NSString(string: text).range(of: "➕")
      let rangeTwo = NSString(string: text).range(of: "➖")
      let rangeThree = NSString(string: text).range(of: "✖️")
      let mutableAttributedString = NSMutableAttributedString(string: text)
      
      let pronunciationKey = NSAttributedString.Key(rawValue: AVSpeechSynthesisIPANotationAttribute)
      mutableAttributedString.setAttributes([pronunciationKey: "plʌs"], range: rangeOne)
      
      mutableAttributedString.setAttributes([pronunciationKey: "ˈmaɪ.nəs"], range: rangeTwo)
      
      mutableAttributedString.setAttributes([pronunciationKey: "taɪmz"], range: rangeThree)
      let utterance = AVSpeechUtterance(attributedString: mutableAttributedString)
      
      
      utterance.voice = AVSpeechSynthesisVoice(language: "en-GB")
      
      let synthesizer = AVSpeechSynthesizer()
      synthesizer.speak(utterance)
      

      来自NShipster

      AVUtterance 添加了控制发音的功能 特定的词,这对于专有名词特别有用。

      为了利用它,构建一个话语使用 init(attributedString:) 而不是 init(string:)。初始化器 扫描属性字符串以查找与 AVSpeechSynthesisIPANotationAttribute,并调整发音 相应地。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-07-06
        • 2012-02-28
        • 2021-09-18
        • 1970-01-01
        • 1970-01-01
        • 2016-01-21
        • 1970-01-01
        相关资源
        最近更新 更多