Python中的音节计数
我需要写一个函数来读取单词中的音节(例如,HAIRY是2个音节)。我的代码显示在底部,我相信它在大多数情况下都能工作,因为它适用于我所做的所有其他测试,但不适用于“毛茸茸”的测试,因为它只能读一个音节Python中的音节计数,python,function,Python,Function,我需要写一个函数来读取单词中的音节(例如,HAIRY是2个音节)。我的代码显示在底部,我相信它在大多数情况下都能工作,因为它适用于我所做的所有其他测试,但不适用于“毛茸茸”的测试,因为它只能读一个音节 def syllable_count(word): count = 0 vowels = "aeiouy" if word[0] in vowels: count += 1 for index in range(1, len(word)):
def syllable_count(word):
count = 0
vowels = "aeiouy"
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 count == 0:
count += 1
return count
试验
print(syllable_count("HAIRY"))
预期:2
Received:1问题是您给它一个大写字符串,但您只比较小写值。这可以通过在函数开头添加
word=word.lower()
来解决
def syllable_count(word):
word = word.lower()
count = 0
vowels = "aeiouy"
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 count == 0:
count += 1
return count
print(syllable_count('HAIRY')) # prints "2"
当以小写形式给出任何内容时,您的代码似乎运行良好。但是,如果您用大写字母传递一个单词,它将始终返回1。这是因为你测试的是“aeiou”,而不是“aeiouAEIOU”。 你可以用几种方法解决这个问题 例1:
vowels = "aeiouyAEIOUY"
例2:
print(syllable_count("HAIRY".lower()))
示例3:在“音节计数”函数的开头添加这行代码
word = word.lower()
音节检测有某些规则,您可以从网站上查看这些规则: 下面是python代码:
import re
def sylco(word) :
word = word.lower()
# exception_add are words that need extra syllables
# exception_del are words that need less syllables
exception_add = ['serious','crucial']
exception_del = ['fortunately','unfortunately']
co_one = ['cool','coach','coat','coal','count','coin','coarse','coup','coif','cook','coign','coiffe','coof','court']
co_two = ['coapt','coed','coinci']
pre_one = ['preach']
syls = 0 #added syllable number
disc = 0 #discarded syllable number
#1) if letters < 3 : return 1
if len(word) <= 3 :
syls = 1
return syls
#2) if doesn't end with "ted" or "tes" or "ses" or "ied" or "ies", discard "es" and "ed" at the end.
# if it has only 1 vowel or 1 set of consecutive vowels, discard. (like "speed", "fled" etc.)
if word[-2:] == "es" or word[-2:] == "ed" :
doubleAndtripple_1 = len(re.findall(r'[eaoui][eaoui]',word))
if doubleAndtripple_1 > 1 or len(re.findall(r'[eaoui][^eaoui]',word)) > 1 :
if word[-3:] == "ted" or word[-3:] == "tes" or word[-3:] == "ses" or word[-3:] == "ied" or word[-3:] == "ies" :
pass
else :
disc+=1
#3) discard trailing "e", except where ending is "le"
le_except = ['whole','mobile','pole','male','female','hale','pale','tale','sale','aisle','whale','while']
if word[-1:] == "e" :
if word[-2:] == "le" and word not in le_except :
pass
else :
disc+=1
#4) check if consecutive vowels exists, triplets or pairs, count them as one.
doubleAndtripple = len(re.findall(r'[eaoui][eaoui]',word))
tripple = len(re.findall(r'[eaoui][eaoui][eaoui]',word))
disc+=doubleAndtripple + tripple
#5) count remaining vowels in word.
numVowels = len(re.findall(r'[eaoui]',word))
#6) add one if starts with "mc"
if word[:2] == "mc" :
syls+=1
#7) add one if ends with "y" but is not surrouned by vowel
if word[-1:] == "y" and word[-2] not in "aeoui" :
syls +=1
#8) add one if "y" is surrounded by non-vowels and is not in the last word.
for i,j in enumerate(word) :
if j == "y" :
if (i != 0) and (i != len(word)-1) :
if word[i-1] not in "aeoui" and word[i+1] not in "aeoui" :
syls+=1
#9) if starts with "tri-" or "bi-" and is followed by a vowel, add one.
if word[:3] == "tri" and word[3] in "aeoui" :
syls+=1
if word[:2] == "bi" and word[2] in "aeoui" :
syls+=1
#10) if ends with "-ian", should be counted as two syllables, except for "-tian" and "-cian"
if word[-3:] == "ian" :
#and (word[-4:] != "cian" or word[-4:] != "tian") :
if word[-4:] == "cian" or word[-4:] == "tian" :
pass
else :
syls+=1
#11) if starts with "co-" and is followed by a vowel, check if exists in the double syllable dictionary, if not, check if in single dictionary and act accordingly.
if word[:2] == "co" and word[2] in 'eaoui' :
if word[:4] in co_two or word[:5] in co_two or word[:6] in co_two :
syls+=1
elif word[:4] in co_one or word[:5] in co_one or word[:6] in co_one :
pass
else :
syls+=1
#12) if starts with "pre-" and is followed by a vowel, check if exists in the double syllable dictionary, if not, check if in single dictionary and act accordingly.
if word[:3] == "pre" and word[3] in 'eaoui' :
if word[:6] in pre_one :
pass
else :
syls+=1
#13) check for "-n't" and cross match with dictionary to add syllable.
negative = ["doesn't", "isn't", "shouldn't", "couldn't","wouldn't"]
if word[-3:] == "n't" :
if word in negative :
syls+=1
else :
pass
#14) Handling the exceptional words.
if word in exception_del :
disc+=1
if word in exception_add :
syls+=1
# calculate the output
return numVowels - disc + syls
重新导入
def sylco(word):
word=word.lower()
#例外情况是需要额外音节的单词
#例外情况是需要较少音节的单词
异常添加=[“严重”、“关键”]
例外情况=幸运、不幸
COU one=[“酷”,“coach”,“coat”,“coal”,“count”,“coin”,“cool”,“cout”,“coif”,“coif”,“cook”,“coign”,“coiffe”,“coof”,“coof”,“court”]
cou_two=['coapt','coed','coinci']
前一节=[“布道”]
syls=0#增加音节数
disc=0#丢弃的音节数
#1) 如果字母<3:返回1
如果len(word)您也可以使用lambda映射
fun_check = lambda x: 1 if x in ["a","i","e","o","u","y","A","E","I","O","U","y"] else 0
sum(list(map(fun_check,"your_string")))
单行
sum(list(map(lambda x: 1 if x in ["a","i","e","o","u","y","A","E","I","O","U","y"] else 0,"your string")))
多个示例失败,“announcement”返回4(应该是3),“columbia”返回3(应该是4),…更多失败示例:“trued”,“course”,对于“tickle”,函数返回1,但应该是2。我认为添加这个应该会有所帮助-word.endswith(“e”)而不是word.endswith(“le”)