-
Notifications
You must be signed in to change notification settings - Fork 1
/
extra.py
397 lines (324 loc) · 14.1 KB
/
extra.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
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
import io
import json
import os
import re
from io import BytesIO
from pathlib import Path
import fitz
import numpy as np
import requests
from arxiv import Client, Search
from bs4 import BeautifulSoup
from pdfminer.high_level import extract_pages
from pdfminer.layout import LTChar, LTTextContainer
from PIL import Image
import api as api_helper
import constants as con
from helper import log
Path(con.PAPER_PDF_DIR).mkdir(parents=True, exist_ok=True)
Path(con.PAPER_JSON_DIR).mkdir(parents=True, exist_ok=True)
Path(con.PAPER_IMG_DATA_DIR).mkdir(parents=True, exist_ok=True)
Path(con.PAPER_PDF_TITLE_IMG).mkdir(parents=True, exist_ok=True)
class ArxivParser:
def __init__(self, url, delete_pdf=False, recalculate_pdf=False, recalculate_html=False):
self.section_patterns = [
r"^(?:\d+\.)*\d+\s+[A-Z]",
r"^[A-Z][a-zA-Z\s]{2,}$",
]
self.arxiv_id = url.strip("/").split("/")[-1]
self.arxiv_id_no_version = self.arxiv_id.lower().split("v")[0]
self.delete_pdf = delete_pdf
self.recalculate_pdf = recalculate_pdf
self.recalculate_html = recalculate_html
def clean_text(self, text):
text = re.sub(
r"\d+\s*v\s*o\s*N\s*\d+\s*]\s*I\s*A\s*\.\s*s\s*c\s*\[\s*\d+", "", text
)
text = re.sub(r"arXiv:\d+\.\d+v\d+\s*\[\w+\.\w+\]", "", text)
text = re.sub(r"\f|\d+\s*$", "", text)
text = re.sub(r"(\w+)-\s*\n(\w+)", lambda m: m.group(1) + m.group(2), text)
text = re.sub(r"(\w+)-\s+(\w+)", lambda m: m.group(1) + m.group(2), text)
text = re.sub(r"\s+", " ", text)
text = re.sub(r"\s+[a-zA-Z]\s+", " ", text)
text = re.sub(
r"\(([Ff]ig\.|[Tt]able)\s*\d+\)", lambda m: f" {m.group(0)} ", text
)
text = re.sub(r'[^\w\s.,;:?!()\[\]{}"\'`@#$%^&*+=<>/-]', "", text)
return text.strip()
def is_section_header(self, text):
text = text.strip()
return any(re.match(pattern, text) for pattern in self.section_patterns)
def get_font_size(self, text_element):
if not list(text_element):
return None
for text_line in text_element:
for char in text_line:
if isinstance(char, LTChar):
return char.size
return None
def download_and_parse_pdf(self):
#debug
# if not '2411.07133' in self.arxiv_id_no_version:
# print('skip')
# return
pdf_path = os.path.join(con.PAPER_PDF_DIR, f"{self.arxiv_id_no_version}.pdf")
json_path = os.path.join(con.PAPER_JSON_DIR, f"{self.arxiv_id_no_version}.json")
pdf_title_img_path = os.path.join(
con.PAPER_PDF_TITLE_IMG, f"{self.arxiv_id_no_version}.jpg"
)
if os.path.exists(json_path) and not self.recalculate_pdf:
log(f"Extra JSON file exists ({json_path}), skip PDF parsing.")
return None
client = Client()
search = Search(id_list=[self.arxiv_id])
paper = next(client.results(search))
if not paper:
log(f"Error. Paper with ID {self.arxiv_id} not found.")
return
else:
log(f"Downloading paper {self.arxiv_id} from {paper.pdf_url}...")
try:
if not os.path.exists(pdf_path):
response = requests.get(paper.pdf_url)
with open(pdf_path, "wb") as f:
f.write(response.content)
pdf_file = BytesIO(response.content)
else:
log(f"PDF exists ({pdf_path}), skip download.")
pdf_file = open(pdf_path, "rb")
except Exception as e:
log(f"Error downloading PDF ({paper.pdf_url}). Details: {str(e)}")
return None
abstract = paper.summary
abstract = abstract.replace("\n", " ")
abstract = re.sub(r"\s+", " ", abstract)
sections = [{"title": "Abstract", "content": abstract}]
current_section = "Start"
current_text = []
font_sizes = {}
for page_layout in extract_pages(pdf_file):
for element in page_layout:
if isinstance(element, LTTextContainer):
text = element.get_text().strip()
if text:
font_size = self.get_font_size(element)
if font_size:
font_sizes[font_size] = font_sizes.get(font_size, 0) + 1
body_font_size = max(font_sizes.items(), key=lambda x: x[1])[0]
pdf_file.seek(0)
# Second pass: extract text with section structure
buffer = [] # Buffer for handling hyphenation across elements
for page_layout in extract_pages(pdf_file):
for element in page_layout:
if isinstance(element, LTTextContainer):
text = element.get_text().strip()
if not text:
continue
font_size = self.get_font_size(element)
# Check if this is a potential section header
if (
font_size
and font_size > body_font_size
and self.is_section_header(text)
):
if buffer:
current_text.append(self.clean_text(" ".join(buffer)))
buffer = []
if current_text:
sections.append(
{
"title": current_section,
"content": self.clean_text(" ".join(current_text)),
}
)
if not 'Start' in [section['title'] for section in sections]:
current_section = 'Start'
else:
current_section = text
current_text = []
else:
buffer.append(text)
if len(buffer) > 5:
current_text.append(self.clean_text(" ".join(buffer)))
buffer = []
if buffer:
current_text.append(self.clean_text(" ".join(buffer)))
if current_text:
sections.append(
{
"title": current_section,
"content": self.clean_text(" ".join(current_text)),
}
)
parsed_data = {
"paper_title": paper.title,
"authors": [author.name for author in paper.authors],
"sections": sections,
}
get_pdf_image(pdf_path, pdf_title_img_path)
if 'Start' in [section['title'] for section in sections]:
acc = ""
start = False
for section in sections:
if start and section['title'].lower().strip() == 'abstract':
break
if section['title'] == 'Start':
start = True
if start:
acc += section['content'] + " "
affiliations = get_affiliations(acc[:2000], api="openai", model="gpt-4o-mini")
#fallback
if "error" in affiliations:
affiliations = get_affiliations(acc[:2000])
parsed_data['affiliations'] = affiliations
else:
log('No Start section detected to extract affiliations.')
parsed_data['affiliations'] = []
#Search deeper for affiliations
aff_limit = 10000
if not parsed_data['affiliations'] or "error" in parsed_data['affiliations']:
acc = ""
rest_len = aff_limit
for section in sections[1:]:
if rest_len >= 0:
take_text = section["content"][:rest_len]
acc += take_text
rest_len -= len(acc)
else:
break
affiliations = get_affiliations(acc[:6500], api='mistral', model='mistral-large-latest')
if affiliations and not "error" in affiliations:
parsed_data['affiliations'] = affiliations
with open(f"{json_path}", "w", encoding="utf-8") as f:
json.dump(parsed_data, f, indent=4, ensure_ascii=False)
pdf_file.close()
if self.delete_pdf:
log(f"Deleting PDF {pdf_path}.")
os.unlink(pdf_path)
def parse_html(self):
img_data_path = os.path.join(
con.PAPER_IMG_DATA_DIR, f"{self.arxiv_id_no_version}.json"
)
if os.path.exists(img_data_path) and not self.recalculate_html:
log(f"Paper image links file exists ({img_data_path}), skip HTML parsing.")
return
url = f"https://arxiv.org/html/{self.arxiv_id}"
response = requests.get(url)
html_content = response.content
soup = BeautifulSoup(html_content, "html.parser")
headers = [h2.get_text(strip=True) for h2 in soup.find_all("h2")]
header_positions = [h2.sourceline for h2 in soup.find_all("h2")]
result = [{"header": "Abstract", "images": []}] + [
{"header": header, "images": []} for header in headers
]
figures = []
for fig in soup.find_all("figure"):
img = fig.find("img")
figcaption = fig.find("figcaption")
if img:
img_src = img["src"]
item = {
"img": img_src,
"caption": "",
"position": fig.sourceline,
}
if figcaption:
item["caption"] = figcaption.get_text(strip=True)
figures.append(item)
for img in soup.find_all("img", class_="ltx_graphics"):
existed_figures = [figure["img"] for figure in figures]
if img and not img["src"] in existed_figures:
img_src = img["src"]
figures.append(
{
"img": img_src,
"caption": "",
"position": img.sourceline,
}
)
figures = sorted(figures, key=lambda x: x["position"])
current_header_index = 0
for figure in figures:
while (
current_header_index < len(header_positions) - 1
and figure["position"] > header_positions[current_header_index]
):
current_header_index += 1
result[current_header_index]["images"].append(
{
"img": f"https://arxiv.org/html/{self.arxiv_id}/{figure['img']}",
"caption": figure["caption"],
"position": figure["position"],
}
)
json.dump(
result,
open(img_data_path, "w", encoding="utf8"),
indent=4,
ensure_ascii=False,
)
def get_pdf_image(pdf_path, output_path, zoom=2, crop_percent=30, threshold=250):
try:
pdf_document = fitz.open(pdf_path)
first_page = pdf_document[0]
mat = fitz.Matrix(zoom, zoom)
pix = first_page.get_pixmap(matrix=mat)
samples_per_pixel = 3 if pix.n == 3 else 4
img_array = np.frombuffer(pix.samples, dtype=np.uint8)
img_array = img_array.reshape(pix.height, pix.width, samples_per_pixel)
if samples_per_pixel == 4:
grayscale = np.mean(img_array[:, :, :3], axis=2)
else:
grayscale = np.mean(img_array, axis=2)
non_white_cols = np.where(np.min(grayscale, axis=0) < threshold)[0]
if len(non_white_cols) > 0:
# first_content_col = int(non_white_cols[0])
last_content_col = int(non_white_cols[-1])
else:
# first_content_col = 0
last_content_col = pix.width - 1
non_white_rows = np.where(np.min(grayscale, axis=1) < threshold)[0]
if len(non_white_rows) > 0:
first_content_row = non_white_rows[0]
else:
first_content_row = 0
content_height = pix.height - first_content_row
if crop_percent > 5:
crop_percent = max(0, min(100, crop_percent))
keep_height = int((crop_percent / 100) * content_height)
else:
keep_height = content_height
x0 = int(pix.width - min(pix.width, last_content_col + 20))
y0 = int(max(0,first_content_row-20))
x1 = int(min(pix.width, last_content_col + 20))
y1 = int(first_content_row + keep_height)
irect = fitz.IRect(x0, y0, x1, y1)
cropped_pix = fitz.Pixmap(pix.colorspace, irect, False)
cropped_pix.copy(pix, irect)
img_data = cropped_pix.tobytes("png")
pil_image = Image.open(io.BytesIO(img_data))
# pil_image = pil_image.convert("RGBA")
# bg = Image.new("RGBA", pil_image.size, (255, 255, 255, 0))
# bg.paste(pil_image, (0, 0), pil_image)
#transparent bg
# pixdata = pil_image.load()
# width, height = pil_image.size
# for y in range(height):
# for x in range(width):
# if pixdata[x, y] == (255, 255, 255, 255):
# pixdata[x, y] = (255, 255, 255, 0)
pil_image.save(output_path, "JPEG", quality=65)
pdf_document.close()
return output_path
except Exception as e:
log(f"Error generating title image for PDF (pdf_path): {str(e)}")
return None
def get_affiliations(text, api='mistral', model='open-mistral-nemo'):
log("Extracting affiliations from text.")
prompt = f"I give you a contaminated text with start of ML paper. Extract all authors affiliations as a single institute, firm, company, etc. Return items as a Python plain list only with affiliations. Do not provide commentaries. If there are no affiliations return empty list.\n\nText:\"{text}\""
res = api_helper.get_json(
prompt, api=api, model=model, temperature=0.0
)
if isinstance(res, list):
res = sorted(list(set(res)))
return res