Skip to content

Instantly share code, notes, and snippets.

@jeffThompson
Last active July 18, 2023 12:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffThompson/26396f49a127c85171b3f52c92794a08 to your computer and use it in GitHub Desktop.
Save jeffThompson/26396f49a127c85171b3f52c92794a08 to your computer and use it in GitHub Desktop.
Splits a word into syllables
'''
SPLIT SYLLABLES
Jeff Thompson | 2016 | jeffreythompson.org
Requires this modified version of the CMU Pronouncing
dictionary by Susan Bartlett, Grzegorz Kondrak and Colin Cherry:
https://webdocs.cs.ualberta.ca/~kondrak/cmudict.html
Download and save to your project directory, or somewhere you can
easily reference it.
'''
dict_filename = 'cmudict.rep'
syllable_dict = {}
with open(dict_filename) as f:
for line in f:
line = line.strip()
line = line.lower()
# ignore comments
if line.startswith('##'):
continue
try:
word, phones = line.split(' ')
syll = phones.split(' - ')
syllable_dict[word] = syll
except:
print 'error parsing word ' + word
print syllable_dict['the']
print syllable_dict['beautiful']
print syllable_dict['serious']
print syllable_dict['seriously']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment