python - 将单词分成音节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))
解决方案
虽然我同意你的方法会有很多失败的评论,但如果没关系,根据你的实现,你可以编写一个函数来完全按照你的描述来拆分单词:
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]
推荐阅读
- .net - 为什么在 vb.net 中调整窗体顶部或左侧的大小时,无边框窗体会闪烁?
- javascript - 如何动态加载 mailchimp 脚本
- c# - Serilog - 单独的信息文件,使用 appsetting.json 的异常
- node.js - 如何从客户端测量 Websocket 背压或网络缓冲区
- spring-boot - 将旧的 DAO bean 添加到 @DataJpaTest
- reactjs - 使用 rxjs/redux (React, socket.io) 防止重复的套接字消息
- azure-artifacts - Azure 工件中通用包的直接下载 URL
- html - 变换比例导致图像脱离身体
- php - $_SESSION 不保留页面之间的值
- huawei-mobile-services - 使用HUAWEI Drive Kit查询文件列表报错