Recursalberg / scripts /text_toolkit.py
KaraKaraWitch's picture
Upload scripts/text_toolkit.py with huggingface_hub
ae1fbb3 verified
raw
history blame
1.9 kB
# Text_toolkit.
puncts_replacements = {
"β€œ": '"',
"”": '"',
"’": "'",
"\n\n\n\n": "\n\n",
"\n\n\n": "\n\n",
"\xa0": " ",
"⁠—": "-",
".β β€Šβ β€¦": "...",
"⁠…": "...",
" ": " ",
", , ": ", ",
". ": ". ",
", ": ", ",
"; ": "; ",
}
def normalize_puncts(html: str):
strp = html.strip()
for k, v in puncts_replacements.items():
strp = strp.replace(k, v)
return strp
def merge_consecutive_lines(lines:str):
last_line = None
final_concate = []
for curr_line in lines.split("\n"):
if last_line is None:
last_line = curr_line.lstrip()
final_concate.append(curr_line)
continue
if curr_line.strip():
# current line not empty
if last_line:
# Last line has content
final_concate[-1] = f"{final_concate[-1]} {curr_line}"
last_line = curr_line.lstrip()
# Current line empty
else:
last_line = curr_line.lstrip()
final_concate.append("\n")
else:
# currenty line is empty
if last_line:
final_concate.append(curr_line + "\n")
else:
continue
return "\n".join(final_concate)
def clean_newlines(lines: str):
last_line = None
final_concate = []
for line in lines.split("\n"):
if last_line is None:
last_line = line.lstrip()
final_concate.append(line.lstrip())
continue
if line.strip():
last_line = line.lstrip()
final_concate.append(line.lstrip())
else:
if last_line:
last_line = line.lstrip()
final_concate.append(line.lstrip())
else:
continue
return "\n".join(final_concate)