File size: 3,441 Bytes
248c176 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
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):
# replace other urls by filler
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}
|