Skip to content

Instantly share code, notes, and snippets.

@AlexDel
Created January 11, 2012 09:05
Show Gist options
  • Save AlexDel/1593818 to your computer and use it in GitHub Desktop.
Save AlexDel/1593818 to your computer and use it in GitHub Desktop.
NLTK Ex 2.21 Write a program to guess the number of syllables contained in a text, making use of the CMU Pronouncing Dictionary
d = nltk.corpus.cmudict.dict() #получаем объект в виде словаря для удобного доступа
def count_syllables(text): #вводим текст как список слов
syll_text = [] #исходный массив где будут копиться слоги
for word in text:
syll_text.extend(d[word][0]) #к исходному массиву добавляем первый элемент (в случае нескольких произношений)с помощью метода extend
return len(syll_text)# ву-а-ля
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment