如何检测抑扬五音步?

4 投票
3 回答
10729 浏览
提问于 2025-04-19 08:57

我在处理一个关于抑扬五音步的问题时遇到了一些困难,不过我会尽量简化一下。
我需要从一个文本文件中获取一些单词及其重音模式,格式大概是这样的:

if, 0
music,10
be,1
the,0
food,1
of,0
love,1
play,0
on,1
hello,01
world,1

从这个文件中,你可以假设会有很多不同句子的单词。我想从一个包含多个句子的文本文件中提取句子,并检查这些句子(忽略标点和大小写)是否是抑扬五音步。

举个例子,如果文本文件里有这样的内容:

If music be the food of love play on
hello world

第一个句子的重音模式会从重音字典中得到,像这样:0101010101,而第二个句子显然不是五音步(011)。我希望程序只打印出那些是抑扬五音步的句子。

抱歉如果这个问题有点复杂或混乱。
这是我目前的进展:

import string
dict = {};
sentence = open('sentences.txt')
stress = open('stress.txt')
for some in stress:
  word,number = some.split(',')
  dict[word] = number
for line in sentence:
  one = line.split()

3 个回答

0

下面是完整代码的样子:

#!/usr/bin/env python3
def is_iambic_pentameter(words, word_stress_pattern):
    """Whether words are a line of iambic pentameter.

    word_stress_pattern is a callable that given a word returns
    its stress pattern
    """
    return ''.join(map(word_stress_pattern, words)) == '01'*5

# create 'word -> stress pattern' mapping, to implement word_stress_pattern(word)
with open('stress.txt') as stress_file:
    word_stress_pattern = dict(map(str.strip, line.split(','))
                               for line in stress_file).__getitem__

# print lines that use iambic pentameter
with open('sentences.txt') as file:
    for line in file:
        if is_iambic_pentameter(line.casefold().split(), word_stress_pattern):
            print(line, end='')
1

我本以为抑扬五音步是很明确的:总是有一些词为了适应节奏而被强调或不强调。不过无所谓。像这样:

for line in sentences:
    words = line.split()
    stresspattern = ''.join([dict[word] for word in words])
    if stresspattern=='0101010101':
         print line

顺便说一下,给你的字典起名为'dict'通常不是个好主意,因为你把dict这个类型给隐藏了。

2

我觉得你在建立压力字典的时候有些地方做得不太对。记得在读取每一行的时候,要去掉隐含的 \n 字符,同时在用逗号分割单词后,也要去掉单词前后的空格。现在这样的话,像 if, 0 这一行会被分割成 ['if', ' 0\n'],这可不是你想要的结果。

所以,要创建你的压力字典,你可以这样做:

stress_dict = {}

with open('stress.txt', 'r') as f:
    for line in f:
        word_stress = line.strip().split(',')
        word = word_stress[0].strip().lower()
        stress = word_stress[1].strip()
        stress_dict[word] = stress

至于实际的检查,@khelwood 的回答是个不错的方法,但我建议在读取每一行的时候,特别注意处理 \n 字符,并确保行中的所有字符都是小写的(就像你字典里的那样)。

定义一个函数 is_iambic_pentameter 来检查一句话是否是抑扬五音步(返回 True/False),然后检查 sentences.txt 中的每一行:

def is_iambic_pentameter(line):
    line_stresses = [stress_dict[word] for word in line.split()]
    line_stresses = ''.join(line_stresses)
    return line_stresses == '0101010101'

with open('sentences.txt', 'r') as f:
    for line in f:
        line = line.rstrip()
        line = line.lower()
        if is_iambic_pentameter(line):
            print line

顺便提一下,你可能会对 NLTK 感兴趣,这是一个用于Python的自然语言处理库。网上有很多人用这个库写了俳句生成器和其他评估诗歌形式的脚本。

撰写回答