Skip to content
Snippets Groups Projects
Commit 17b94014 authored by Simon Will's avatar Simon Will
Browse files

Add methods for filtering on meters to corpus.py

parent 426c377f
No related branches found
No related tags found
No related merge requests found
......@@ -91,6 +91,18 @@ class HypotacticDocument:
if all(fil(line) for fil in line_filters)
)
def get_lines_with_meter(self, meters):
filters = [lambda tag: any((meter in tag.attrs['class'])
for meter in meters)]
if self.root.find(name='div', class_='poem'):
yield from (
line
for poem in self.get_poems(filters)
for line in poem.find_all(name='div', class_='line')
)
else:
self.get_lines(filters)
class HypotacticCorpus:
......@@ -121,12 +133,14 @@ class HypotacticCorpus:
)
def get_lines_with_meter(self, meters):
filters = [lambda line: any((meter in line.attrs['class'])
for meter in meters)]
yield from self.get_lines(filters)
yield from (
line
for doc in self.documents
for line in doc.get_lines_with_meter(meters)
)
def save_lines(self, file_handle, lines, title='Saved Poems',
base_html=BASE_HTML):
base_html=BASE_HTML, pretty=False):
soup = BeautifulSoup(base_html, self.parser)
title_tag = soup.new_tag('title')
......@@ -139,4 +153,8 @@ class HypotacticCorpus:
latin.append(line)
soup.find(name='body').append(latin)
file_handle.write(soup.prettify())
if pretty:
output = soup.prettify()
else:
output = str(soup)
file_handle.write(output)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment