SPB Git

Repository Public

test_extract_boilerplate.py 4.6 KB 101 lines python

1# =============================================================================2#                  SP BOUCHER | SOFTWARE & RESEARCH3#             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4# =============================================================================5#6# Project          : SPIDER — Universal Web Intelligence Engine7# File             : tests/unit/extract/test_extract_boilerplate.py8# Module           : Tests9# Repository       : https://git.spboucher.ai/spider10# Project Website  : Not specified11# Created          : 2026-12-0412# Last Modified    : 2026-12-0413#14# Author           : Simon-Pierre Boucher15# Position         : Professor of Finance16# Institution      : Université du Québec en Outaouais (UQO)17# Organization     : SP Boucher18# Website          : https://spboucher.ai19# Git              : https://git.spboucher.ai20#21# Preferred Contact: contact@spboucher.ai22# Development      : dev@spboucher.ai23# Support          : support@spboucher.ai24# Security/Admin   : admin@spboucher.ai25#26# Copyright (c) 202627# Simon-Pierre Boucher28# All rights reserved where applicable. This file is governed by the29# applicable project license, copyright notices and relevant law.30# Existing third-party rights and notices remain unaffected.31#32# License          : ISC33#34# Do not embed credentials, private keys, access tokens,35# certificates or confidential personal information.36#37# =============================================================================38# END OF SP BOUCHER SOURCE HEADER39# =============================================================================4041"""Block classification and non-destructive main-content extraction."""42from spider.extract.boilerplate import classify_blocks, main_content4344PAGE = """<html><body>45<header class="site-header"><a href="/">Logo</a></header>46<nav><a href="/a">A</a><a href="/b">B</a><a href="/c">C</a></nav>47<div class="ad-slot"><a href="https://ads.example/x">Achetez maintenant</a></div>48<main><article><h1>Titre de l'article</h1>49<p>Premier paragraphe avec assez de texte pour être considéré comme du contenu principal de la page.</p>50<div class="share-buttons"><a href="#">Partager</a></div>51<ins class="adsbygoogle" data-ad-client="ca-pub-1"></ins>52<p>Second paragraphe, lui aussi dans l'article, avec un <a href="/lien">lien</a> isolé.</p></article></main>53<aside><p>Articles liés</p></aside>54<div class="cookie-banner"><p>Nous utilisons des témoins.</p></div>55<footer><p>© 2026 Exemple inc.</p></footer></body></html>"""565758def _by_category(blocks):59    out = {}60    for block in blocks:61        out.setdefault(block.category, []).append(block)62    return out636465def test_classification_categories():66    cats = _by_category(classify_blocks(PAGE))67    assert {b.tag for b in cats["main"]} == {"h1", "p"}68    assert len(cats["main"]) == 369    assert {b.tag for b in cats["navigation"]} == {"nav"}70    assert {b.tag for b in cats["advertising"]} == {"div", "ins"}71    boiler = {(b.tag, b.reasons[0]) for b in cats["boilerplate"]}72    assert ("header", "semantic:header") in boiler and ("footer", "semantic:footer") in boiler73    assert ("aside", "semantic:aside") in boiler and ("div", "boilerplate_hint") in boiler747576def test_main_content_is_a_copy_and_original_is_untouched():77    original = str(PAGE)78    main_html, report = main_content(PAGE)79    assert PAGE == original80    assert "Premier paragraphe" in main_html and "Second paragraphe" in main_html81    assert "Achetez maintenant" not in main_html and "témoins" not in main_html and "Logo" not in main_html82    assert all(block.selector and block.reasons for block in report)83    assert {b.category for b in report} == {"main", "navigation", "advertising", "boilerplate"}848586def test_link_density_and_repeated_text_without_semantic_tags():87    html = """<body><div><a href="/1">Un</a> <a href="/2">Deux</a> <a href="/3">Trois</a> <a href="/4">Quatre</a></div>88    <div><p>Un long paragraphe de contenu, sans balise sémantique autour, qui doit rester du contenu.</p>89    <p>Ajouter au panier</p><p>Ajouter au panier</p><p>Ajouter au panier</p></div></body>"""90    blocks = classify_blocks(html)91    assert blocks[0].category == "navigation" and blocks[0].reasons == ["link_density"]92    assert blocks[1].category == "main" and blocks[1].reasons == ["text_block"]93    assert [b.reasons for b in blocks[2:]] == [["repeated_text"]] * 3949596def test_degenerate_inputs():97    assert main_content("")[1] == []98    main_html, report = main_content("<p>Texte seul, sans structure, mais assez long pour compter comme contenu.</p>")99    assert "Texte seul" in main_html and report[0].category == "main"100    assert classify_blocks("<<<>>>")  # garbage is one text block, no exception101
Files main
Menu

Theme