2

For speaking out numbers with an AVSpeechUtterance, I would like Siri to speak numbers in ways that respect the convention for the type of number.

For a date, I would like it to pronounce 1492 as fourteen ninety two rather then one thousand, four hundred, ninety-two.

For the phone number 650-412-3456, I would like it to say six five oh, four one two three four five six as opposed to six hundred fifty dash four hundred 12 dash three,thousand four hundred fifty six.

Is there anyway to specify pronunciation using AVSpeech and AVUtterance? There does not seem to be anything obvious in the docs.

user6631314
  • 1,751
  • 1
  • 13
  • 44

2 Answers2

1

While not an AV setting, parsing the phrase for the speaker would get the desired results.

For example, using the extension below:

let number = "1492"
let phrase = number.separate(every: 2, with: " ")
print(phrase) // 14 92

And for the phone:

let phone   = "650-412-3456"
let parts = phone.components(separatedBy: CharacterSet.decimalDigits.inverted)
var phrase2 = String()

for part in parts {
  if Int(part) != nil {
    phrase2.append(String(describing: part).separate(every: 1, with: " ") + ",")
  }
} 

print(phrase2) // 6 5 0,4 1 2,3 4 5 6,

Commas were added for a more natural reading by the speech synthesizer but they could be left off.

String extension from Joe Maher:

extension String {
  func separate(every: Int, with separator: String) -> String {
    return String(stride(from: 0, to: Array(self).count, by: every).map {
       Array(Array(self)[$0..<min($0 + every, Array(self).count)])
       }.joined(separator: separator))
  }
}
Marcy
  • 4,611
  • 2
  • 34
  • 52
1

Is there anyway to specify pronunciation using AVSpeech and AVUtterance? There does not seem to be anything obvious in the docs.

The best way to get your purpose is to provide the appropriate format for the text to be read out.

The code snippet hereunder highlights this assertion with few examples:

    class ViewController: UIViewController {

        let synthesizer = AVSpeechSynthesizer()

        override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)

            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "dd/MM/yyyy HH:mm"

            let date = dateFormatter.date(from: "01/04/2015 05:30")

            let dateStr = DateFormatter.localizedString(from: date!,
                                                        dateStyle: .medium,
                                                        timeStyle: .short)
            let dateUtterance = AVSpeechUtterance(string: dateStr)
            dateUtterance.voice = AVSpeechSynthesisVoice(language: "en-GB")

            let hourComponents = Calendar.current.dateComponents([.hour, .minute],
                                                                 from: date!)
            let hourStr = DateComponentsFormatter.localizedString(from: hourComponents,
                                                                  unitsStyle: .spellOut)
            let hourUtterance = AVSpeechUtterance(string: hourStr!)
            hourUtterance.voice = AVSpeechSynthesisVoice(language: "en-GB")

            let numberValue = NSNumber(value: 54038921.7)
            let nbStr = NumberFormatter.localizedString(from: numberValue,
                                                        number: .decimal)
            let nbUtterance = AVSpeechUtterance(string: nbStr)
            nbUtterance.voice = AVSpeechSynthesisVoice(language: "en-GB")

            synthesizer.speak(hourUtterance) //Only time
            synthesizer.speak(dateUtterance) //The complete date and hour
            synthesizer.speak(nbUtterance) //Just for numbers
        }
    }

More info with code snippets (Objc and Swift) are available if this example isn't enough.

XLE_22
  • 5,124
  • 3
  • 21
  • 72
  • The website you linked to seems really good and to go beyond Apple's standard stuff. To be clear, no additional library is required and you don't need to call the accessibility methods? – user6631314 May 25 '19 at 14:04
  • @user6631314: all that is presented is definitely iOS native, nothing must be added and you definitely don't need to call the accessibility methods as shown in my code example here. – XLE_22 May 25 '19 at 15:08
  • Really cool approach and I upvoted. I accepted other as slightly simpler but your method is very elegant. Wish I could accept both. – user6631314 May 25 '19 at 18:02