Check for iambic pentameter?

I'm kind of stuck on the question I should be doing in regards to iambic pentameters, but since it's long I'll try to simplify it. So I need to get some words and their stress patterns from a text file that looks something like this:

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

      

And from the file, you can guess that there will be many words for different sentences. I'm trying to get sentences from a text file with multiple sentences and see if the sentence (ignoring punctuation and case) is iambic pentameter.

For example, if the text file contains the following:

If music be the food of love play on
hello world

      

The first sentence will be assigned from the stress dictionary as follows: 0101010101

and the second is obviously not a pentameter ( 011

). I would like it to only print sentences that are iambic pentameters.

Sorry if this is a confusing or messy question. This is what I have so far:

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


source to share


3 answers


I don't think you are building your stress dictionary correctly. It is imperative to remember to get rid of the implicit character \n

from the lines as you read them, and also remove any spaces from words after you have separated the comma. Be that as it may, the line if, 0

will split into ['if', ' 0\n']

, which is not what you want.

So, to build your stress dictionary, you can do something like this:

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

      

For actual verification, the answer by @khelwood is a good way to go, but I would take care of handling the character \n

as you read the strings, and also make sure that all characters in the string were lowercase (e.g. in your dictionary).



Define a function is_iambic_pentameter

to check if a sentence is iambic pentameter (returns True

/ False

) and then checks each row in 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

      

As an aside, you might be interested in NLTK , a natural language processing library for Python. Some internet searches have found that people wrote Haiku generators and other scripts to evaluate poetry forms using the library.

+2


source


I would not have thought the iambic pentameter was so clear: always some words end up tense or unstressed to match the rhythm. But anyway. Something like that:

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

      



By the way, it's generally a bad idea to name your dictionary "dict" since you are hiding the type dict

.

+1


source


This is what the complete code would look like:

#!/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='')

      

0


source







All Articles