看网上关于这方面的讨论比较少,特意记录一下。方法一和方法二是计算单个词汇的音节数,方法三可以统计一句话总共的音节数。
方法一: from curses.ascii import isdigit from nltk.corpus import cmudict def nsyl(word): d = cmudict.dict() return max([len([y for y in x if isdigit(y[-1])]) for x in d[word.lower()]]) 方法二: def syllables(word): count = 0 vowels = 'aeiouy' word = word.lower().strip("_.:;?!") if word[0] in vowels: count += 1 for index in range(1, len(word)): if word[index] in vowels and word[index - 1] not in vowels: count += 1 if word.endswith('e'): count -= 1 if word.endswith('le'): count += 1 if count == 0: count += 1 print("{0}:{1}".format(word, count)) return count 方法三: import textstat print(textstat.syllable_count(str))