Spaces:
Build error
Build error
File size: 23,457 Bytes
908351f |
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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 |
import sys
import regex
import yaml
import shutil
import bibtexparser
from charset_normalizer import from_path
from langdetect import detect
import os
import subprocess
import numpy as np
import networkx as nx
import re
def is_venv():
return (hasattr(sys, 'real_prefix') or
(hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix))
def read_yaml_file(file_path):
with open(file_path, 'r') as file:
try:
data = yaml.safe_load(file)
return data
except yaml.YAMLError as e:
print(f"Error reading YAML file: {e}")
def read_tex_file(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
tex_content = file.read()
return tex_content
def write_tex_file(file_path, s):
with open(file_path, 'w', encoding='utf-8') as file:
file.write(s)
def get_core(s):
start = '\\begin{document}'
end = '\\end{document}'
beginning_doc = s.find(start)
end_doc = s.rfind(end)
return s[beginning_doc+len(start):end_doc]
def retrieve_text(text, command, keep_text=False):
"""Removes '\\command{*}' from the string 'text'.
Regex `base_pattern` used to match balanced parentheses taken from:
https://stackoverflow.com/questions/546433/regular-expression-to-match-balanced-parentheses/35271017#35271017
"""
base_pattern = (
r'\\' + command + r"(?:\[(?:.*?)\])*\{((?:[^{}]+|\{(?1)\})*)\}(?:\[(?:.*?)\])*"
)
def extract_text_inside_curly_braces(text):
"""Extract text inside of {} from command string"""
pattern = r"\{((?:[^{}]|(?R))*)\}"
match = regex.search(pattern, text)
if match:
return match.group(1)
else:
return ""
# Loops in case of nested commands that need to retain text, e.g. \red{hello \red{world}}.
while True:
all_substitutions = []
has_match = False
for match in regex.finditer(base_pattern, text):
# In case there are only spaces or nothing up to the following newline,
# adds a percent, not to alter the newlines.
has_match = True
if not keep_text:
new_substring = ""
else:
temp_substring = text[match.span()[0] : match.span()[1]]
return extract_text_inside_curly_braces(temp_substring)
if match.span()[1] < len(text):
next_newline = text[match.span()[1] :].find("\n")
if next_newline != -1:
text_until_newline = text[
match.span()[1] : match.span()[1] + next_newline
]
if (
not text_until_newline or text_until_newline.isspace()
) and not keep_text:
new_substring = "%"
all_substitutions.append((match.span()[0], match.span()[1], new_substring))
for start, end, new_substring in reversed(all_substitutions):
text = text[:start] + new_substring + text[end:]
if not keep_text or not has_match:
break
def reduce_linebreaks(s):
return re.sub(r'(\n[ \t]*)+(\n[ \t]*)+', '\n\n', s)
def replace_percentage(s):
return re.sub(r'% *\n', '\n', s)
def reduce_spaces(s):
return re.sub(' +', ' ', s)
def delete_urls(s):
return re.sub(r'http\S+', '', s)
def remove_tilde(s):
s1 = re.sub(r'[~ ]\.', '.', s)
s2 = re.sub(r'[~ ],', ',', s1)
return re.sub(r'{}', '', s2)
def remove_verbatim_words(s):
with open("configs/latex_commands.yaml", "r") as stream:
read_config = yaml.safe_load(stream)
for command in read_config['verbatim_to_delete']:
s = s.replace(command, '')
for command in read_config['two_arguments']:
pattern = r'\\' + command + r'{[^}]*}' + r'{[^}]*}'
s = re.sub(pattern, '', s)
for command in read_config['three_arguments']:
pattern = r'\\' + command + r'{[^}]*}' + r'{[^}]*}' + r'{[^}]*}'
s = re.sub(pattern, '', s)
for command in read_config['two_arguments_elaborate']:
s = remove_multargument(s, '\\' + command, 2)
for command in read_config['three_arguments_elaborate']:
s = remove_multargument(s, '\\' + command, 3)
for command in read_config['replace_comments']:
pattern = r'\\' + command
s = re.sub(pattern, '%', s)
s = re.sub(
r'\\end{[\s]*abstract[\s]*}',
'',
s,
flags=re.IGNORECASE
)
s = re.sub(
r'\\begin{[\s]*abstract[\s]*}',
'Abstract\n\n',
s,
flags=re.IGNORECASE
)
return s
def yes_or_no(s):
return 1 if "Yes" == s[0:3] else 0 if "No" == s[0:2] else -1
def get_main(directory):
file_paths = []
for root, _, files in os.walk(directory):
for file in files:
file_path = os.path.join(root, file)
file_paths.append(file_path)
latex_paths = [f for f in file_paths if f.endswith('.tex')]
number_tex = len(latex_paths)
if number_tex == 0:
return None
if number_tex == 1:
return latex_paths[0]
adjacency = np.zeros((number_tex, number_tex))
keys = [os.path.basename(path) for path in latex_paths]
reg_ex = r'\\input{(.*?)}|\\include{(.*?)}|\\import{(.*?)}|\\subfile{(.*?)}|\\include[*]{(.*?)}|}'
for i,file in enumerate(latex_paths):
content = read_tex_file(file)
find_pattern_input = re.findall(reg_ex, content)
find_pattern_input = [tup for tup in find_pattern_input if not all(element == "" for element in tup)]
number_matches = len(find_pattern_input)
if number_matches == 0:
continue
else:
content = replace_imports(file, content)
reg_ex_clean = r'\\input{(.*?)}|\\include{(.*?)}'
find_pattern_input = re.findall(reg_ex_clean, content)
number_matches = len(find_pattern_input)
for j in range(number_matches):
match = find_pattern_input[j]
non_empty_match = [t for t in match if t]
for non_empty in non_empty_match:
base_match = os.path.basename(non_empty)
if not base_match.endswith('.tex'):
base_match = base_match + '.tex'
if base_match not in keys:
continue
ind = keys.index(base_match)
adjacency[i][ind] = 1
G = nx.from_numpy_array(adjacency, create_using=nx.DiGraph)
connected_components = list(nx.weakly_connected_components(G))
size_connected = [len(x) for x in connected_components]
maximum_size = max(size_connected)
biggest_connected = [x for x in connected_components if len(x) == maximum_size]
if len(biggest_connected)>1:
roots = [n for connected in biggest_connected for n in connected if not list(G.predecessors(n))]
_check = []
for r in roots:
try:
_check.append(check_begin(latex_paths[r]))
except Exception as e:
_check.append(False)
potentials_files = [latex_paths[x] for x, y in zip(roots, _check) if y == True]
sizes_files = [os.path.getsize(x) for x in potentials_files]
return potentials_files[sizes_files.index(max(sizes_files))]
else:
roots = [n for n in biggest_connected[0] if not list(G.predecessors(n))]
return latex_paths[roots[0]]
def initial_clean(directory, config):
config_cmd = ''
if config == True:
config_cmd = '--config configs/cleaning_config.yaml'
temp_dir = directory[:directory.rfind('/')] + '_temp' + '/'
shutil.copytree(directory, temp_dir)
try:
command_res = os.system('arxiv_latex_cleaner --keep_bib {} {}'.format(directory, config_cmd))
if command_res != 0:
raise Exception('Error cleaning')
else:
shutil.rmtree(temp_dir)
except Exception as e:
shutil.rmtree(directory)
os.rename(temp_dir, directory)
file_paths = []
for root, _, files in os.walk(directory):
for file in files:
file_path = os.path.join(root, file)
file_paths.append(file_path)
latex_paths = [f for f in file_paths if f.endswith('.tex')]
for p in latex_paths:
results = from_path(p)
with open(p, 'w', encoding='utf-8') as f:
f.write(str(results.best()))
os.system('arxiv_latex_cleaner --keep_bib {} {}'.format(directory, config_cmd))
cleaned_directory = directory[:directory.rfind('/')] + '_arXiv'
shutil.rmtree(directory)
os.rename(cleaned_directory, directory)
def check_begin(directory):
content = read_tex_file(directory)
english = detect(content) == 'en'
return True and english if re.findall(r'\\begin{document}', content) else False
def post_processing(extracted_dir, file):
_dir = os.path.dirname(file) + '/'
perl_expand(file)
file = _dir + 'merged_latexpand.tex'
try:
de_macro(file)
file = _dir + 'merged_latexpand-clean.tex'
except Exception as e:
pass
try:
def_handle(file)
except Exception as e:
pass
try:
declare_operator(file) # has additional add-ons
except Exception as e:
pass
try:
de_macro(file)
file = _dir + os.path.splitext(os.path.basename(file))[0] + '-clean' + '.tex'
except Exception as e:
pass
initial_clean(_dir, config=True)
initial_clean(_dir, config=False)
tex_content = read_tex_file(file)
final_tex = reduce_spaces(
delete_urls(
remove_tilde(
reduce_linebreaks(
replace_percentage(
remove_verbatim_words(
tex_content
)
)
)
)
)
).strip()
shutil.rmtree(extracted_dir)
os.makedirs(extracted_dir)
write_tex_file(extracted_dir + 'final_cleaned.tex', final_tex)
initial_clean(extracted_dir, config=False)
return extracted_dir + 'final_cleaned.tex'
def perl_expand(file):
# Save the current working directory
oldpwd = os.getcwd()
target_dir = os.path.dirname(file) + '/'
# Correctly construct the path
target = os.path.join(target_dir, 'latexpand')
src = './src/utils/latexpand'
# Copy the `latexpand` script to the target directory
shutil.copyfile(src, target)
# Change to the target directory
os.chdir(target_dir)
# Run the perl command without shell=True and handle redirection within Python
with open('merged_latexpand.tex', 'w') as output_file:
subprocess.run(['perl', 'latexpand', os.path.basename(file)],
stdout=output_file, stderr=subprocess.DEVNULL)
# Return to the original directory
os.chdir(oldpwd)
def de_macro(file):
# Save the current working directory\
oldpwd = os.getcwd()
target_dir = os.path.dirname(file) + '/'
# Construct the target path
target = os.path.join(target_dir, 'de-macro.py')
src = '.src/utils/de-macro.py'
# Copy the `de-macro.py` script to the target directory
shutil.copyfile(src, target)
# Change to the target directory
os.chdir(target_dir)
# Run the de-macro script without os.system and capture errors
try:
subprocess.run(['python3', 'de-macro.py', os.path.basename(file)],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True)
except subprocess.CalledProcessError as e:
raise Exception(f"Error de-macro: {e}") from e
finally:
# Always return to the original directory
os.chdir(oldpwd)
def def_handle(file):
h = os.system('python3 src/utils/def_handle.py {} --output {}'.format(file, file))
if h != 0:
raise Exception('Error def handle')
def declare_operator(file):
s = read_tex_file(file)
## Operators
pattern = r'\\DeclareMathOperator'
s = re.sub(pattern, r'\\newcommand', s)
pattern = {
r'\\newcommand\*': r'\\newcommand',
r'\\providecommand\*': r'\\newcommand',
r'\\providecommand': r'\\newcommand',
r'\\renewcommand\*': r'\\renewcommand',
r'\\newenvironment\*': r'\\newenvironment',
r'\\renewenvironment\*': r'\\renewenvironment'
}
s = re.sub(r'\\end +', r'\\end', s)
for key in pattern:
s = re.sub(key, pattern[key], s)
## Title
start = '\\begin{document}'
beginning_doc = s.find(start)
pattern = {
r'\\icmltitlerunning\*': r'\\title',
r'\\icmltitlerunning': r'\\title',
r'\\inlinetitle\*': r'\\title',
r'\\icmltitle\*': r'\\title',
r'\\inlinetitle': r'\\title',
r'\\icmltitle': r'\\title',
r'\\titlerunning\*': r'\\title',
r'\\titlerunning': r'\\title',
r'\\toctitle': r'\\title',
r'\\title\*': r'\\title',
r'\\TITLE\*': r'\\title',
r'\\TITLE': r'\\title',
r'\\Title\*': r'\\title',
r'\\Title': r'\\title',
}
for key in pattern:
s = re.sub(key, pattern[key], s)
find_potential = s.find('\\title')
## Remove \\
title_content = retrieve_text(s, 'title', keep_text = True)
if title_content != None:
cleaned_title = re.sub(r'\\\\', ' ', title_content)
cleaned_title = re.sub(r'\n',' ', cleaned_title)
cleaned_title = re.sub(r'\~',' ', cleaned_title)
s = s.replace(title_content, cleaned_title)
if find_potential != -1 and find_potential < beginning_doc:
s = s.replace('\\maketitle', cleaned_title)
## Cite and ref commands
pattern = {
r'\\citep\*': r'\\cite',
r'\\citet\*': r'\\cite',
r'\\citep': r'\\cite',
r'\\citet': r'\\cite',
r'\\cite\*': r'\\cite',
r'\\citealt\*': r'\\cite',
r'\\citealt': r'\\cite',
r'\\citealtp\*': r'\\cite',
r'\\citealp': r'\\cite',
r'\\citeyear\*': r'\\cite',
r'\\citeyear': r'\\cite',
r'\\citeauthor\*': r'\\cite',
r'\\citeauthor': r'\\cite',
r'\\citenum\*': r'\\cite',
r'\\citenum': r'\\cite',
r'\\cref': r'\\ref',
r'\\Cref': r'\\ref',
r'\\factref': r'\\ref',
r'\\appref': r'\\ref',
r'\\thmref': r'\\ref',
r'\\secref': r'\\ref',
r'\\lemref': r'\\ref',
r'\\corref': r'\\ref',
r'\\eqref': r'\\ref',
r'\\autoref': r'\\ref',
r'begin{thm}': r'begin{theorem}',
r'begin{lem}': r'begin{lemma}',
r'begin{cor}': r'begin{corollary}',
r'begin{exm}': r'begin{example}',
r'begin{defi}': r'begin{definition}',
r'begin{rem}': r'begin{remark}',
r'begin{prop}': r'begin{proposition}',
r'end{thm}': r'end{theorem}',
r'end{lem}': r'end{lemma}',
r'end{cor}': r'end{corollary}',
r'end{exm}': r'end{example}',
r'end{defi}': r'end{definition}',
r'end{rem}': r'end{remark}',
r'end{prop}': r'end{proposition}',
}
for key in pattern:
s = re.sub(key, pattern[key], s)
pattern = {
r'subsubsection': r'section',
r'subsubsection ': r'section',
r'subsubsection\*': r'section',
r'subsubsection\* ': r'section',
r'subsection': r'section',
r'subsection ': r'section',
r'subsection\*': r'section',
r'subsection\* ': r'section',
r'section ': r'section',
r'section\*': r'section',
r'section\* ': r'section',
r'chapter': r'section',
r'chapter ': r'section',
r'chapter\*': r'section',
r'chapter\* ': r'section',
r'mysubsubsection': r'section',
r'mysubsection': r'section',
r'mysection': r'section',
}
for key in pattern:
s = re.sub(key, pattern[key], s)
# In case any new commands for appendix/appendices
s = re.sub(r'newcommand{\\appendix}', '', s)
s = re.sub(r'newcommand{\\appendices}', '', s)
s = get_core(s)
## In case of double titles being defined
title_content = retrieve_text(s, 'title', keep_text = True)
if title_content != None:
cleaned_title = re.sub(r'\\\\', ' ', title_content)
cleaned_title = re.sub(r'\n',' ', cleaned_title)
cleaned_title = re.sub(r'\~',' ', cleaned_title)
s = s.replace(title_content, cleaned_title)
write_tex_file(file, s)
def replace_imports(file, s):
regex_p1 = r'\\import{(.*?)}{(.*?)}'
s = re.sub(regex_p1, r"\\input{\1\2}", s)
regex_p2 = r'\\subfile{(.*?)}'
s = re.sub(regex_p2, r"\\input{\1}", s)
regex_p3 = r'\\include[*]{(.*?)}'
s = re.sub(regex_p3, r"\\input{\1}", s)
write_tex_file(file, s)
return s
def remove_multargument(s, target, k):
ind = s.find(target)
while ind != -1:
start_ind = ind + len(target)
stack_open = 0
stack_close = 0
track_arg = 0
for i, char in enumerate(s[start_ind:]):
if char == '{':
stack_open += 1
if char == '}':
stack_close += 1
if stack_open !=0 and stack_close !=0:
if stack_open == stack_close:
track_arg += 1
stack_open = 0
stack_close = 0
if track_arg == k:
break
s = s[:ind] + s[start_ind + i + 1:]
ind = s.find(target)
return s
def fix_citations(s):
pattern = {
r'\\citep\*': r'\\cite',
r'\\citet\*': r'\\cite',
r'\\citep': r'\\cite',
r'\\citet': r'\\cite',
r'\\cite\*': r'\\cite',
r'\\citealt\*': r'\\cite',
r'\\citealt': r'\\cite',
r'\\citealtp\*': r'\\cite',
r'\\citealp': r'\\cite',
r'\\citeyear\*': r'\\cite',
r'\\citeyear': r'\\cite',
r'\\citeauthor\*': r'\\cite',
r'\\citeauthor': r'\\cite',
r'\\citenum\*': r'\\cite',
r'\\citenum': r'\\cite'
}
for key in pattern:
s = re.sub(key, pattern[key], s)
return s
def find_bib(directory):
file_paths = []
for root, _, files in os.walk(directory):
for file in files:
file_path = os.path.join(root, file)
file_paths.append(file_path)
bib_paths = [f for f in file_paths if f.endswith('.bib')]
return bib_paths
def create_bib_from_bbl(bibfile):
with open(bibfile, 'r') as f:
content = f.read()
library_raw = bibtexparser.parse_string(content)
library = {}
for block in library_raw.blocks:
if isinstance(
block,
(bibtexparser.model.DuplicateBlockKeyBlock, bibtexparser.model.ParsingFailedBlock, bibtexparser.model.ImplicitComment)
):
continue
fields = {}
for field in block.fields:
fields[field.key] = field.value
## Get a good title one ##
field_content = fields["note"]
field_content = field_content.replace("\n", " ")
field_content = re.sub(" +", " ", field_content)
if field_content.find("``") != -1 and field_content.find("\'\'") != -1:
title = (
field_content[field_content.find("``") + 2 : field_content.find("\'\'")]
.replace("\\emph", "")
.replace("\\emp", "")
.replace("\\em", "")
.replace(",", "")
.replace("{", "")
.replace("}","")
.replace("``", "")
.replace("\'\'", "")
.strip(".")
.strip()
.strip(".")
.lower()
)
fields['title'] = title
else:
if field_content.count("\\newblock") == 2:
field_content = field_content.replace("\\newblock", "``", 1)
field_content = field_content.replace("\\newblock", "\'\'", 1)
if field_content.find("``") != -1 and field_content.find("\'\'") != -1:
title = (
field_content[field_content.find("``") + 2 : field_content.find("\'\'")]
.replace("\\emph", "")
.replace("\\emp", "")
.replace("\\em", "")
.replace(",", "")
.replace("{", "")
.replace("}","")
.replace("``", "")
.replace("\'\'", "")
.strip(".")
.strip()
.strip(".")
.lower()
)
fields['title'] = title
library[block.key] = fields
return library
def create_bib(bibfile):
with open(bibfile, 'r') as f:
content = f.read()
library_raw = bibtexparser.parse_string(content)
library = {}
for block in library_raw.blocks:
if isinstance(
block,
(bibtexparser.model.DuplicateBlockKeyBlock, bibtexparser.model.ParsingFailedBlock, bibtexparser.model.ImplicitComment)
):
continue
fields = {}
for field in block.fields:
fields[field.key] = field.value.replace('{', '').replace('}', '')
if field.key == 'title':
title = re.sub(r'[\n]+', ' ', field.value) # keep only one \n
title = re.sub(r' +', ' ', title)
fields[field.key] = (
title.replace("\\emph", "")
.replace("\\emp", "")
.replace("\\em", "")
.replace(",", "")
.replace("{", "")
.replace("}", "")
.strip(".")
.strip()
.strip(".")
.lower()
)
if 'title' not in fields:
continue
library[block.key] = fields
return library
def find_bbl(directory):
file_paths = []
for root, _, files in os.walk(directory):
for file in files:
file_path = os.path.join(root, file)
file_paths.append(file_path)
bib_paths = [f for f in file_paths if f.endswith('.bbl')]
return bib_paths
def textobib(file):
oldpwd = os.getcwd()
target_dir = os.path.dirname(file) + '/'
target = target_dir + 'tex2bib'
src = './tex2bib'
shutil.copyfile(src, target)
os.chdir(target_dir)
output_file = os.path.splitext(os.path.basename(file))[0] + '.bib'
os.system('perl tex2bib -i {} -o {}'.format(os.path.basename(file), output_file))
os.chdir(oldpwd)
return target_dir + output_file
def get_library_bib(bib_files):
library = []
for bib_file in bib_files:
library.append(create_bib(bib_file))
final_library = {}
for d in library:
final_library.update(d)
return final_library
def get_library_bbl(bbl_files):
bib_files = []
for bbl_file in bbl_files:
bib_files.append(textobib(bbl_file))
library = []
for bib_file in bib_files:
library.append(create_bib_from_bbl(bib_file))
final_library = {}
for d in library:
final_library.update(d)
return final_library
|