-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_processing.py
159 lines (128 loc) · 5.11 KB
/
text_processing.py
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import re
from markdown import Markdown
from io import StringIO
import unidecode
def unmark_element(element, stream=None):
if stream is None:
stream = StringIO()
if element.text:
stream.write(element.text)
for sub in element:
unmark_element(sub, stream)
if element.tail:
stream.write(element.tail)
return stream.getvalue()
# patching Markdown
Markdown.output_formats["plain"] = unmark_element # type: ignore
__md = Markdown(output_format="plain") # type: ignore
__md.stripTopLevelTags = False # type: ignore
def remove_markdown(text):
return __md.convert(text)
def split_text_to_max_x_chars(text: str, x: int) -> list[str]:
if len(text) > x:
first_part = text[0:x]
separate_by = " "
for sep in [".", ";", ","]:
if sep in first_part:
separate_by = sep
break
period_index = first_part.rindex(separate_by)
first_part = text[0 : period_index + 1]
return [first_part] + split_text_to_max_x_chars(text[period_index + 1 :], x)
else:
return [text]
def text_cleanup(text: str) -> str:
text = remove_markdown(text)
to_remove_paragraph_if_starts_with = [
"edit",
"edit:",
"tldr",
"tl;dr",
"tl:dr",
"tl,dr",
"update:",
"disclaimer",
]
for paragraph in text.split("\n"):
for phrase in to_remove_paragraph_if_starts_with:
if paragraph.lower().startswith(phrase):
# print(f"replacing paragraph starting with {phrase}")
text = text.replace(paragraph, "")
to_remove_paragraph_if_included = [
"throwaway",
"repost",
"made this account",
"is the right subreddit",
"spelling",
"formatting",
"first time posting",
"first post",
"in r/",
]
for phrase in to_remove_paragraph_if_included:
if phrase in text.lower()[: int(len(text) * 0.3)]:
edit_position = 0
if "\n" in text[:edit_position]:
edit_position = text[:edit_position].rindex("\n")
text = text.replace(text[edit_position : text.index("\n")], "")
for match in re.findall("[a-zA-Z]+\n[a-zA-Z]+", text):
replace_with = ". ".join(match.split("\n"))
text = text.replace(match, replace_with)
# print(f"replacing {match} with {replace_with}")
text = " ".join(text.split())
text = text.replace(" , ", ", ")
text = text.replace(" . ", ". ")
text = text.replace("“", '"')
text = unidecode.unidecode(text)
for match in re.findall('[a-zA-Z]+"[a-zA-Z]+', text):
replace_with = " ".join(match.split('"'))
text = text.replace(match, replace_with)
# print(f"replacing {match} with {replace_with}")
for match in re.findall("^--+[\s]", text):
text = text.replace(match, "")
# print(f"replacing {match} with {replace_with}")
for match in re.findall("^__+[\s]", text):
text = text.replace(match, "")
# print(f"replacing {match} with {replace_with}")
for match in re.findall("[a-zA-Z]\(", text):
replace_with = " (".join(match.split("("))
text = text.replace(match, replace_with)
# print(f"replacing {match} with {replace_with}")
for match in re.findall("\)[a-zA-Z]", text):
replace_with = ") ".join(match.split(")"))
text = text.replace(match, replace_with)
# print(f"replacing {match} with {replace_with}")
for match in re.findall("[a-zA-Z],[a-zA-Z]", text):
replace_with = ", ".join(match.split(","))
text = text.replace(match, replace_with)
# print(f"replacing {match} with {replace_with}")
for match in re.findall("[a-zA-Z] - [a-zA-Z]", text):
replace_with = ", ".join(match.split(" - "))
text = text.replace(match, replace_with)
# print(f"replacing {match} with {replace_with}")
for match in re.findall("[a-zA-Z] ,[a-zA-Z]", text):
replace_with = ", ".join(match.split(" ,"))
text = text.replace(match, replace_with)
# print(f"replacing {match} with {replace_with}")
for match in re.findall("\.\.\.\.+", text):
replace_with = "..."
text = text.replace(match, replace_with)
# print(f"replacing {match} with {replace_with}")
for match in re.findall("[a-zA-Z]\.\.\.[a-zA-Z]", text):
replace_with = "... ".join(match.split("..."))
text = text.replace(match, replace_with)
# print(f"replacing {match} with {replace_with}")
# markdown links
for match in re.findall("(\[([^\]]+)\]\((\S+(?=\)))\))", text):
replace_with = match[1]
# print(f"replacing {match[0]} with {replace_with}")
text = text.replace(match[0], match[1])
with open("config/replace_in_text.txt") as file:
for line in file.readlines():
line = line.strip("\n")
replace_from, replace_to = line.split(",")
# print(f"replacing \"{replace_from}\" with \"{replace_to}\"")
text = text.replace(replace_from, replace_to)
# print(self.post_id)
# print(text)
return text