Repository navigation
fix(links): match "ad" as a whole class word in link scoring - #2633
Yasser-Ameur wants to merge 2 commits into
Conversation
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.
SoulmatelynchVFX
left a comment
There was a problem hiding this comment.
方向没问题,测试也覆盖了正文举的那批类名,但抽出来逐条对比时有两个边界和「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_-])) 可以让这四种写法一致。
验证方式:只把 _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 辅助生成,上述结论以本机正则对比输出为准,未经维护者复核。
|
Thanks for going through it class by class. Pushed 6f3f71a for the two points that hold:
Both sets are in the tests, together with Not changed: words that end in "ad", such as |
Summary
With
score_links=True, a link loses a point of intrinsic score whenever itsclass merely contains the letters "ad".
breadcrumb,header-link,badge,download-btn,loadingandread-moreare all scored like anad-slotlink:
calculate_link_intrinsic_scoretested'ad' in class_str, a substring checkon 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*,adfollowed by digits or a size (ad1,ad300x250), andadfollowed by box, slot, unit, wrap, holder, zone, block, space, container,
server, mob, frame, words, tech or x. So
ad-slot,sidebar-ad,ad1,adContainer,googleAdandadwrapscore as before. Thesponsor,track,promoandbannerchecks 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): matchingthose would bring back
head,loadandbread, 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.pyis 16 passed on thisbranch and 8 failed, 8 passed with
utils.pyfromdevelop; the 8 failures arethe classes that only contain the letters.
70 class strings were scored on
developand on this branch. 26 change, all ofthem 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.pyfailures that are the same ondevelop. The async and browser tests were not run.Checklist: