KaraKaraWitch commited on
Commit
ae1fbb3
1 Parent(s): 4a1b52e

Upload scripts/text_toolkit.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. scripts/text_toolkit.py +70 -0
scripts/text_toolkit.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Text_toolkit.
2
+
3
+ puncts_replacements = {
4
+ "“": '"',
5
+ "”": '"',
6
+ "’": "'",
7
+ "\n\n\n\n": "\n\n",
8
+ "\n\n\n": "\n\n",
9
+ "\xa0": " ",
10
+ "⁠—": "-",
11
+ ".⁠ ⁠…": "...",
12
+ "⁠…": "...",
13
+ " ": " ",
14
+ ", , ": ", ",
15
+ ". ": ". ",
16
+ ", ": ", ",
17
+ "; ": "; ",
18
+ }
19
+
20
+
21
+ def normalize_puncts(html: str):
22
+ strp = html.strip()
23
+ for k, v in puncts_replacements.items():
24
+ strp = strp.replace(k, v)
25
+ return strp
26
+
27
+ def merge_consecutive_lines(lines:str):
28
+ last_line = None
29
+ final_concate = []
30
+ for curr_line in lines.split("\n"):
31
+ if last_line is None:
32
+ last_line = curr_line.lstrip()
33
+ final_concate.append(curr_line)
34
+ continue
35
+ if curr_line.strip():
36
+ # current line not empty
37
+ if last_line:
38
+ # Last line has content
39
+ final_concate[-1] = f"{final_concate[-1]} {curr_line}"
40
+ last_line = curr_line.lstrip()
41
+ # Current line empty
42
+ else:
43
+ last_line = curr_line.lstrip()
44
+ final_concate.append("\n")
45
+ else:
46
+ # currenty line is empty
47
+ if last_line:
48
+ final_concate.append(curr_line + "\n")
49
+ else:
50
+ continue
51
+ return "\n".join(final_concate)
52
+
53
+ def clean_newlines(lines: str):
54
+ last_line = None
55
+ final_concate = []
56
+ for line in lines.split("\n"):
57
+ if last_line is None:
58
+ last_line = line.lstrip()
59
+ final_concate.append(line.lstrip())
60
+ continue
61
+ if line.strip():
62
+ last_line = line.lstrip()
63
+ final_concate.append(line.lstrip())
64
+ else:
65
+ if last_line:
66
+ last_line = line.lstrip()
67
+ final_concate.append(line.lstrip())
68
+ else:
69
+ continue
70
+ return "\n".join(final_concate)