在字符串中计算音节

时间:2014-11-24 18:48:27

标签: python-3.x counting

new_listes(poem_line):
    r""" (list of str) -> list of str 

    Return a list of lines from poem_lines
    """
    new_list = []
    for line in poem_lines:
        new_list.append(clean_up(line))    
    return new_list


    Precondition: len(poem_lines) == len(pattern[0])

    Return a list of lines from poem_lines that do not have the right number of
    syllables for the poetry pattern according to the pronunciation dictionary.
    If all lines have the right number of syllables, return th
    """
    k = ""
    i=0
    lst = []
    for line in new_listes(poem_lines):
        for word in line.split():
                for sylables in word_to_phonemes[word]:
                    for char in sylables:
                        k = k + char
        if k.isdigit():
            i=i+1
        return i

所以对于身体而言,这是我迄今为止所写的内容。我有一个由音素构建的单词列表(['N', 'EH1', 'K', 'S', 'T']用于单词next),我想查看有多少位数(1 EH1使其成为1 {1}}单词`next'但我得到0。 我尝试将代码移到不同的缩进处,但我不确定如何从那里开始。

1 个答案:

答案 0 :(得分:1)

如果我理解正确您要求您将此行'The first line leads off'转换为如此列出的行:

[
    ['DH', 'AH0'],          # The
    ['F', 'ER1', 'S', 'T'], # first
    ['L', 'AY1', 'N'],      # line
    ['L', 'IY1', 'D', 'Z'], # leads
    ['AO1', 'F']            # off
]

并计算包含数字的元素数量(在提供的示例中为5个AH0ER1AY1IY1AO1)。

你正在做的是建立一个像:

这样的字符串
'DH'
'DHAH0'
'DHAH0F'
>>> 'DHAH0FER1'.isdigit()
False

您需要计算digits in string

def number_count(input_string):
     return sum(int(char.isdigit()) for char in input_string)

>>> number_count('a1b2')
2

在你的代码中使用它(你不需要构建字符串,你可以动态计算数字):

lst = []
for line in new_listes(poem_lines):
    i = 0
    for word in line.split():
            for sylables in word_to_phonemes[word]:
                for char in sylables:
                    i += number_count(char)
    lst.append(i)

return lst

或者多做一些 pythonically

for line in new_listes(poem_lines):
    i = 0
    for word in line.split():
        for sylables in word_to_phonemes[word]:
            i += sum(number_count(char) for char in sylables)
    yield i

或者如果你想保留你的代码(并在返回之前首先构建字符串):

lst = []
for line in new_listes(poem_lines):
    k = "" # K needs to reset after each line
    for word in line.split():
        for sylables in word_to_phonemes[word]:
            for char in sylables:
                k = k + char
    lst.append(number_count(k))
return lst

他们都应该返回列表(或生成器)返回[5,5,4]


仅列出与

不匹配的记录

我不确定pattern[1]应该是什么意思,但我们假设您想要使用line[n]pattern[0][n]pattern[1][n]的每一行,最简单的方法是使用zip()

for line, pattern0, pattern1 in zip(new_listes(poem_lines), pattern[0], pattern[1]):
    i = 0
    for word in line.split():
        for sylables in word_to_phonemes[word]:
            i += sum(number_count(char) for char in sylables)

    if i != pattern0:
        yield line