A lightweight syllable counter written in Swift.
Syllable counting does not have a simple algorithmic solution so this framework takes a combination of dictionary lookups and heuristics, in two parts:
(1) Python's Natural Language Toolkit includes a wide range of corpus for language processing. Python NLTK takes advantage of the CMU Pronunciation Dictionary which contains pronounciation transcriptions for over 100,000 words. If the word is found within the pronunciation dictionary, the first valid pronunciation is used to find the number of syllables.
(2) If the word is not found within the pronunciation dictionary, SwiftSyllables defaults to a heuristic solution. We used a straightforward implementation of Emre Aydin's heuristic syllable counting algorithm.
The current most robust algorithmic solution to syllable counting is Frank Liang's Stanford PhD dissertation -- the implementation of the algorithm for this framework is not currently planned.
Uses Swift 5.0, run on Xcode 11+.
SwiftSyllables is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "SwiftSyllables"
SwiftSyllables currently has one simple API for getting the syllable count from a string:
open class func getSyllables(_ string: String) -> Int
Simply import the framework and call the getSyllables
method.
import SwiftSyllables
...
var syllables : Int = SwiftSyllables.getSyllables(string)
To run the example project, clone the repo, and run pod install
from the Example directory first.
- Vivian Qu - hello@vivqu.com
SwiftSyllables is available under the MIT license. See the LICENSE file for more info.