首页 > 解决方案 > 将单词分成音节python

问题描述

我有一个名为 syllable_split(word_input) 的函数,它接收一个单词并计算音节数并仅返回一个包含给定单词音节的列表。
例如
混乱 ----> ['pan', 'de', 'mo', 'ni', 'um']

自以为是---> ['self', 'right','eous', 'ly']

你好---> ['hel','lo]

饮食----> ['di','et]

七 ---> ['sev','en']

我的函数正确计算音节,但我无法将单词拆分为其相应的音节。我只设法将单词拆分为其第一个对应的音节,但它往往不适用于某些单词。例如,我输入“七”,我只得到“se”而不是“sev”。我正在考虑遵循音节划分模式(vc/cv,c/cv,vc/v,v/v),但我无法将其实现到我的函数中。

def syllable_split(word_input):
count = 0
word = word_input.lower()
vowels = set("aeiou")
syll = list()
temp = 0
for letter in word:
    if letter in vowels:
        count += 1
if count == 1:
    return word
for index in range(count, len(word)):
    if word[index] in vowels and word[index - 1] not in vowels:
        w = word[temp: index - 1]
        if len(w) != 0:
            syll.append(w)
            temp = index - 1
return syll

user_input = input()
print(syllable_split(user_input))

标签: python

解决方案


虽然我同意你的方法会有很多失败的评论,但如果没关系,根据你的实现,你可以编写一个函数来完全按照你的描述来拆分单词:

vowels = 'AEIOU'
consts = 'BCDFGHJKLMNPQRSTVWXYZ'
consts = consts + consts.lower()
vowels = vowels + vowels.lower()

def is_vowel(letter):
    return letter in vowels 
def is_const(letter):
    return letter in consts

# get the syllables for vc/cv
def vc_cv(word):
    segment_length = 4 # because this pattern needs four letters to check
    pattern = [is_vowel, is_const, is_const, is_vowel] # functions above
    split_points = []

    # find where the pattern occurs
    for i in range(len(word) - segment_length):
        segment = word[i:i+segment_length]

        # this will check the four letter each match the vc/cv pattern based on their position
        # if this is new to you I made a small note about it below
        if all([fi(letter) for letter, fi in zip(segment, pattern)]):
            split_points.append(i+segment_length/2)

    # use the index to find the syllables - add 0 and len(word) to make it work
    split_points.insert(0, 0)
    split_points.append(len(word))
    syllables = []
    for i in range(len(split_points) - 1):
        start = split_points[i]
        end = split_points[i+1]
        syllables.append(word[start:end])
    return syllables

word = 'vortex'
print(vc_cv(word))
# ['vor', 'text']

您可以对其他模式执行类似的操作,例如,c/cvpatterns=[is_const, is_const, is_vowel]的段长度为 3

  • 注意您可以将函数放在列表中:
def linear(x):
    return x
def squared(x):
    return x * x 
def cubed(x):
    return x * x * x 

funcs =  [linear, squared, cubed]
numbers = [2, 2, 2]
transforms = [fi(ni) for ni, fi in zip(numbers, funcs)]
# results -> [2, 4, 8]

推荐阅读