Repository navigation
FIX: Match a target shorter than n in ApproximateTextMatching - #4820
Shivam Sharma (ShivamSharma43) wants to merge 2 commits 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>
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)) |
There was a problem hiding this comment.
Fixed in 056f8f2. The target's length is now checked before lowercasing, and a short target uses its whole lowercased form as the single gram. So "İİ" against "İi" scores 0.0 in both coverage modes. I added a test for that case, for a near miss, and for the contained case.
Lowercasing can lengthen a string: "İ" becomes "i" plus a combining dot, so the two-character target "İİ" became four characters and was split into n-grams again, scoring 0.5 against "İi". Check the target's length before lowercasing and use the whole lowercased target as the single gram. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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