python 单词拆音节_计算一个单词的音节数

我是个初学者,我有一个问题需要帮助。这是家庭作业,所以任何提示都是值得赞赏的。我也看到过一些类似的话题,但答案超出了我所知道的范围。。。

作为大型程序的一部分,我需要计算文本文件中的音节数。除了音节我什么都有。我试过几种不同的方法,但并不总是能抓住特殊情况。我应该“数一组相邻的元音,不包括单词末尾的‘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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值