fix: use str.split() for accurate word count in PruningContentFilter#1838
Open
karesansui-u wants to merge 1 commit intounclecode:developfrom
Open
fix: use str.split() for accurate word count in PruningContentFilter#1838karesansui-u wants to merge 1 commit intounclecode:developfrom
karesansui-u wants to merge 1 commit intounclecode:developfrom
Conversation
text.count(" ") + 1 overcounts words when consecutive spaces are
present, which is common in HTML-extracted text from get_text(strip=True).
This causes min_word_threshold checks to be too lenient, allowing
short/noisy content to pass through the filter.
The same file already uses len(text.split()) for the same purpose
at lines 268 and 302.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
_compute_composite_score()usestext.count(" ") + 1to count words, which overcounts when consecutive spaces are present. HTML-extracted text fromget_text(strip=True)commonly contains multiple consecutive spaces between inline elements — each extra space inflates the count by one, makingmin_word_thresholdchecks too lenient and allowing short/noisy nodes to survive pruning.Before (line 742):
After:
The same file already uses
len(text.split())for the identical purpose at line 268 and line 302, so this change also restores internal consistency.Edge case
text.count(" ") + 1returns1for an empty string, whilelen("".split())correctly returns0. This means empty-text nodes that should be removed bymin_word_threshold >= 1currently slip through.Changed files
crawl4ai/content_filter_strategy.py— 1 line in_compute_composite_score()Testing
Verified that
str.split()handles consecutive spaces, tabs, and empty strings correctly: