Skip to content

fix(links): match "ad" as a whole class word in link scoring - #2633

Open
Yasser-Ameur wants to merge 2 commits into
unclecode:developfrom
Yasser-Ameur:fix/link-score-class-tokens
Open

Yasser-Ameur wants to merge 2 commits into
unclecode:developfrom
Yasser-Ameur:fix/link-score-class-tokens

Conversation

@Yasser-Ameur

@Yasser-Ameur Yasser-Ameur commented Nov 26, 2026 •

Copy link
Copy Markdown

Summary

With score_links=True, a link loses a point of intrinsic score whenever its
class merely contains the letters "ad". breadcrumb, header-link, badge,
download-btn, loading and read-more are all scored like an ad-slot
link:

from crawl4ai.utils import calculate_link_intrinsic_score

ctx = {"terms": set(), "is_docs_site": False}
for cls in ["", "breadcrumb", "header-link", "badge", "ad-slot"]:
    print(repr(cls), calculate_link_intrinsic_score(
        link_text="Getting started guide", url="https://example.com/docs/start",
        title_attr="", class_attr=cls, rel_attr="", page_context=ctx))
# develop: 5.0, 4.0, 4.0, 4.0, 4.0
# here:    5.0, 5.0, 5.0, 5.0, 4.0

calculate_link_intrinsic_score tested 'ad' in class_str, a substring check
on the whole class attribute. "ad" is now matched as a class word: words are
split on whitespace, - and _, and camelCase is split before lowercasing.

Still penalised: ad, ads* (adsense, adsbygoogle, adsbox), adv,
advert*, ad followed by digits or a size (ad1, ad300x250), and ad
followed by box, slot, unit, wrap, holder, zone, block, space, container,
server, mob, frame, words, tech or x. So ad-slot, sidebar-ad, ad1,
adContainer, googleAd and adwrap score as before. The sponsor, track,
promo and banner checks are unchanged.

Two limits. An ad class with a run-together suffix that is not in that list is
no longer penalised. Neither is a word that ends in "ad" (textad): matching
those would bring back head, load and bread, which is the bug.

List of files changed and why

  • crawl4ai/utils.py - match "ad" as a class word in the link class penalty.
  • tests/unit/test_link_intrinsic_score_classes.py - new, offline.

How Has This Been Tested?

pytest tests/unit/test_link_intrinsic_score_classes.py is 16 passed on this
branch and 8 failed, 8 passed with utils.py from develop; the 8 failures are
the classes that only contain the letters.

70 class strings were scored on develop and on this branch. 26 change, all of
them words that only contain "ad"; every ad, sponsor, tracking, promo and banner
class scores the same on both.

The wider run over the utils, link scoring and URL tests is 135 passed, 5
failed, the 5 being tests/test_normalize_url.py failures that are the same on
develop. The async and browser tests were not run.

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added/updated unit tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Links with a class such as breadcrumb, header-link, download-btn, badge,
shadow, loading or gradient lost a point of intrinsic score, the same as a
real ad-slot link.

The penalty checked whether the lowercased class string contained "ad" as a
substring, so any longer word with those two letters inside matched.

"ad" is now matched as a whole class word. Words split on whitespace, hyphen
and underscore, and camelCase is split before lowercasing. Still penalised:
ad, ads, adsense, adsbygoogle, adv, advert*, and ad followed by digits, box,
slot, unit, wrap*, holder, zone, block, space, container, server or mob, so
ad-slot, sidebar-ad, ad1, adContainer, googleAd and adwrap keep their
penalty. No longer penalised: classes that only contain the letters, and
ad-hoc and ad-free. The sponsor, track, promo and banner checks are
unchanged. Adds offline tests for both cases.
@Yasser-Ameur
Yasser-Ameur marked this pull request as ready for review November 26, 2026 15:36

@SoulmatelynchVFX SoulmatelynchVFX left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

方向没问题,测试也覆盖了正文举的那批类名,但抽出来逐条对比时有两个边界和「ad 是独立的类名单词」这条规则对不上。

第一,_AD_CLASS_RE 只认「ad 出现在词首」((?:^|[\s_-]))加上一份后缀白名单,于是 textad、mobilead 这种把 ad 挂在词尾的类名,以及 adwords、adtech、adframe 这种「ad + 不在名单里的后缀」都不再扣分(改前 'ad' in class_str 会扣)。其中 ad300x250 与正文「ad followed by digits」的说法有出入:\d+ 后面紧跟 (?![^\s_-]),数字后接 x 就整条失配,而 ad300x250 是常见的广告位命名。正文只列了 adframe 一处已知退化,实际范围比这大。

第二,ad-free / ad-hoc 的豁免用 \b 收尾,混用分隔符时会失效:ad-free_cta、ad-hoc_test 里 e 与 _ 之间没有词边界,负向前瞻不成立,于是被当成广告类扣分;ad-free-cta 和 ad_free 都正常。把豁免改成 (?![\s_-](?:hoc|free)(?=$|[\s_-])) 可以让这四种写法一致。

验证方式:只把 _AD_CLASS_RE、新加的 re.sub 那行和 search 取出来,与 develop 的 'ad' in class_str.lower() 逐条对比同一组类名;['textad','adwords','adtech','ad300x250','adsbox','adx','adframe'] 改前命中、改后不命中,非广告类名(breadcrumb、header-link、badge、download-btn、loading、read-more、headline、shadow、upload)在改前改后都不命中。没跑仓内 pytest(本机没装该仓依赖),也没走 score_links=False 那条分支。

这条评论由 AI 辅助生成,上述结论以本机正则对比输出为准,未经维护者复核。

@Yasser-Ameur

Copy link
Copy Markdown
Author

Thanks for going through it class by class. Pushed 6f3f71a for the two points that hold:

  • Sizes and a few more run-together names now count as ad classes: ad300x250, adsbox (any ads* word), adframe, adwords, adtech, adx.
  • The ad-free / ad-hoc exemption no longer depends on \b, so ad-free_cta and ad-hoc_test are left alone like ad-free-cta and ad_free.

Both sets are in the tests, together with address, admin and add-to-cart as controls.

Not changed: words that end in "ad", such as textad or mobilead. Matching those is the same as matching head, load and bread, which is the bug this PR fixes. On develop, 'ad' in class_str does hit breadcrumb, header-link, badge, download-btn, loading, read-more, headline, shadow and upload, so those are not unaffected before the change. The PR description now lists both limits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants