Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions llm_web_kit/html_layout/html_layout_cosin.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ def __get_children(el_lst: List[HtmlElement], layer_n: int, tag_attr: Dict):
parent_tag_attr.add(f'<{tag}>')
else:
attrs_str = __parse_attributes(child)
if attrs_str is not None:
parent_tag_attr.add(f'<{tag} {attrs_str}>' if attrs_str else f'<{tag}>')
parent_tag_attr.add(f'<{tag} {attrs_str}>' if attrs_str else f'<{tag}>')

el_tag_attr.append(parent_tag_attr)

Expand All @@ -130,15 +129,15 @@ def __parse_attributes(element: HtmlElement):
class_d = class_attr.split()
if len(class_d) > 1:
class_s = ' '.join([i for i in class_d if not RE_NUM.search(i)])
else:
elif len(class_d) == 1:
class_s = __standardizing_dynamic_attributes(class_d[0])

id_attr = element.get('id')
if id_attr:
id_d = id_attr.split()
if len(id_d) > 1:
id_s = ' '.join([i for i in id_d if not RE_NUM.search(i)])
else:
elif len(id_d) == 1:
id_s = __standardizing_dynamic_attributes(id_d[0])

if not class_s and not id_s:
Expand Down Expand Up @@ -269,7 +268,7 @@ def __parse_valid_layer(features: List[Dict]) -> int:
layer_max_l.append(max_key)
counter = Counter(layer_max_l)
layer_n = counter.most_common(1)[0][0]
return layer_n if layer_n > 5 else len(features[0]['tags'])
return layer_n if layer_n > 1 else len(features[0]['tags'])


def cluster_html_struct(sampled_list: List[Dict], threshold=0.95) -> List[Dict]:
Expand Down
7 changes: 7 additions & 0 deletions tests/llm_web_kit/html_layout/test_html_layout_cosin.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
4: ['div', 'div', 'header', 'article', 'aside', 'div', 'a', 'div', 'div']}},
'expected': {'layer_n': {1: 1, 2: 5, 3: 7, 4: 9}, 'total_n': 22}}
]
TEST_CLUSTER_TEST = [
{'feature': {'tags': {'2': ['img', 'a']}}, 'layer_n': {'2': 2}, 'total_n': 2},
{'feature': {'tags': {'2': ['img', 'a']}}, 'layer_n': {'2': 2}, 'total_n': 2},
{'feature': {'tags': {'2': ['img', 'a']}}, 'layer_n': {'2': 2}, 'total_n': 2}
]


class TestHtmllayoutcosin(unittest.TestCase):
Expand All @@ -72,6 +77,8 @@ def test_cluster_html_struct(self):
TEST_CLUSTER_HTML['input']]
res, layout_list = cluster_html_struct(features)
self.assertEqual(TEST_CLUSTER_HTML['expected'], layout_list)
res, layout_list = cluster_html_struct(TEST_CLUSTER_TEST)
self.assertEqual(layout_list, [0])

def test_similarity(self):
for TEST_SIMIL_HTML in TEST_SIMIL_HTMLS:
Expand Down
Loading