计算单词中的音节数

时间:2013-01-26 20:33:58

标签: python python-3.x

我是初学者,我有一个需要帮助的问题。它是功课,所以任何提示都受到赞赏。我看过几个类似的话题,但答案超出了我所知道的怎么做......

我需要将文本文件中的音节数量计算为较大程序的一部分。除了音节,我有我需要的一切。我尝试了几种不同的方法,但它并不总能捕捉特殊情况。我应该'计算相邻元音的组,不包括单词末尾的'e'。我明白这意味着什么,但我无法在我的计划中做到这一点。这是我的:::

def syllables(word):
    syl = 0
    vowels = 'aeiouy'
    starts = ['ou','ei','ae','ea','eu','oi']
    endings = ['es','ed','e']
    word = word.lower().strip(".:;?!")
    for vowel in vowels:
        syl +=word.count(vowel)
    for ending in endings:
        if word.endswith(ending):
            syl -=1
    for start in starts:
        if word.startswith(start):
            syl -=1
    if word.endswith('le'):
        syl +=1
    if syl == 0:
        syl+=1
    return syl

编辑:新代码

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
    return count

2 个答案:

答案 0 :(得分:5)

只是一个建议,但不是“寻找”相邻的元音,每次遇到一个单词的开头或单词中的辅音发生的初始元音时,你不能增加'计数',除了一个单词末尾的'e'(除非你的计数为零)。为了澄清,每当遇到相邻的元音时,只有第一个元音会增加你的计数。

不是肯定它会起作用,但我认为它适用于我刚写的所有单词。

祝你好运。

答案 1 :(得分:1)

该主题已在How to get the number of syllables in a word?

中进行了讨论

他们得出的结论是,在CMU发音词典中未出现的单词应像此处讨论的那样用短函数来处理。

另一个建议的解决方案是使用pyphen

更简单:Wikipedia的文章https://en.wikipedia.org/wiki/Hyphenation_algorithm链接到Francis Mark Liang's hyphenation algorithm的Python实现。该算法相当古老,但仍在TeX中使用。

>>> import hyphenate
>>> hyphenate.hyphenate_word("computer")
['com', 'put', 'er']
相关问题