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 #38 (common.py should remove control codes) and Fixed #39 (common.py should remove byte order marks) #41

Merged
merged 1 commit into from
Dec 29, 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
22 changes: 22 additions & 0 deletions src/corporacreator/preprocessors/common.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import unicodedata

from urllib.parse import unquote
from html.parser import HTMLParser

Expand Down Expand Up @@ -39,6 +41,24 @@ def _strip_tags(html):
return s.get_data()


def _strip_string(sentence):
"""Cleans a string based on a whitelist of printable unicode categories.

You can find a full list of categories here:
http://www.fileformat.info/info/unicode/category/index.htm
"""
letters = ('LC', 'Ll', 'Lm', 'Lo', 'Lt', 'Lu')
numbers = ('Nd', 'Nl', 'No')
marks = ('Mc', 'Me', 'Mn')
punctuation = ('Pc', 'Pd', 'Pe', 'Pf', 'Pi', 'Po', 'Ps')
symbol = ('Sc', 'Sk', 'Sm', 'So')
space = ('Zs',)

allowed_categories = letters + numbers + marks + punctuation + symbol + space

return u''.join([c for c in sentence if unicodedata.category(c) in allowed_categories])


def common(sentence):
"""Cleans up the passed sentence in a language independent manner, removing or reformatting invalid data.

Expand All @@ -53,5 +73,7 @@ def common(sentence):
sentence = unquote(sentence)
# Remove any HTML tags
sentence = _strip_tags(sentence)
# Remove non-printable characters
sentence = _strip_string(sentence)
# TODO: Clean up data in a language independent manner
return sentence