Usage
let syllables = Syllables()
syllables.count("dog") // 1
syllables.count("rabbit") // 2
syllables.count("octopus") // 3
syllables.count("I like dogs, rabbits and octopi.") // 9
A 5MB dictionary file is loaded upon Syllables
initialisation. You may wish to hold a lazy reference to the Syllables
instance if you wish to delay the file load until first use.
private lazy var syllables = Syllables()
syllables.count("dog") // 1
The class caches returned values automatically. This saves a trip to the CMU Dictionary or algorithm for future values. You may wish to turn this off if every input is unique.
let syllables = Syllables()
syllables.isCaching = false
Installation
SPM
Add the following to your project:
https://github.com/ptrkstr/syllables
How Does it Work?
Sentences are broken down into words. These words are first processed through the CMU Pronounciation Dictionary and their syllables extracted. Any words that weren't found in the dictionary have their syllables extracted via an algorithm (with a small number of hardcoded exceptions). The total number of syllables in the sentence are then returned.
Found a Wrong Result?
Raise an issue and I'll investigate
Inspiration
This package is inspired by:
Possible Future Features
- Handle numbers; read number by number, or as a phrase