forked from nlmatics/nlm-ingestor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
html_ingestor.py
324 lines (298 loc) · 13.3 KB
/
html_ingestor.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
import logging
from bs4 import BeautifulSoup, UnicodeDammit
from nlm_ingestor.ingestor_utils.ing_named_tuples import LineStyle
from nlm_ingestor.ingestor.visual_ingestor import block_renderer
from nlm_ingestor.ingestor_utils.utils import safe_int, sent_tokenize
from nlm_ingestor.ingestor import line_parser
import codecs
class HTMLIngestor:
def __init__(self, file_name, sec=False):
self.logger = logging.getLogger(self.__class__.__name__)
self.logger.setLevel(logging.INFO)
if str(type(file_name)) == "<class 'bs4.element.Tag'>":
self.html = file_name
else:
with open(file_name, 'rb') as f:
raw_data = f.read()
suggestion = UnicodeDammit(raw_data)
f = codecs.open(file_name, 'r', encoding=suggestion.original_encoding, errors='ignore')
self.html = BeautifulSoup(f.read(), features="html5lib")
self.html = self.html.find("body")
self.sec = sec
self.blocks = []
self.parse_blocks()
self.line_style_classes = {}
self.class_levels = {}
self.add_styles()
br = block_renderer.BlockRenderer(self)
self.html_str = br.render_html()
self.json_dict = br.render_json()
def parse_blocks(self):
self.logger.info("parsing html file")
header_tags = ["h1", "h2", "h3", "h4", "h5", "h6"]
para_tags = ["p", "span"]
# list: li
# bold: b, em, strong
i = 0
children = self.html.findChildren(recursive=True)
level_stack = []
header_stack = []
while i < len(children):
child = children[i]
if not child.text.strip():
i += 1
continue
tag = child.name
if self.sec:
# some containers are actually p
div_is_para = True
current_level_child = [c.name for c in child.findChildren(recursive=False)]
if len(current_level_child) > 0:
for name in current_level_child:
if name != "font":
div_is_para = False
# use styles to determine headers
style = self.parse_style(child.get("style"))
if "font-weight" in style and style["font-weight"] == "bold":
line = line_parser.Line(child.text)
if line.is_header:
tag = "h3"
if child.text.isupper():
tag = "h2"
else:
div_is_para = False
div_text = ""
if self.sec:
for c in child.findAll(text=True, recursive=False):
if c.strip():
div_text += c
if tag in header_tags:
if len(level_stack) == 0:
level_stack = [tag]
header_stack = [child.text]
level = 0
elif tag in level_stack:
level = level_stack.index(tag)
level_stack = level_stack[:level+1]
header_stack = header_stack[:level]
header_stack.append(child.text)
else:
idx = 0
while idx < len(level_stack) and level_stack[idx] in header_tags and \
header_tags.index(level_stack[idx]) < header_tags.index(tag):
idx += 1
level_stack = level_stack[:idx]
level_stack.append(tag)
header_stack = header_stack[:idx]
header_stack.append(child.text)
level = idx
header_block = {
"block_idx": len(self.blocks),
"page_idx": 0,
"block_text": child.text,
"block_type": "header",
"block_class": "nlm-text-header",
"header_block_idx": 0,
"level": level,
"header_text": header_stack[-1] if header_stack else "",
"level_chain": header_stack[::-1],
}
self.blocks.append(header_block)
i += len(child.findChildren(recursive=True))
elif tag in para_tags or div_text or div_is_para:
is_header = False
line = line_parser.Line(child.text)
para_child_tag = None
if line.is_header:
is_header = True
if child.name == "p":
para_child = child.findChildren(recursive=True)
if len(para_child) > 0:
para_child_tag = child.name + "_" + para_child[0].name
if is_header and para_child_tag:
if len(level_stack) == 0:
level_stack = [para_child_tag]
header_stack = [child.text]
level = 0
elif para_child_tag in level_stack:
level = level_stack.index(para_child_tag)
level_stack = level_stack[:level+1]
header_stack = header_stack[:level]
header_stack.append(child.text)
else:
idx = len(level_stack)
level_stack = level_stack[:idx]
level_stack.append(para_child_tag)
header_stack = header_stack[:idx]
header_stack.append(child.text)
level = idx
header_block = {
"block_idx": len(self.blocks),
"page_idx": 0,
"block_text": child.text,
"block_type": "header",
"block_class": "nlm-text-header",
"header_block_idx": 0,
"level": level,
"header_text": header_stack[-1] if header_stack else "",
"level_chain": header_stack[::-1],
}
self.blocks.append(header_block)
else:
para_block = {
"block_idx": len(self.blocks),
"page_idx": 0,
"block_text": child.text,
"block_type": "para",
"block_class": "nlm-text-body",
"header_block_idx": 0,
"block_sents": sent_tokenize(child.text),
"level": len(level_stack),
"header_text": header_stack[-1] if header_stack else "",
"level_chain": header_stack[::-1],
}
self.blocks.append(para_block)
i += len(child.findChildren(recursive=True))
elif tag == "li":
list_block = {
"block_idx": len(self.blocks),
"page_idx": 0,
"block_text": child.text,
"block_type": "list_item",
"list_type": "",
"block_class": "nlm-list-item",
"header_block_idx": 0,
"block_sents": sent_tokenize(child.text),
"level": len(level_stack),
"header_text": header_stack[-1] if header_stack else "",
"level_chain": header_stack[::-1],
}
self.blocks.append(list_block)
i += len(child.findChildren(recursive=True))
elif tag == "table":
rows = child.find_all('tr')
table_start_idx = len(self.blocks)
empty_cols = []
for row in rows:
cols = row.find_all(['th', 'td'])
col_text = []
col_spans = []
empty_col = []
header_group_flag = False
all_th = True
for col_idx, col in enumerate(cols):
text = col.text.replace(u'\xa0', '')
text = text.strip()
col_text.append(text)
if not text:
empty_col.append(col_idx)
if not col.name == "th" and text and not col.find('b'):
all_th = False
if col.get("colspan"):
header_group_flag = True
col_spans.append(safe_int(col.get("colspan"), 1))
empty_cols.append(empty_col)
if not ''.join(col_text).strip():
# Empty Row
continue
if len(rows) > 1:
table_row = {
"block_idx": len(self.blocks),
"page_idx": 0,
"block_text": ' '.join([c for c in col_text]),
"block_type": "table_row",
"block_class": "nlm-table-row",
"header_block_idx": 0,
"block_sents": sent_tokenize(' '.join([c for c in col_text])),
"level": len(level_stack),
"header_text": header_stack[-1] if header_stack else "",
"level_chain": header_stack[::-1],
"cell_values": col_text,
"col_spans": col_spans,
}
if header_group_flag:
table_row["is_header_group"] = True
if all_th:
table_row["is_header"] = True
self.blocks.append(table_row)
else:
blk_text = ' '.join(col_text)
line = line_parser.Line(child.text)
is_list_item = False
if line.is_list_item:
is_list_item = True
t_block = {
"block_idx": len(self.blocks),
"page_idx": 0,
"block_text": blk_text,
"block_type": "para",
"block_class": "nlm-text-body",
"header_block_idx": 0,
"block_sents": sent_tokenize(blk_text),
"level": len(level_stack),
"header_text": header_stack[-1] if header_stack else "",
"level_chain": header_stack[::-1],
}
if is_list_item:
t_block["block_type"] = "list_item"
t_block["block_class"] = "nlm-list-item"
t_block["list_type"] = ""
self.blocks.append(t_block)
if len(rows) > 1:
self.blocks[table_start_idx]['is_table_start'] = True
self.blocks[-1]["is_table_end"] = True
# Remove any empty columns if there are intersection
empty_col_intersection = set.intersection(*map(set, empty_cols))
if empty_col_intersection:
# Start from the last as we are popping members out
# might change the number of elements in the list
for inter in list(empty_col_intersection)[::-1]:
for blk in self.blocks[table_start_idx:]:
blk["col_spans"].pop(inter)
blk["cell_values"].pop(inter)
i += len(child.findChildren(recursive=True))
i += 1
def add_styles(self):
title_style = LineStyle(
"Roboto, Georgia, serif",
"bold",
14.0,
"500",
"left",
0, # TODO: Decide what font_space_width needs to be added
"left"
)
self.line_style_classes[title_style] = "nlm-text-title"
self.class_levels["nlm-text-title"] = 0
header_style = LineStyle(
"Roboto, Georgia, serif",
"normal",
12.0,
"600",
"left",
0, # TODO: Decide what font_space_width needs to be added
"left"
)
self.line_style_classes[header_style] = "nlm-text-header"
self.class_levels["nlm-text-header"] = 1
para_style = LineStyle(
"Roboto, Georgia, serif",
"normal",
10.0,
"400",
"left",
0, # TODO: Decide what font_space_width needs to be added
"left"
)
self.line_style_classes[para_style] = 'nlm-text-body'
self.class_levels['nlm-text-body'] = 2
def parse_style(self, style_str):
d = {}
if not style_str:
return d
for style in style_str.split(";"):
style = style.strip()
if ":" in style:
key, value = style.split(":")
d[key] = value
return d