INNER CODE UNIT · Python
html_to_text
cognitivetech/ollama-ebook-summary · book2text.py:106
def html_to_text(html_path):
with open(html_path, 'r', encoding='utf-8') as file:
soup = BeautifulSoup(file, 'html.parser')
return soup.get_text()
def pdf_to_text(pdf_path):
reader = PdfReader(pdf_path)
text = []
for page in reader.pages:
text.append(page.extract_text())
return '\n'.join(text)
def natural_sort_key(s):
"""
This function constructs a tuple of either integers (if the pattern matches digits)
or the original elements (if not). This tuple can be used as a key for sorting.
"""
return [int(text) if text.isdigit() else text.lower() for text in re.split('(\d+)', s)]