I try to set a button to speech the text in a label (with different voices / languages), but the speech is not working very well when there are operator symbols (+, -, ×), any idea to fix this problem?
I tried:
//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
}
I expect speech the symbol + (plus), - (minus), ×(times) correctly in different languages with AVSpeechSynthesisVoice, but the Option 1 is not correct or mute some symbol... and the Option 2 is better but reproduce the word "symbol"
...the speech is not working very well when there are operator symbols (+, -, ×), any idea to fix this problem?
To get the most accurate results, you should remove any ambiguous symbol (it isn't possible to reliably detect the context they must be read out) and replace them with spelled-out forms.
I expect speech the symbol + (plus), - (minus), ×(times) correctly in different languages...
I suggest to use NSLocalizedString(, comment:)
in order for each one of your symbols to be read out in different languages.
A very simple example is provided hereafter (remove and replace the symbols):
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)
}
}
Create one Localizable.strings
for each language where you define the following terms in english for instance:
"MyPlus" = " plus ";
"MyEqual" = " is equal to ";