Swift - 基本运算符 - 符号(+、-、×)怎么读?
Swift - How Speech basic operator - Symbol (+, -, ×)?
我尝试设置一个按钮来朗读标签中的文本(使用不同的声音/语言),但是当有运算符符号(+、-、×)时,语音效果不佳,任何想法解决这个问题?
我试过了:
//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 是更好,但重现单词 "symbol"
...the speech is not working very well when there are operator symbols (+, -, ×), any idea to fix this problem?
要获得最准确的结果,您应该删除任何不明确的符号 (无法可靠地检测必须读出的上下文) 并将它们替换为 spelled-out 形式。
I expect speech the symbol + (plus), - (minus), ×(times) correctly in different languages...
我建议使用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 ";
你可以使用符号,只是用自定义发音来代替说话。
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 added functionality to control the pronunciation of
particular words, which is especially helpful for proper names.
To take advantage of it, construct an utterance using
init(attributedString:) instead of init(string:). The initializer
scans through the attributed string for any values associated with the
AVSpeechSynthesisIPANotationAttribute, and adjusts pronunciation
accordingly.
我尝试设置一个按钮来朗读标签中的文本(使用不同的声音/语言),但是当有运算符符号(+、-、×)时,语音效果不佳,任何想法解决这个问题?
我试过了:
//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 是更好,但重现单词 "symbol"
...the speech is not working very well when there are operator symbols (+, -, ×), any idea to fix this problem?
要获得最准确的结果,您应该删除任何不明确的符号 (无法可靠地检测必须读出的上下文) 并将它们替换为 spelled-out 形式。
I expect speech the symbol + (plus), - (minus), ×(times) correctly in different languages...
我建议使用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 ";
你可以使用符号,只是用自定义发音来代替说话。
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 added functionality to control the pronunciation of particular words, which is especially helpful for proper names.
To take advantage of it, construct an utterance using init(attributedString:) instead of init(string:). The initializer scans through the attributed string for any values associated with the AVSpeechSynthesisIPANotationAttribute, and adjusts pronunciation accordingly.