Skip to content

FIX: Match a target shorter than n in ApproximateTextMatching - #4820

Open
Shivam Sharma (ShivamSharma43) wants to merge 1 commit into
microsoft:mainfrom
ShivamSharma43:fix/approximate-text-matching-short-target
Open

Shivam Sharma (ShivamSharma43) wants to merge 1 commit into
microsoft:mainfrom
ShivamSharma43:fix/approximate-text-matching-short-target

Conversation

@ShivamSharma43

Copy link
Copy Markdown
Contributor

Description

ApproximateTextMatching returned an overlap of 0.0 for any target shorter than n, so is_match was always False for it, even when the text contained the target verbatim. With the default n=3, SubStringScorer(substring="ok", text_matcher=ApproximateTextMatching()) could never return True. This PR compares a target like that as a single n-gram.

  • Remove the len(target) < n early return. _calculate_ngram_overlap now uses n = 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 least n characters are scored exactly as before.
  • The occurrence-sensitive mode gets the same effective n (passed to _calculate_occurrence_overlap).
  • The class docstring now describes this behaviour.
  • Add tests that a short target matches when the text contains it, in both coverage modes and case-insensitively. Also add tests that it doesn't match when absent or when the case differs and case_sensitive=True. The existing test_target_too_short case ("hi" against "hello world") still expects no match. Only its comment changes.

Before / after with ApproximateTextMatching(threshold=0.5, n=3):

# before
target="ok"  text="ok, here it is"   -> 0.0 False
target="ok!" text="ok!, here it is"  -> 1.0 True

# after
target="ok"  text="ok, here it is"   -> 1.0 True
target="ok!" text="ok!, here it is"  -> 1.0 True

Fixes #4819

🤖 Generated with Claude Code

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>
Copilot AI balanced review requested due to automatic review settings December 3, 2026 07:00

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 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

No deployments
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.

BUG: ApproximateTextMatching never matches a target shorter than n, even verbatim

2 participants