-
Notifications
You must be signed in to change notification settings - Fork 0
/
Split_MD.py
246 lines (219 loc) · 8.37 KB
/
Split_MD.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import re
from dataclasses import dataclass
from typing import List
import concurrent.futures
from tqdm import tqdm
@dataclass
class Block:
position: int
sub_position: int = 1
type: str = ""
content: str = ""
def split_markdown(content: str) -> List[Block]:
A = []
pattern = re.compile(
r"(?P<title>^#{1,6} .+?$)|"
r"(?P<table><table[\s\S]*?<\/table>)|"
r"(?P<block_formula>\$\$[\s\S]+?\$\$)|"
r'(?P<img><img src="[^"]+"\/?>)|'
r"(?P<link_img>!\[.*?\]\([^\)]+\))|"
r"(?P<link>\[[^\]]+\]\([^\)]+\))",
re.MULTILINE,
)
pos = 0
for match in pattern.finditer(content):
start, end = match.span()
if start > pos:
text = content[pos:start].strip()
if text:
A.append(Block(position=len(A) + 1, type="text", content=text))
if match.group("title"):
A.append(
Block(position=len(A) + 1, type="title", content=match.group("title"))
)
elif match.group("table"):
A.append(
Block(position=len(A) + 1, type="table", content=match.group("table"))
)
elif match.group("block_formula"):
A.append(
Block(
position=len(A) + 1,
type="block_formula",
content=match.group("block_formula"),
)
)
elif match.group("img"):
A.append(
Block(position=len(A) + 1, type="image", content=match.group("img"))
)
elif match.group("link_img"):
A.append(
Block(
position=len(A) + 1,
type="link_image",
content=match.group("link_img"),
)
)
elif match.group("link"):
A.append(
Block(position=len(A) + 1, type="link", content=match.group("link"))
)
pos = end
if pos < len(content):
text = content[pos:].strip()
if text:
A.append(Block(position=len(A) + 1, type="text", content=text))
return A
def split_text_blocks(A: List[Block]) -> List[Block]:
end_punctuations = re.compile(r"[。?!.!?;;]")
new_blocks = []
for block in A:
if block.type == "text":
text = block.content
start = 0
while start < len(text):
end = start + 512
if end >= len(text):
end = len(text)
else:
match = end_punctuations.search(text, end)
if match:
end = match.end()
else:
end = min(start + 1024, len(text))
segment = text[start:end].strip()
if segment:
new_blocks.append(
Block(
position=block.position,
sub_position=len(new_blocks) + 1,
type="text",
content=segment,
)
)
start = end
else:
new_blocks.append(block)
return new_blocks
def replace_inline_formula(
text: str, placeholder_counter: int, placeholders: dict
) -> str:
inline_formula_pattern = re.compile(r"\$[^$]+\$")
def replacer(match):
nonlocal placeholder_counter
placeholder = f"⚛️{placeholder_counter}⚛️"
placeholders[placeholder] = match.group()
placeholder_counter += 1
return placeholder
return inline_formula_pattern.sub(replacer, text)
def concurrent_translate(
A: List[Block], translate: callable, thread: int
) -> List[Block]:
placeholders = {}
placeholder_counter = 1
def process_block(block: Block):
nonlocal placeholder_counter
if block.type in ["table", "block_formula", "image", "link_image", "link"]:
return block
elif block.type == "title":
content = block.content.lstrip("#").strip()
translated = translate(content, "", "")
block.content = translated
return block
elif block.type == "text":
prev_block = None
next_block = None
if block.sub_position > 1:
for b in reversed(A[: A.index(block)]):
if (
b.position == block.position
and b.sub_position == block.sub_position - 1
):
prev_block = b.content
break
else:
for b in reversed(A[: A.index(block)]):
if b.position == block.position - 1:
prev_block = b.content
break
for b in A[A.index(block) + 1 :]:
if (
b.position == block.position
and b.sub_position == block.sub_position + 1
):
next_block = b.content
break
elif b.position == block.position + 1:
next_block = b.content
break
translated_content = replace_inline_formula(
block.content, placeholder_counter, placeholders
)
placeholder_counter += len(placeholders)
translated = translate(
translated_content, prev_block or "", next_block or ""
)
for placeholder, formula in placeholders.items():
# Remove spaces between $ and formula content
formula = re.sub(r"\$\s*(.*?)\s*\$", r"$\1$", formula)
translated = translated.replace(placeholder, f" {formula} ")
if "⚛️" in translated:
sentences = re.split(r"(?<=[。?!.!?;;])", block.content)
translated_sentences = [translate(s, "", "") for s in sentences]
translated = "".join(translated_sentences)
block.content = translated
return block
return block
total_blocks = len(A)
with concurrent.futures.ThreadPoolExecutor(max_workers=thread) as executor:
A = list(
tqdm(
executor.map(process_block, A),
total=total_blocks,
desc="Translating blocks",
unit="block",
)
)
return A
def combine_blocks(A: List[Block]) -> str:
combined = []
special_types = ["table", "block_formula", "image", "link_image", "link"]
for i, block in enumerate(A):
# Add newline before special blocks
if block.type in special_types and (
i == 0 or A[i - 1].type not in special_types
):
combined.append("\n")
combined.append(block.content)
# Add newline after special blocks
if block.type in special_types and (
i == len(A) - 1 or A[i + 1].type not in special_types
):
combined.append("\n")
return "".join(combined)
def process_markdown(input_markdown: str, translate: callable, thread: int = 10) -> str:
# Preprocess markdown content
pattern1 = re.compile(
r"\\begin{center}\s*\\adjustbox{max width=\\textwidth}{\s*(.*?)\s*\\end{tabular}\s*}\s*\\end{center}",
re.DOTALL,
)
replacement1 = r"\\begin{center}\n\1\n\\end{tabular}\n\\end{center}"
input_markdown = re.sub(pattern1, replacement1, input_markdown)
pattern2 = re.compile(r"\\tag\{(.*?)\}")
replacement2 = r"\\qquad \\text{(\1)}"
input_markdown = re.sub(pattern2, replacement2, input_markdown)
# Remove media and footnote comments
input_markdown = re.sub(r"<!-- Media -->\n?", "", input_markdown)
input_markdown = re.sub(r"<!-- Footnote -->\n?", "", input_markdown)
# Replace \( \) with $ and \[ \] with $$ for math expressions
input_markdown = re.sub(r"\\[()]", "$", input_markdown)
input_markdown = re.sub(r"\\[\[\]]", "$$", input_markdown)
# Replace $$$$ with $$\n$$ for better readability
input_markdown = re.sub(r"\$\$\$\$", "$$\n$$", input_markdown)
# Process blocks
blocks = split_markdown(input_markdown)
blocks = split_text_blocks(blocks)
blocks = concurrent_translate(A=blocks, translate=translate, thread=thread)
output_markdown = combine_blocks(blocks)
return output_markdown