2
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

So for the body this is what I've written so far. I have a list of words built from phonemes (['N', 'EH1', 'K', 'S', 'T'] for the word next) and I would like to check how many digits are there (1 in EH1 makes it 1 for word `next) but I get back a 0. I've tried moving my code around to different indentation but I'm not sure how to proceed from there.

  • 1
    So what is the problem? What is your code (not) doing and should? Can you provide example input, output and expected output? – Vyktor Nov 24 '14 at 18:54
  • i'm trying to count how many syllables are in each line of poem_lines then seeings if its match the pattern's list in this example [5,5,4] and i'm suppose to return the list of lines that don't have the right number of syllables. In the word_to_phonemes it sounds out the word for us, so the ones with numbers at the end are syllables so thats what i'm counting. the example is below there – xxxxxxx3333 Nov 24 '14 at 18:58
  • so i guess to clarify, if i have the line 'The first line leads off,' it has 5 syllables since THE': ['DH', 'AH0'] has a string with the number 0 so 1 syllables and'FIRST': ['F', 'ER1', 'S', 'T'] has one string with a number so 1 syllables etc so 5 in total – xxxxxxx3333 Nov 24 '14 at 19:04
  • Just note for future questions here. That example in your last comment should be a part of question, describing that you want to split line into words and then map syllables and then count those containing a digit. [Please read How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve), because I (personally) had to re-read question about 10 times until I understood what are you trying to achieve. – Vyktor Nov 24 '14 at 19:31

1 Answers1

1

If I understand correctly what are you asking you want to transform line like this 'The first line leads off' to list like this:

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

And count number of elements that contain number (5 in provided example - AH0, ER1, AY1, IY1, AO1).

What you were doing was building a string like:

'DH'
'DHAH0'
'DHAH0F'
>>> 'DHAH0FER1'.isdigit()
False

You need to count digits in string:

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

>>> number_count('a1b2')
2

And use it in your code (you don't have to build string, you can count digits on the fly):

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

Or do it a bit more 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

Or if you want to keep your code (and build string first, before returning):

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

They all should return list (or generator) returning [5,5,4].


Listing only records that doesn't match

I'm not sure what pattern[1] is supposed to mean, but lets assume that for every line you want to work with line[n], pattern[0][n], pattern[1][n], the easiest way to do this is using 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
Community
  • 1
  • 1
Vyktor
  • 20,559
  • 6
  • 64
  • 96
  • i used your approach and continue the code with this since i need to generate a list with the lines whose total syllables do not match the pattern so after `k = 0 new_list = [] while k < len(lst): if lst[k] != pattern[0][k]: new_list.append[poem_lines[k]] else: return new_list` but i get back a empty set when i should get back those two lines in the example i think i made a mistake where the loop only iterated once so i'm not sure how to continue the loop to check the other items in the list – xxxxxxx3333 Nov 24 '14 at 20:40
  • pattern[1] is irrevelant in this code but its more important in a different code i wrote, and sorry to ask i don't really use yield is yield equivalent to return – xxxxxxx3333 Nov 24 '14 at 20:53
  • @xxxxxxx3333 if you're serious with python you should read [this](http://stackoverflow.com/a/231855/1149736), but you can replace `yield line` with `lst=[]` `lst.append(line)` and `return lst` as shown in first example in my answer. – Vyktor Nov 24 '14 at 20:58