|
import re |
|
import ftfy |
|
|
|
|
|
def fix_utf8_encoding(text): |
|
if text is None: |
|
return "" |
|
return ftfy.fix_text(text) |
|
|
|
|
|
|
|
|
|
whitespace = {" ", "β", "β", "β―", "β
", "γ", "β", " ", "β", "β", "οΏΌ", "Β"} |
|
|
|
|
|
def normalize_whitespace(text): |
|
chars = [char if char not in whitespace else " " for char in text] |
|
text = "".join(chars) |
|
return text |
|
|
|
|
|
unicode_punctuation = { |
|
"οΌ": ",", |
|
"γ": ".", |
|
"γ": ",", |
|
"β": '"', |
|
"β": '"', |
|
"β": '"', |
|
"Β«": '"', |
|
"Β»": '"', |
|
"οΌ": '"', |
|
"γ": '"', |
|
"γ": '"', |
|
"γ": '"', |
|
"γ": '"', |
|
"Β΄": "'", |
|
"βΆ": ":", |
|
"οΌ": ":", |
|
"οΌ": "?", |
|
"οΌ": "!", |
|
"οΌ": "(", |
|
"οΌ": ")", |
|
"οΌ": ";", |
|
"β": "-", |
|
"β": " - ", |
|
"οΌ": ". ", |
|
"ο½": "~", |
|
"β": "'", |
|
"β¦": "...", |
|
"β": "-", |
|
"γ": "<", |
|
"γ": ">", |
|
"γ": "[", |
|
"γ": "]", |
|
"οΌ
": "%", |
|
"βΊ": "-", |
|
} |
|
|
|
|
|
def normalize_punctuation(text): |
|
chars = [unicode_punctuation.get(char, char) for char in text] |
|
text = "".join(chars) |
|
return text |
|
|
|
|
|
def remove_empty_lines(text): |
|
lines = text.splitlines() |
|
func = lambda x: not re.match(r'^\s*$', x) |
|
filtered = filter(func, lines) |
|
text = "\n".join(filtered) |
|
if text is None or isinstance(text, str): |
|
text = "" |
|
return text |
|
|
|
|
|
def clean_new_lines(text): |
|
text = text.strip() |
|
text = text.replace("\n", "") |
|
return text |
|
|