Repository navigation
FIX: Match a target shorter than n in ApproximateTextMatching - #4820
Open
Shivam Sharma (ShivamSharma43) wants to merge 1 commit into
Open
Shivam Sharma (ShivamSharma43) wants to merge 1 commit into
Shivam Sharma (ShivamSharma43) wants to merge 1 commit into
Conversation
A target with fewer than n characters has no n-grams, so the overlap score was always 0.0 and is_match was always False, even when the text contained the target verbatim. Compare such a target as a single n-gram (n = len(target)) in both coverage modes, so it scores 1.0 when the text contains it and 0.0 otherwise. Fixes microsoft#4819 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
A target shorter than n should be treated as one gram, but Unicode expansion during lowercasing can split it into multiple grams. With n=3, target="İİ" and text="İi", both coverage modes return 0.5 and report a match at the default threshold even though the full normalized target is absent.
1 open issue
What changed in this PR
Addresses #4819 by enabling PyRIT’s approximate text matcher to match targets shorter than the configured n-gram length.
Changes:
- Adjusts n-gram sizing in both coverage modes.
- Documents short-target behavior and adds regression tests.
| File | Description |
|---|---|
| tests/unit/analytics/test_text_matching.py | Tests short-target matches, absent targets, case sensitivity, and shorter text. |
| pyrit/common/text_matching.py | Adjusts n-gram sizing and documents the behavior. |
Review effort: Balanced
| target_str = target if self._case_sensitive else target.lower() | ||
| text_str = text if self._case_sensitive else text.lower() | ||
| # A target shorter than n has no n-grams of its own; compare it as a single gram. | ||
| n = min(self._n, len(target_str)) |
This branch has not been deployed
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.

Description
ApproximateTextMatchingreturned an overlap of 0.0 for any target shorter thann, sois_matchwas always False for it, even when the text contained the target verbatim. With the defaultn=3,SubStringScorer(substring="ok", text_matcher=ApproximateTextMatching())could never return True. This PR compares a target like that as a single n-gram.len(target) < nearly return._calculate_ngram_overlapnow usesn = min(self._n, len(target)), so a short target scores 1.0 when the text contains it and 0.0 when it doesn't. Targets of at leastncharacters are scored exactly as before.n(passed to_calculate_occurrence_overlap).case_sensitive=True. The existingtest_target_too_shortcase ("hi"against"hello world") still expects no match. Only its comment changes.Before / after with
ApproximateTextMatching(threshold=0.5, n=3):Fixes #4819
🤖 Generated with Claude Code