检查音节,python

时间:2014-11-23 21:57:18

标签: python list for-loop dictionary key

所以我正在尝试编写将执行以下操作的代码:

r""" (list of str, poetry pattern, pronunciation dictionary) -> list of str

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 the empty list.

>>> poem_lines = ['The first line leads off,', 'With a gap before the next.', 'Then the poem ends.']
>>> pattern = ([5, 5, 4], ['*', '*', '*'])
>>> word_to_phonemes = {'NEXT': ['N', 'EH1', 'K', 'S', 'T'],
...                     'GAP': ['G', 'AE1', 'P'],
...                     'BEFORE': ['B', 'IH0', 'F', 'AO1', 'R'],
...                     'LEADS': ['L', 'IY1', 'D', 'Z'],
...                     'WITH': ['W', 'IH1', 'DH'],
...                     'LINE': ['L', 'AY1', 'N'],
...                     'THEN': ['DH', 'EH1', 'N'],
...                     'THE': ['DH', 'AH0'], 
...                     'A': ['AH0'], 
...                     'FIRST': ['F', 'ER1', 'S', 'T'], 
...                     'ENDS': ['EH1', 'N', 'D', 'Z'],
...                     'POEM': ['P', 'OW1', 'AH0', 'M'],
...                     'OFF': ['AO1', 'F']}
>>> check_syllables(poem_lines, pattern, word_to_phonemes)
['With a gap before the next.', 'Then the poem ends.']
>>> poem_lines = ['The first line leads off,']
>>> check_syllables(poem_lines, ([0], ['*']), word_to_phonemes)
[]
"""

我不明白它究竟是如何工作的,因为列表中的每个字符串都有比给定的更多的音节。例如:第一个有7个音节但是模式是5,第二个有8个但是再次给出5个,依此类推。

我不清楚从什么开始以及我的代码应该做什么,到目前为止我有这个:

def check_syllables(poem_lines, pattern, word_to_phonemes):
   if  len(poem_lines) == len(pattern[0]):
        for k in word_to_phonemes:
            for v in poem_lines:
                if k is in v:
                   k , v = v, k

1 个答案:

答案 0 :(得分:1)

以下问题是如何解决的:

(1)所需功能的签名:

r""" (list of str, poetry pattern, pronunciation dictionary) -> list of str

(2)对输入结构的保证:

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

(3)返回值的说明:

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 the empty list.

(4)样本输入 - 第一个参数[str的列表]:

>>> poem_lines = ['The first line leads off,', 'With a gap before the next.', 'Then the poem ends.']

(5)样本输入 - 第二个参数[诗歌模式]:

>>> pattern = ([5, 5, 4], ['*', '*', '*'])

(6)样本输入 - 第三个参数[发音字典]:

>>> word_to_phonemes = {'NEXT': ['N', 'EH1', 'K', 'S', 'T'],
...                     'GAP': ['G', 'AE1', 'P'],
...                     'BEFORE': ['B', 'IH0', 'F', 'AO1', 'R'],
...                     'LEADS': ['L', 'IY1', 'D', 'Z'],
...                     'WITH': ['W', 'IH1', 'DH'],
...                     'LINE': ['L', 'AY1', 'N'],
...                     'THEN': ['DH', 'EH1', 'N'],
...                     'THE': ['DH', 'AH0'], 
...                     'A': ['AH0'], 
...                     'FIRST': ['F', 'ER1', 'S', 'T'], 
...                     'ENDS': ['EH1', 'N', 'D', 'Z'],
...                     'POEM': ['P', 'OW1', 'AH0', 'M'],
...                     'OFF': ['AO1', 'F']}

(7)第一个样本输出 - 返回值[str的列表]:

>>> check_syllables(poem_lines, pattern, word_to_phonemes)
['With a gap before the next.', 'Then the poem ends.']

(8)第二个样本输出 - 返回值[str的列表]:

>>> poem_lines = ['The first line leads off,']
>>> check_syllables(poem_lines, ([0], ['*']), word_to_phonemes)
[]
"""

第一个样本输出(7)显示输入行(5)的预期返回值,其中不匹配模式(6)。也就是说,三个输入行具有模式[5, 7, 5],它与最后两个元素中的模式[5, 5, 4]不匹配,因此函数返回相应的行。

第二个样本输入(8)显示当输入行(5)执行与模式(6)匹配时输出应该是什么 - 即应返回空列表。

<强> PS

看起来(8)中存在错误。示例代码应该是:

>>> check_syllables(poem_lines, ([5], ['*']), word_to_phonemes)