-
Notifications
You must be signed in to change notification settings - Fork 10
/
complete_dataset.py
162 lines (130 loc) · 5.45 KB
/
complete_dataset.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
import urllib.request
import shutil
import os
import tarfile
import random
import glob
from TexSoup import TexSoup
def import_resolve(tex, path):
"""Resolve all imports and update the parse tree.
Reads from a tex file and once finished, writes to a tex file.
"""
soup = TexSoup(tex)
dir_path = os.path.dirname(path) + "/"
for _input in soup.find_all('input'):
#print("input statement detected")
path = os.path.join(dir_path, _input.args[0])
if not os.path.exists(path):
path = path + ".tex"
#print("Resolved Path:", path)
_input.replace(*import_resolve(open(path), dir_path).contents)
# CHECK FOLLOWING ONES
# resolve subimports
for subimport in soup.find_all('subimport'):
#print("subimport statement detected")
path = os.path.join(dir_path, subimport.args[0] + subimport.args[1])
if not os.path.exists(path):
path = path + ".tex"
#print("Resolved Path:", path)
subimport.replace(*import_resolve(open(path), dir_path).contents)
# resolve imports
for _import in soup.find_all('import'):
#print("import statement detected")
path = os.path.join(dir_path, _import.args[0])
if not os.path.exists(path):
path = path + ".tex"
#print("Resolved Path:", path)
_import.replace(*import_resolve(open(path), dir_path).contents)
# resolve includes
for include in soup.find_all('include'):
#print("include statement detected")
path = os.path.join(dir_path, include.args[0])
if not os.path.exists(path):
path = path + ".tex"
#print("Resolved Path:", path)
include.replace(*import_resolve(open(path), dir_path).contents)
return soup
DOWNLOAD_FOLDER = "./compressed_paper_folders/"
EXTRACT_FOLDER = "./paper_folders/"
FINAL_FOLDER = "./papers/"
if not os.path.exists(DOWNLOAD_FOLDER):
os.mkdir(DOWNLOAD_FOLDER)
if not os.path.exists(EXTRACT_FOLDER):
os.mkdir(EXTRACT_FOLDER)
if not os.path.exists(FINAL_FOLDER):
os.mkdir(FINAL_FOLDER)
papers = open("selected_papers.txt", "rt")
selected_papers = papers.read().split("\n")
papers.close()
nb_papers = int(len(selected_papers) / 2)
print("number papers:", nb_papers)
papers = open("paperlinks.txt", "rt")
all_papers = papers.read().split("\n")
papers.close()
nb_total_papers = int(len(all_papers) / 2)
print("number total papers:", nb_total_papers)
error_papers_file = open("error_papers.txt", "at")
error_indices = []
for i in range(0, nb_papers, 1):
if not (os.path.exists(FINAL_FOLDER + str(i) + ".tex") or os.path.exists(FINAL_FOLDER + str(i) + "-0.tex")):
print("missing paper %g" % (i))
error_indices.append(i)
paper_link = selected_papers[2*i+1]
error_papers_file.write(paper_link + "\n")
error_papers_file.close()
error_papers_file = open("error_papers.txt", "rt")
error_papers = error_papers_file.read().split("\n")
error_papers_file.close()
for i in error_indices:
new_selection = random.randint(0,nb_total_papers-1)
paper_link = all_papers[2*new_selection+1]
while paper_link in error_papers or paper_link in selected_papers:
new_selection = random.randint(0,nb_total_papers-1)
paper_link = all_papers[2*new_selection+1]
paper_title = all_papers[2*new_selection]
selected_papers[2*i] = paper_title
selected_papers[2*i+1] = paper_link
paper_code = paper_link.split("/")[-1]
paper_source_link = "https://arxiv.org/e-print/" + paper_code
try:
# Download the file from `paper_source_link` and save it locally under `DOWNLOAD_FOLDER+str(i)+".tar.gz"`:
compressed_file_path = DOWNLOAD_FOLDER+str(i)+".tar.gz"
with urllib.request.urlopen(paper_source_link) as response, open(compressed_file_path, 'wb') as out_file:
shutil.copyfileobj(response, out_file)
# Extract from tar the tar file
tar = tarfile.open(compressed_file_path)
paper_folder_dir = EXTRACT_FOLDER + str(i) + "/"
tar.extractall(path=paper_folder_dir)
tar.close()
# Solve Latex Input Statements
paper_folder_dir = EXTRACT_FOLDER + str(i) + "/**/"
extension = "*.tex"
tex_files = glob.glob(paper_folder_dir + extension, recursive=True)
root_files = []
for f_path in tex_files:
with open(f_path) as f:
tex = f.read()
soup = TexSoup(tex)
if soup.documentclass is not None:
latex_object = import_resolve(tex, f_path)
root_files.append(latex_object)
if len(root_files) < 1:
print("no root file?")
elif len(root_files) > 1:
print("writing multiple root files for paper", i)
for j in range(len(root_files)):
with open(FINAL_FOLDER + str(i) + "-" + str(j) + ".tex", "wt") as f:
f.write(str(root_files[j]))
else:
print("writing single root file for paper", i)
with open(FINAL_FOLDER + str(i) + ".tex", "wt") as f:
f.write(str(root_files[0]))
except Exception as e:
print("error at paper %g" % (i))
print(e)
print("progress: %g / %g" % (i,nb_papers), end="\r")
# Rewrite selected_papers_file
selected_papers_file = open("selected_papers.txt", "wt")
for i in range(len(selected_papers)-1):
selected_papers_file.write(selected_papers[i] + "\n")
selected_papers_file.close()