Miscs (Interro 28)
This commit is contained in:
+42
-1
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user