|
import re |
|
import unicodedata |
|
from functools import reduce |
|
from itertools import groupby |
|
from string import punctuation |
|
|
|
url_regex = re.compile(r"((www\.[^\s]+)|(https?://[^\s]+)|(http?://[^\s]+))") |
|
control_char_regex = re.compile(r"[\r\n\t]+") |
|
phone_number_regex = re.compile("\(?\+?\d[\s\d()-]{5,}\d") |
|
finnish_social_id_regex = re.compile("\d{6}[-+Aa]\d{3}[a-zA-Z0-9]") |
|
email_regex = re.compile(r"[\w.+-]+@[\w-]+\.[\w.-]+") |
|
|
|
|
|
def clean_text(example) -> str: |
|
data_preprocessing_funcs = [ |
|
_replace_identity, |
|
_replace_phone_number, |
|
_replace_url, |
|
_replace_email, |
|
_fix_html, |
|
_standardise_punc, |
|
_remove_unicode_symbols, |
|
_remove_control_char, |
|
_remove_remaining_control_chars, |
|
_remove_multiple_punctuation, |
|
_remove_multi_space, |
|
] |
|
return reduce(lambda x, y: y(x["text"]), data_preprocessing_funcs, example) |
|
|
|
|
|
def _standardise_punc(text: str) -> str: |
|
transl_table = dict([(ord(x), ord(y)) for x, y in zip("‘’´“”–-", "'''\"\"--")]) |
|
text_mod = text.translate(transl_table) |
|
text_mod = re.sub(r"[^a-zA-Z0-9À-ÿ .,'%&€$=@+;<>/()!?%:-]", " ", text_mod) |
|
return {"text": text_mod} |
|
|
|
|
|
def _remove_control_char(text: str) -> str: |
|
text_mod = re.sub(control_char_regex, " ", text) |
|
return {"text": text_mod} |
|
|
|
|
|
def _remove_remaining_control_chars(text: str) -> str: |
|
text_mod = "".join(ch for ch in text if unicodedata.category(ch)[0] != "C") |
|
return {"text": text_mod} |
|
|
|
|
|
def _remove_multi_space(text: str) -> str: |
|
text_mod = " ".join([txt for txt in text.split(" ") if txt and txt != ""]) |
|
return {"text": text_mod} |
|
|
|
|
|
def _remove_unicode_symbols(text: str) -> str: |
|
text_mod = "".join(ch for ch in text if unicodedata.category(ch)[0:2] != "So") |
|
return {"text": text_mod} |
|
|
|
|
|
def _replace_url(text: str) -> str: |
|
filler = "" |
|
occ = text.count("www.") + text.count("http:") + text.count("https:") |
|
text_mod = text |
|
for _ in range(occ): |
|
|
|
text_mod = re.sub(url_regex, filler, text_mod) |
|
text_mod = " ".join(text_mod.split()) |
|
return {"text": text_mod} |
|
|
|
|
|
def _fix_html(text: str) -> str: |
|
text_mod = ( |
|
text.replace("#39;", "'") |
|
.replace("amp;", "&") |
|
.replace("#146;", "'") |
|
.replace("nbsp;", " ") |
|
.replace("\\n", "\n") |
|
.replace("quot;", "'") |
|
.replace("<br />", "\n") |
|
.replace('\\"', '"') |
|
.replace(" @.@ ", ".") |
|
.replace(" @-@ ", "-") |
|
.replace("...", " …") |
|
) |
|
return {"text": text_mod} |
|
|
|
|
|
def _replace_phone_number(text: str) -> str: |
|
text_mod = re.sub(phone_number_regex, " ", text) |
|
return {"text": text_mod} |
|
|
|
|
|
def _replace_identity(text: str) -> str: |
|
text_mod = re.sub(finnish_social_id_regex, " ", text) |
|
return {"text": text_mod} |
|
|
|
|
|
def _replace_email(text: str) -> str: |
|
text_mod = re.sub(email_regex, " ", text) |
|
return {"text": text_mod} |
|
|
|
|
|
def _remove_news_tags(text: str) -> str: |
|
text_mod = re.sub(r"(<[A-Z].+?>)|(</[A-Z].+?>)", " ", text) |
|
return {"text": text_mod} |
|
|
|
|
|
def _remove_multiple_punctuation(text: str) -> str: |
|
text_mod = re.sub(r"\b(\w+)( \1\b)+", r"\1", text) |
|
punc = set(punctuation) |
|
newtext = [] |
|
for k, g in groupby(text_mod): |
|
if k in punc: |
|
newtext.append(k) |
|
else: |
|
newtext.extend(g) |
|
text_mod = "".join(newtext) |
|
return {"text": text_mod} |
|
|