-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjclimate_htmlpars.py
362 lines (321 loc) · 16.3 KB
/
jclimate_htmlpars.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
"""
GCB html parsing
"""
from bs4 import BeautifulSoup
from parser_pdf import get_abstract, get_title, get_content, get_doi_regex, get_from_doi2bibapi, get_affiliations, get_references_nonumber, get_keywords, char_number2words_pages
from functions import pdf2html, find_custom_element_by_regex, add_custom_tag_after_element, check_if_ium
import re
from os import listdir
import pandas as pd
from tqdm import tqdm
import logging
import argparse
from time import time
from random import randint
## Multprocessing add-on
def list_of_strings(arg):
return arg.split('ž')
def number(arg):
return arg
parser = argparse.ArgumentParser()
parser.add_argument("--str-list", type=list_of_strings)
args = parser.parse_args()
samples = args.str_list
multi_flag = True # Flag to see if script is run on multiprocessing manner
##
DIR = "./FULL_DATA/JCLIMATE/"
##
logging.basicConfig(
format='%(asctime)s %(message)s',
filename="_".join(DIR.split("/")),
filemode='w',
) # Adds time to warning output
doctype0_1 = {
"get_title": ["font-family: Times-Bold; font-size:12px"],
"get_doi_regex": ["font-family: Times-Roman; font-size:8px"],
"get_doi_regex_r": ["[Dd][Oo][Ii]:\s*([\d.\/\w-]+)"],
"get_affiliations": ["font-family: Times-Italic; font-size:8px"], # Affiliation text
"get_references_nonumber_title": ["font-family: Times-Roman; font-size:8px",
"font-family: Symbol; font-size:8px"], # Reference title
"get_references_nonumber_title_r": ["^(?i)r\s*e\s*f\s*e\s*r\s*e\s*n\s*c\s*e\s*s\n"], # Reference custom regex
"get_references_nonumber_ref": ["font-family: Times-Roman; font-size:8px",
"font-family: Times-Bold; font-size:8px",
"font-family: Times-Italic; font-size:8px",
"font-family: Symbol; font-size:8px"], # References
"get_content": ["font-family: Times-(Roman|Italic); font-size:10px"], # Content
"get_keywords": ["font-family: Times-Italic; font-size:8px"], # Keywords
"get_keywords_r": ["^(?i)k\s*e\s*y\s*w\s*o\s*r\s*d\s*s\n*"], # Keywords regex
"get_keywords_styles": ["font-family: Times-Italic; font-size:8px"], # Keywords styles
"get_abstract": ["font-family: Times-Roman; font-size:8px", "font-family: Symbol; font-size:8px"], # Abstract
}
doctype1_1 = {
"get_title": ["font-family: AdvPSTIM10-B; font-size:11px"],
"get_doi_regex": ["font-family: AdvPSTIM10-R; font-size:7px"], # Styles of doi text
"get_doi_regex_r": ["[Dd][Oo][Ii]:\s*([\d.\/\w-]+)"],
"get_affiliations": ["font-family: AdvPSTIM10-I; font-size:7px"], # Affiliation text
"get_references_nonumber_title": ["font-family: AdvPSTIM10-R; font-size:7px"], # Reference title
"get_references_nonumber_title_r": ["^(?i)r\s*e\s*f\s*e\s*r\s*e\s*n\s*c\s*e\s*s\n"], # Reference custom regex
"get_references_nonumber_ref": ["font-family: AdvPSTIM10-R; font-size:7px", "font-family: AdvPSTIM10-I; font-size:7px"], # References
"get_content": ["font-family: (AdvPSTIM10-R|AdvPSTIM10-I|AdvP4C4E46); font-size:9px"], # Content regex
"get_keywords": ["font-family: Times-Italic; font-size:8px"], # Keywords
"get_keywords_r": ["^(?i)k\s*e\s*y\s*w\s*o\s*r\s*d\s*s\n*"], # Keywords regex
"get_keywords_styles": ["font-family: Times-Italic; font-size:8px"], # Keywords styles
"get_abstract": ["font-family: AdvPSTIM10-R; font-size:7px"], # Abstract
}
doctype2_1 = {
"get_title": ["font-family: AdvPSTIM10-B; font-size:10px"],
"get_doi_regex": ["font-family: AdvOTbb216540; font-size:7px",
"font-family: AdvPSTIM10-R; font-size:7px"], # Styles of doi text
"get_doi_regex_r": ["[Dd][Oo][Ii]:\s*([\d.\/\w-]+)"],
"get_affiliations": ["font-family: AdvOT2b0f33d7.I; font-size:7px",
"font-family: AdvPSTIM10-I; font-size:7px"], # Affiliation text
"get_references_nonumber_title": ["font-family: AdvOTbb216540; font-size:7px",
"font-family: AdvPSTIM10-R; font-size:7px"], # Reference title
"get_references_nonumber_title_r": ["^(?i)r\s*e\s*f\s*e\s*r\s*e\s*n\s*c\s*e\s*s\n"], # Reference custom regex
"get_references_nonumber_ref": ["font-family: AdvOTbb216540; font-size:7px", "font-family: AdvOT2b0f33d7.I; font-size:7px",
"font-family: AdvPSTIM10-R; font-size:7px", "font-family: AdvPSTIM10-I; font-size:7px"], # References
"get_content": ["font-family: (AdvOTbb216540|AdvPSTIM10-R); font-size:8px"], # Content regex
"get_keywords": ["font-family: AdvOTbb216540; font-size:7px",
"font-family: AdvPSTIM10-R; font-size:7px"], # Keywords
"get_keywords_r": ["^(?i)k\s*e\s*y\s*w\s*o\s*r\s*d\s*s\n*"], # Keywords regex
"get_keywords_styles": ["font-family: AdvOTbb216540; font-size:7px",
"font-family: AdvPSTIM10-R; font-size:7px"], # Keywords styles
"get_abstract": ["font-family: AdvOTbb216540; font-size:7px",
"font-family: AdvPSTIM10-R; font-size:7px"], # Abstract
}
doctype3_1 = {
"get_title": ["font-family: TimesTen-Bold; font-size:11px"],
"get_doi_regex": ["font-family: AdvPSTIM10-R; font-size:7px",
"font-family: TimesTen-Roman; font-size:7px"], # Styles of doi text
"get_doi_regex_r": ["[Dd][Oo][Ii]:\s*([\d.\/\w-]+)"],
"get_affiliations": ["font-family: TimesTen-Italic; font-size:7px"], # Affiliation text
"get_references_nonumber_title": ["font-family: TimesTen-Roman; font-size:7px"], # Reference title
"get_references_nonumber_title_r": ["^(?i)r\s*e\s*f\s*e\s*r\s*e\s*n\s*c\s*e\s*s\n"], # Reference custom regex
"get_references_nonumber_ref": ["font-family: TimesTen-Roman; font-size:7px", "font-family: TimesTen-Italic; font-size:7px"], # References
"get_content": ["font-family: TimesTen-Roman; font-size:9px"], # Content regex
"get_keywords": ["font-family: Times-Italic; font-size:8px"], # Keywords
"get_keywords_r": ["^(?i)k\s*e\s*y\s*w\s*o\s*r\s*d\s*s\n*"], # Keywords regex
"get_keywords_styles": ["font-family: Times-Italic; font-size:8px"], # Keywords styles
"get_abstract": ["font-family: TimesTen-Roman; font-size:7px"], # Abstract
}
doctype4_1 = {
"get_title": ["font-family: Times-Bold; font-size:12px"],
"get_doi_regex": ["font-family: Times-Roman; font-size:7px"], # Styles of doi text
"get_doi_regex_r": ["[Dd][Oo][Ii]:\s*([\d.\/\w-]+)"],
"get_affiliations": ["font-family: Times-Italic; font-size:8px"], # Affiliation text
"get_references_nonumber_title": ["font-family: Times-Roman; font-size:8px",
"font-family: Symbol; font-size:8px"], # Reference title
"get_references_nonumber_title_r": ["^(?i)r\s*e\s*f\s*e\s*r\s*e\s*n\s*c\s*e\s*s\n"], # Reference custom regex
"get_references_nonumber_ref": ["font-family: Times-Roman; font-size:8px",
"font-family: Times-Italic; font-size:8px",
"font-family: Symbol; font-size:8px"], # References
"get_content": ["font-family: Times-(Roman|Italic); font-size:10px"], # Content regex
"get_keywords": ["font-family: Times-Italic; font-size:8px"], # Keywords
"get_keywords_r": ["^(?i)k\s*e\s*y\s*w\s*o\s*r\s*d\s*s\n*"], # Keywords regex
"get_keywords_styles": ["font-family: Times-Italic; font-size:8px"], # Keywords styles
"get_abstract": ["font-family: Times-Roman; font-size:8px",
"font-family: Symbol; font-size:8px"], # Abstract
}
doctypedef_1 = {
"get_title": ["font-size:17px"],
"get_doi_regex": ["font-size:8px"],
"get_authors_and_affiliations_au": ["font-size:8px"], # Author
"get_authors_and_affiliations_nu": ["font-size:8px"], # Number
"get_affiliations": ["font-size:8px"], # Affiliation text
"get_references_nonumber_title": ["font-size:10px"], # Reference title
"get_references_nonumber_ref": ["font-size:6px", "font-size:5px" ], # References
"get_content": ["font-size:9px"], # Content
"get_keywords": ["font-size:8px"], # Keywords
"get_abstract": ["font-size:9px"]
}
# List of style samples to try for processing
styles = [doctype0_1, doctype1_1, doctype2_1, doctype3_1, doctype4_1, doctypedef_1]
data_list = []
Faults = 0
Faulty_samples = []
Styleless_samples = []
skip_samples = ["masthead"]
# samples = [a.replace(".html", ".pdf") for a in listdir(DIR.replace("SAMPLE", "TEST"))]
if not samples:
samples = listdir(DIR)
multi_flag = False
# # print(samples[2])
# exit()
# samples = ["Global Change Biology - 2001 - Hendrey - A free‐air enrichment system for exposing tall forest vegetation to elevated(2).pdf"]
for sample in samples:
s = 0
# print(20*"-")
print(sample)
should_skip = any(True for skip in skip_samples if skip in sample)
if should_skip:
# print(f"Skipping {sample.split()[0]} pdf: {sample}")
continue
# Parse to html
html = pdf2html(target=DIR+sample, all_texts=False)
if not html:
Faults += 1
warning_message = f"HTML isn't parsed correctly -> Implies invalid pdf structure!"
logging.warning(warning_message)
Faulty_samples.append(sample)
continue
# Wy to deal with pdf scans heuristicaly
if len(html) < 15000:
warning_message = f"HTML is less then 15000 characters long -> Implies a scanned PDF document!"
logging.warning(warning_message)
continue
# Create soup object
soup = BeautifulSoup(html, 'html.parser')
if check_if_ium(soup):
warning_message = f"HTML isn't parsed correctly due to incomplite unicode mappings."
logging.warning(warning_message)
Faulty_samples.append(sample)
continue
# Extract title data
title = [""]
while len(title[0]) == 0:
try:
style = styles[s]
except:
warning_message = "Title isn't extracted correctly. No more styles to try ..."
logging.warning(warning_message)
title = ["no_title"]
s = 0
break
title = get_title(soup, style["get_title"])
# print(title)
if len(title[0]) == 0:
warning_message = "Title isn't extracted correctly. -> Implies different paper structure! -> Trying style number: {}".format(s+1)
logging.warning(warning_message)
Faults += 1
s += 1
if len(title[0]) > 190:
warning_message = "Title too long. -> Implies different paper structure! -> Trying style number: {}".format(s+1)
logging.warning(warning_message)
title[0] = ""
Faults += 1
s += 1
# Extract doi data
doi = []
while len(doi) == 0:
try:
style = styles[s]
except:
warning_message = "DOI isn't extracted correctly. No more styles to try ..."
logging.warning(warning_message)
title = ["no_doi"]
s = -1
break
doi = get_doi_regex(soup, style["get_doi_regex"])
# print(doi)
if len(doi) == 0:
warning_message = "DOI isn't extracted correctly. -> Implies different paper structure! Skipping paper! Trying style number: {}".format(s+1)
logging.warning(warning_message)
Faults += 1
s += 1
# Try available regex patterns for the style
if doi[0] == "no_doi":
warning_message = "DOI isn't extracted correctly. -> Implies wrong regex pattern, trying other options if available ..."
logging.warning(warning_message)
if "get_doi_regex_r" in style.keys():
for regex in style["get_doi_regex_r"]:
doi = get_doi_regex(soup, style["get_doi_regex"], regex)
if doi[0] != "no_doi":
# print(doi)
break
# Get data
if s >= 0 and s < len(styles):
style = styles[s]
if "get_keywords_r" in style.keys():
if "get_keywords_styles" in style.keys():
keywords = get_keywords(soup, style["get_keywords"], style["get_keywords_r"][0], style["get_keywords_styles"])
else:
keywords = get_keywords(soup, style["get_keywords"], style["get_keywords_r"][0])
else:
keywords = get_keywords(soup, style["get_keywords"])
# authors_and_affiliations, affiliations = get_authors_and_affiliations(soup, style["get_authors_and_affiliations_au"], style["get_authors_and_affiliations_nu"], style["get_authors_and_affiliations_af"])
# authors_and_affiliations, affiliations = [], []
# # print(affiliations)
authors_and_affiliations = ["no_auth_and_affil"]
affiliations = get_affiliations(soup, style["get_affiliations"])
authors, journal, date, subjects, abstract = get_from_doi2bibapi(doi[0]) # Sa meta/v2 je bilo moguće dohvatiti i disciplines
# Try available regex styles if unintentional doi was found prior
if authors == "no_authors":
warning_message = "DOI isn't extracted correctly. -> Implies wrong regex pattern, trying other options if available ..."
logging.warning(warning_message)
if "get_doi_regex_r" in style.keys():
for regex in style["get_doi_regex_r"]:
doi = get_doi_regex(soup, style["get_doi_regex"], regex)
if doi[0] != "no_doi":
# print(doi)
break
if doi[0].endswith("."): # Hot fix if doi ends with .
doi[0] = doi[0][:-1]
authors, journal, date, subjects, abstract = get_from_doi2bibapi(doi[0]) # Sa meta/v2 je bilo moguće dohvatiti i disciplines
# # print(authors)
# # print(journal)
# # print(date)
# # print(subjects)
# # print(abstract[:100])
if "get_references_nonumber_title_r" in style.keys():
references = get_references_nonumber(soup, style["get_references_nonumber_title"], style["get_references_nonumber_ref"], style["get_references_nonumber_title_r"][0])
else:
references = get_references_nonumber(soup, style["get_references_nonumber_title"], style["get_references_nonumber_ref"])
# # print(references[:5])
if "get_abstract" in style.keys() and abstract == "no_abstract":
abstract = get_abstract(soup, style["get_abstract"])
# Add references tag to remove during content extraction
elem = find_custom_element_by_regex(soup)
add_custom_tag_after_element(soup, elem, "reftag", "STOP CONTENT EXTRACTION HERE IN THE NAME OF GOD", {'style': 'font-family: TimesNewReference; font-size:69px'})
content = get_content(soup, style["get_content"])
# # print("Content length: ", len(content))
# char_number2words_pages(len(content), re.findall(r"font-size:(\d+)", style["get_content"][0])[0])
# # print(abstract)
# Create a dictionary with the paper's data
paper_data = {
"Title": title,
"Authors_and_Affiliations": authors_and_affiliations,
"Affiliations": affiliations,
"DOI": doi,
"Authors": authors,
"Journal": journal,
"Date": date,
"Subjects": subjects,
"Abstract": abstract,
"References": references,
"Content": content,
"Keywords": keywords,
"Style": s,
}
# Append the dictionary to the list
data_list.append(paper_data)
else:
paper_data = {
"Title": title,
"Authors_and_Affiliations": "no_auth_and_affil",
"Affiliations": "no_affil",
"DOI": doi,
"Authors": "no_authors",
"Journal": "no_journal",
"Date": "no_date",
"Subjects": "no_subjects",
"Abstract": "no_abstract",
"References": "no_references",
"Content": "no_content",
"Keywords": "no_keywords",
"Style": s,
}
Styleless_samples.append(sample)
# Create the DataFrame from the list of dictionaries
# print(Styleless_samples)
# print(Faulty_samples)
# # print(paper_data)
##
t = round(time(), 1) # Timestamp when multiprocessing
n = randint(1, 10) # For fragments of dataframes
df = pd.DataFrame(data_list)
if multi_flag:
df.to_pickle(f"./RESULTS/JCLIMATE/jclimate_({t})_({n}).pickle")
else:
df.to_pickle("./PARS_OUT/test_jclimate.pickle")
# print(Faults)
##