Reputation: 23
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.
Upvotes: 2
Views: 1604
Reputation: 21007
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]
.
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
Upvotes: 1