Swift-语音基本运算符-符号(+,-,×)

时间:2019-05-20 03:28:17

标签: swift speech-recognition avspeechsynthesizer

我尝试设置一个按钮来使标签中的文本语音化(具有不同的声音/语言),但是当存在操作员符号(+,-,×)时,语音无法很好地工作,任何解决此问题的想法有问题吗?

我尝试过:

//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更好,但是重现“符号”一词

2 个答案:

答案 0 :(得分:2)

  

...当存在操作员符号(+,-,×)时,语音不能很好地工作,有解决此问题的主意吗?

要获得最准确的结果,您应该删除任何不明确的符号 (无法可靠地检测出必须读出的上下文)并替换它们带有清晰的表格。

  

我希望语音能正确地用不同的语言显示+(加号),-(减号),×(倍数)...

我建议使用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 ";

答案 1 :(得分:0)

您可以使用符号,只需使用“自定义发音”代替语音即可。

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,并调整发音   相应地。