Miscs (Interro 28)

This commit is contained in:
2026-05-14 09:02:09 +02:00
parent 0836d5809d
commit 7e7045293a
10 changed files with 281 additions and 161 deletions
+42 -1
View File
@@ -7,6 +7,46 @@ import argparse
if len(sys.argv) < 2:
sys.exit("Usage: python script.py <InputDir>")
def escape_latex_underscores(text):
r"""
Escape '_' outside LaTeX math environments.
Supports:
- $...$
- $$...$$
- \( ... \)
- \[ ... \]
"""
# Regex matching LaTeX math blocks
math_pattern = re.compile(
r'(\$\$.*?\$\$|' # $$...$$
r'\$.*?\$|' # $...$
r'\\\(.*?\\\)|' # \( ... \)
r'\\\[.*?\\\])', # \[ ... \]
re.DOTALL
)
parts = []
last_end = 0
for match in math_pattern.finditer(text):
start, end = match.span()
# Escape underscores outside math
outside = text[last_end:start].replace('_', r'\_')
parts.append(outside)
# Keep math block unchanged
parts.append(match.group(0))
last_end = end
# Remaining text after last math block
outside = text[last_end:].replace('_', r'\_')
parts.append(outside)
return ''.join(parts)
arg_path = Path(sys.argv[1])
tasks = [] # List of tuples: (filepath_str, label_str)
results = {}
@@ -79,7 +119,8 @@ def clean_string(s: str) -> str:
if '\x00' in s:
s = fast_fix(s)
s = s.replace('\x00', '')
return some_other_replacements(s)
s = some_other_replacements(s)
return escape_latex_underscores(s)
def clean_obj(obj):