Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #18 (Some sentences contain HTML). #19

Merged
merged 1 commit into from
Dec 13, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/corporacreator/preprocessors/common.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,39 @@
from html.parser import HTMLParser

class _HTMLStripper(HTMLParser):
"""Class that strips HTML from strings.
Examples:
>>> stripper = _HTMLStripper()
>>> stripper.feed(html)
>>> nohtml = stripper.get_data()
"""
def __init__(self):
super().__init__()
self.reset()
self.strict = False
self.convert_charrefs= True
self.fed = []

def handle_data(self, d):
self.fed.append(d)

def get_data(self):
return ''.join(self.fed)

def _strip_tags(html):
"""Removes HTML tags from passed text.
Args:
html (str): String containing HTML
Returns:
(str): String with HTML removed
"""
s = _HTMLStripper()
s.feed(html)
return s.get_data()

def common(sentence):
"""Cleans up the passed sentence in a language independent manner, removing or reformatting invalid data.
Expand All @@ -7,5 +43,7 @@ def common(sentence):
Returns:
(str): Cleaned up sentence.
"""
# Remove any HTML tags
sentence = _strip_tags(sentence)
# TODO: Clean up data in a language independent manner
return sentence