-
Notifications
You must be signed in to change notification settings - Fork 1
/
BlockRule.py
491 lines (445 loc) · 14.7 KB
/
BlockRule.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
class BlockRule:
threshold = 40000
@staticmethod
def initialize(passed_nodeList):
global nodeList
nodeList = passed_nodeList
@staticmethod
def dividable(block):
box = block.boxs[0]
#print(box.tag_name)
if box.nodeType == 3:
return False
name = box.nodeName
if(name == "img"):
return False
if not BlockRule.isBlock(name):
return BlockRule.inlineRules(block)
elif (name == 'table'):
return BlockRule.tableRules(block)
elif (name == 'tr'):
return BlockRule.trRules(block)
elif (name == 'td'):
return BlockRule.tdRules(block)
elif (name == 'p'):
return BlockRule.pRules(block)
else:
return BlockRule.otherRules(block)
@staticmethod
def otherRules(block):
if BlockRule.rule1(block):
return True
if BlockRule.rule2(block):
return True
if BlockRule.rule3(block):
return True
if BlockRule.rule4(block):
return True
if BlockRule.rule6(block):
return True
if BlockRule.rule7(block):
return True
if BlockRule.rule9(block):
return True
if BlockRule.rule10(block):
return True
if BlockRule.rule12(block):
return True
return False
@staticmethod
def pRules(block):
if BlockRule.rule1(block):
return True
if BlockRule.rule2(block):
return True
if BlockRule.rule3(block):
return True
if BlockRule.rule4(block):
return True
if BlockRule.rule5(block):
return True
if BlockRule.rule6(block):
return True
if BlockRule.rule7(block):
return True
if BlockRule.rule9(block):
return True
if BlockRule.rule10(block):
return True
if BlockRule.rule12(block):
return True
return False
@staticmethod
def tdRules(block):
if BlockRule.rule1(block):
return True
if BlockRule.rule2(block):
return True
if BlockRule.rule3(block):
return True
if BlockRule.rule4(block):
return True
if BlockRule.rule9(block):
return True
if BlockRule.rule10(block):
return True
if BlockRule.rule11(block):
return True
if BlockRule.rule13(block):
return True
return False
@staticmethod
def trRules(block):
if BlockRule.rule1(block):
return True
if BlockRule.rule2(block):
return True
if BlockRule.rule3(block):
return True
if BlockRule.rule7(block):
return True
if BlockRule.rule8(block):
return True
if BlockRule.rule10(block):
return True
if BlockRule.rule13(block):
return True
return False
@staticmethod
def tableRules(block):
if BlockRule.rule1(block):
return True
if BlockRule.rule2(block):
return True
if BlockRule.rule3(block):
return True
if BlockRule.rule8(block):
return True
if BlockRule.rule10(block):
return True
if BlockRule.rule13(block):
return True
return False
@staticmethod
def inlineRules(block):
if BlockRule.rule1(block):
return True
if BlockRule.rule2(block):
return True
if BlockRule.rule3(block):
return True
if BlockRule.rule4(block):
return True
if BlockRule.rule5(block):
return True
if BlockRule.rule6(block):
return True
if BlockRule.rule7(block):
return True
if BlockRule.rule9(block):
return True
if BlockRule.rule10(block):
return True
if BlockRule.rule12(block):
return True
return False
"""
Rule 1: If the DOM node is not a text node and it has no valid children,
then this node cannot be divided and will be cut.
"""
@staticmethod
def rule1(block):
node = block.boxs[0]
if not BlockRule.isTextNode(node) and not BlockRule.hasValidChildNode(node):
#question
block.parent.children.remove(block)
return False
return True
"""
Rule 2:If the DOM node has only one valid child and the child is not a text node,
then divide this node..
"""
@staticmethod
def rule2(block):
if(len(block.children) == 1):
node = block.children[0].boxs[0]
if(BlockRule.isValidNode(node) and not BlockRule.isTextNode(node)):
return True
return False
"""
Rule 3: If the DOM node is the root node of the sub-DOM tree (corresponding to the block),
and there is only one sub DOM tree corresponding to this block, divide this node.
"""
@staticmethod
def rule3(block):
node = block.boxs[0]
#question
result = True
cnt = 0
for WpsdbBlock in block.children:
if WpsdbBlock.boxs[0].nodeName == node.nodeName:
result = True
BlockRule.isOnlyOneDomSubTree(node, WpsdbBlock.boxs[0],result)
if result:
cnt+=1
return True if cnt == 1 else False
"""
Rule 4: If all of the child nodes of the DOM node are text nodes or virtual text nodes,
do not divide the node.
If the font size and font weight of all these child nodes are same,
set the DoC of the extracted block to 10.
Otherwise, set the DoC of this extracted block to 9.
"""
@staticmethod
def rule4(block):
node = block.boxs[0]
subBoxList = node.childNodes
count = 0
for box in subBoxList:
if(BlockRule.isTextNode(box) or BlockRule.isVirtualTextNode(box)):
count+=1
if(count == len(subBoxList)):
fontSize = 0
for box in subBoxList:
childSize = box.visual_cues['font-size']
if fontSize != 0:
if fontSize != childSize:
block.Doc = 9
return False
else:
fontSize = childSize
fontWeight = None
for box in subBoxList:
childWeight = box.visual_cues['font-weight']
if fontWeight != None:
if fontWeight != childWeight:
block.Doc = 9
return False
else:
fontWeight = childWeight
block.Doc = 10
return False
return True
"""
Rule 5: If one of the child nodes of the DOM node is line-break node, then divide this DOM node
"""
@staticmethod
def rule5(block):
node = block.boxs[0]
subBoxList = node.childNodes
for box in subBoxList:
#question
if not BlockRule.isBlock(box.nodeName):
return True
return False
"""
Rule 6: If one of the child nodes of the DOM node has HTML tag <HR>,
then divide this DOM node
"""
@staticmethod
def rule6(block):
node = block.boxs[0]
subBoxList = node.childNodes
for box in subBoxList:
if box.nodeName == 'hr':
return True
return False
"""
Rule 7: If the sum of all the child nodes' size is greater than this DOM node's size,
then divide this node.
"""
@staticmethod
def rule7(block):
node = block.boxs[0]
x = node.visual_cues['bounds']['x']
y = node.visual_cues['bounds']['y']
width = node.visual_cues['bounds']['width']
height = node.visual_cues['bounds']['height']
subBoxList = node.childNodes
#question
for box in subBoxList:
if(box.visual_cues['bounds']['x']< x):
return True
if(box.visual_cues['bounds']['y'] < y):
return True
if((x+width) < (box.visual_cues['bounds']['x'] + box.visual_cues['bounds']['width'] )):
return True
if((y+height) < (box.visual_cues['bounds']['y'] + box.visual_cues['bounds']['height'])):
return True
return False
"""
Rule 8: If the background color of this node is different from one of its children's,
divide this node and at the same time,
the child node with different background color will not be divided in this round.
Set the DoC value (6-8) for the child node based on the html tag of the child node and the size of the child node.
"""
@staticmethod
def rule8(block):
ret = False
node = block.boxs[0]
bColor = node.visual_cues['background-color']
for b in block.children:
child = b.boxs[0]
childColor = child.visual_cues['background-color']
if bColor != childColor:
b.isDividable = False
b.Doc = BlockRule.getDocByTagSize("",0)
ret = True
return ret
"""
Rule 9: If the node has at least one text node child or at least one virtual text node child,
and the node's relative size is smaller than a threshold, then the node cannot be divided
Set the DoC value (from 5-8) based on the html tag of the node
"""
@staticmethod
def rule9(block):
ret = True
node = block.boxs[0]
subBoxList = node.childNodes
count = 0
for box in subBoxList:
if(BlockRule.isTextNode(box) or BlockRule.isVirtualTextNode(box)):
count+=1
if count > 0:
if (node.visual_cues['bounds']['x'] * node.visual_cues['bounds']['y'] < BlockRule.threshold):
ret = False
block.Doc = BlockRule.getDocByTagSize("",0)
return ret
"""
Rule 10: If the child of the node with maximum size are smaller than a threshold (relative size),
do not divide this node.
Set the DoC based on the html tag and size of this node.
"""
@staticmethod
def rule10(block):
node = block.boxs[0]
subBoxList = node.childNodes
maxSize = 0
for box in subBoxList:
childSize = box.visual_cues['bounds']['x'] * box.visual_cues['bounds']['y']
maxSize = childSize if maxSize < childSize else maxSize
if maxSize < BlockRule.threshold:
block.Doc = BlockRule.getDocByTagSize("",0)
return False
return True
"""
Rule 11: If previous sibling node has not been divided, do not divide this node
"""
@staticmethod
def rule11(block):
children = block.parent.children
index = children.index(block)
count = 0
for i in range(0,index):
if not children[i].isDividable:
count+=1
return not count == index
"""
Rule 12: Divide this node.
"""
@staticmethod
def rule12(block):
return True
"""
Rule 13: Do not divide this node
Set the DoC value based on the html tag and size of this node.
"""
@staticmethod
def rule13(block):
block.Doc = BlockRule.getDocByTagSize("",0)
return False
@staticmethod
def hasValidChildNode(node):
subBoxList = node.childNodes
for box in subBoxList:
if(BlockRule.isValidNode(box)):
return True
return False
"""
a node that can be seen through the browser.
The node's width and height are not equal to zero.
"""
@staticmethod
def isValidNode(node):
display = node.visual_cues['display']
visibility = node.visual_cues['visibility']
if display == 'none' or visibility == 'hidden':
return False
height = node.visual_cues['bounds']['height']
width = node.visual_cues['bounds']['width']
if height == '0px' or width == '0px':
return False
return True
"""
the DOM node corresponding to free text, which does not have an html tag
"""
@staticmethod
def isTextNode(node):
return node.nodeType == 3
"""
Virtual text node (recursive definition):
Inline node with only text node children is a virtual text node.
Inline node with only text node and virtual text node children is a virtual text node.
"""
@staticmethod
def isVirtualTextNode(node):
subBoxList = node.childNodes
count = 0
for box in subBoxList:
if(BlockRule.isTextNode(box) or BlockRule.isVirtualTextNode(box)):
count+=1
if(count == len(subBoxList)):
return True
return False
@staticmethod
def isBlock(name):
if (name == 'a' or
name == 'abbr' or
name == 'acronym' or
name == 'b' or
name == 'bdo' or
name == 'big' or
name == 'br' or
name == 'button' or
name == 'cite' or
name == 'code' or
name == 'dfn' or
name == 'em' or
name == 'i' or
name == 'img' or
name == 'input' or
name == 'kbd' or
name == 'label' or
name == 'map' or
name == 'object' or
name == 'q' or
name == 'samp' or
name == 'script' or
name == 'select' or
name == 'small' or
name == 'span' or
name == 'strong' or
name == 'sub' or
name == 'sup' or
name == 'textarea' or
name == 'time' or
name == 'tt' or
name == 'var'):
return False
else:
return True
@staticmethod
def isOnlyOneDomSubTree(pattern, node, result):
if pattern.nodeName != node.nodeName:
result = False
pattern_child = pattern.childNodes
node_child = node.childNodes
if len(pattern_child) != len(node_child):
result = False
if not result:
return
for i in range(0,len(pattern_child)):
BlockRule.isOnlyOneDomSubTree(pattern_child[i],node_child[i],result)
@staticmethod
def getDocByTagSize(tag, size):
return 7