def get_word_syllables(word, pronouncingDict):
# returns a list of transcriptions for a word
# (a word may have alternative pronunciations)
# eg. 'orange':
# -> [['AO1', 'R', 'AH0', 'N', 'JH'], ['A01', 'R', 'IH0', 'N', 'JH']]
# vowels are marked with numbers from 0-2
# by counting the vowels we can get the number of syllables
try:
# last element is more common
pronList = pronouncingDict[word.lower()][-1]
sylCount = len([ syl for syl in pronList if syl[-1].isdecimal() ])
return sylCount
except KeyError:
# scrape syllable count
url = 'http://www.syllablecount.com/syllables/' + word
request = urllib.request.urlopen(url)
response = request.read().decode('utf-8')
# use regex to match desired value
sylCount = int(
re.search("(?<=<b style='color: #008000'>)[0-9]+", response)[0]
)
return sylCount;
评论列表
文章目录