-
Notifications
You must be signed in to change notification settings - Fork 9
/
pre_extraction_simplificator.py
181 lines (152 loc) · 6.18 KB
/
pre_extraction_simplificator.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
import re
from obelics.utils import (
MEDIA_CONTAIN_INTERESTING_ATTRIBUTES_SET,
TAG_TO_SEP,
simplify_media_node,
)
class Node:
def __init__(self, path_in_tree, media_info, text, children):
self.path_in_tree = path_in_tree
self.media_info = media_info
self.text = text
self.children = children
@property
def tag(self):
return self.path_in_tree[-1][0]
@property
def level(self):
return len(self.path_in_tree)
class Tree:
def __init__(
self,
selectolax_root_node,
page_url,
):
self.num_nodes = 0
self.tree = self.make_tree(selectolax_root_node, page_url)
def make_tree(self, selectolax_node, page_url, path_in_tree=[]):
tag = selectolax_node.tag
path_in_tree = path_in_tree + [[tag, self.num_nodes]]
self.num_nodes += 1
if tag in MEDIA_CONTAIN_INTERESTING_ATTRIBUTES_SET:
return Node(
path_in_tree=path_in_tree,
media_info=simplify_media_node(selectolax_node, page_url=page_url),
text="",
children=[],
)
elif tag == "-text":
return Node(
path_in_tree=path_in_tree,
media_info=None,
text=selectolax_node.text(deep=False, separator="", strip=False),
children=[],
)
return Node(
path_in_tree=path_in_tree,
media_info=None,
text="",
children=[
self.make_tree(child, page_url=page_url, path_in_tree=path_in_tree)
for child in selectolax_node.iter(include_text=True)
],
)
def traverse(self):
def traverse_recursive(node, list_nodes):
list_nodes.append(node)
for child_node in node.children:
traverse_recursive(child_node, list_nodes=list_nodes)
list_nodes = []
traverse_recursive(self.tree, list_nodes=list_nodes)
return list_nodes
class PreExtractionSimplificator:
def __init__(
self,
only_text_image_nodes=True,
format_texts=True,
merge_consecutive_text_nodes=True,
):
self.only_text_image_nodes = only_text_image_nodes
self.format_texts = format_texts
self.merge_consecutive_text_nodes = merge_consecutive_text_nodes
def __call__(self, selectolax_tree, page_url):
tree = Tree(selectolax_tree.root, page_url=page_url)
list_nodes = tree.traverse()
if self.only_text_image_nodes:
list_nodes = self._only_text_image_nodes(list_nodes)
if self.format_texts:
list_nodes = self._format_texts(list_nodes)
if self.merge_consecutive_text_nodes:
list_nodes = self._merge_consecutive_text_nodes(list_nodes)
return list_nodes
def _only_text_image_nodes(self, list_nodes):
list_nodes = [
node
for node in list_nodes
if (node.tag == "-text") or (node.tag == "figure") or ((node.tag == "img") and (node.media_info))
]
return list_nodes
def _format_texts(self, list_nodes):
def format_one_text(text):
if text == "":
return text
text = text.replace("\n", " ")
text = text.replace("\t", " ")
text = re.sub(r"[ ]{2,}", " ", text)
beg_sep = " " == text[0]
end_sep = (" " == text[-1]) and (len(text) > 1)
text = "\n".join([el.strip() for el in text.split("#BR_TAG#")])
text = beg_sep * " " + text + end_sep * " "
return text
for idx, node in enumerate(list_nodes):
list_nodes[idx].text = format_one_text(node.text)
list_nodes = [node for node in list_nodes if (node.tag != "-text") or ((node.tag == "-text") and (node.text))]
return list_nodes
def _merge_consecutive_text_nodes(self, list_nodes):
current_idx = 0
while current_idx <= len(list_nodes) - 1:
if list_nodes[current_idx].tag != "-text":
current_idx += 1
else:
if (current_idx == len(list_nodes) - 1) or (
(current_idx + 1 <= len(list_nodes) - 1) and (list_nodes[current_idx + 1].tag != "-text")
):
list_nodes[current_idx].path_in_tree = [["-text", 0]]
list_nodes[current_idx].text = list_nodes[current_idx].text.strip()
current_idx += 1
else:
seps = set()
text_1 = list_nodes[current_idx].text
text_2 = list_nodes[current_idx + 1].text
for char in ["\n\n", "\n", " "]:
if text_1.endswith(char):
seps.add(char)
text_1 = text_1[: -len(char)]
if text_2.startswith(char):
seps.add(char)
text_2 = text_2[len(char) :]
path_1 = list_nodes[current_idx].path_in_tree
path_2 = list_nodes[current_idx + 1].path_in_tree
start_diff_path = 0
for i in range(min(len(path_1), len(path_2))):
if path_1[i] != path_2[i]:
start_diff_path = i
break
for tag, _ in path_1[start_diff_path:] + path_2[start_diff_path:]:
if tag in TAG_TO_SEP:
seps.add(TAG_TO_SEP[tag])
if "\n\n" in seps:
sep = "\n\n"
elif "\n" in seps:
sep = "\n"
elif " " in seps:
sep = " "
else:
sep = ""
list_nodes[current_idx].path_in_tree = path_2
list_nodes[current_idx].text = text_1 + sep + text_2
del list_nodes[current_idx + 1]
list_nodes = [
node for node in list_nodes if (node.tag != "-text") or ((node.tag == "-text") and (node.text.strip()))
]
return list_nodes