diff --git a/pyrit/common/text_matching.py b/pyrit/common/text_matching.py index c1bd98d6ff..5c90e9b0e5 100644 --- a/pyrit/common/text_matching.py +++ b/pyrit/common/text_matching.py @@ -94,7 +94,8 @@ class ApproximateTextMatching(TextMatching): This strategy computes the proportion of character n-grams from the target that are present in the text. Useful for detecting partial matches, encoded - content, or text with variations. + content, or text with variations. A target shorter than ``n`` is compared as a + single n-gram, so it scores 1.0 when the text contains it and 0.0 otherwise. By default coverage is computed over the *distinct* target n-grams, so a short text that contains every distinct n-gram of a long repeated target scores 1.0. Set ``occurrence_sensitive=True`` to @@ -184,16 +185,16 @@ def _calculate_ngram_overlap(self, *, target: str, text: str) -> float: return 0.0 if not target.strip(): return 0.0 - if len(target) < self._n: - return 0.0 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)) if self._occurrence_sensitive: - return self._calculate_occurrence_overlap(target=target_str, text=text_str) + return self._calculate_occurrence_overlap(target=target_str, text=text_str, n=n) - target_ngrams = {target_str[i : i + self._n] for i in range(len(target_str) - (self._n - 1))} + target_ngrams = {target_str[i : i + n] for i in range(len(target_str) - (n - 1))} if not target_ngrams: return 0.0 @@ -201,19 +202,20 @@ def _calculate_ngram_overlap(self, *, target: str, text: str) -> float: matching_ngrams = sum(int(ngram in text_str) for ngram in target_ngrams) return matching_ngrams / len(target_ngrams) - def _calculate_occurrence_overlap(self, *, target: str, text: str) -> float: + def _calculate_occurrence_overlap(self, *, target: str, text: str, n: int) -> float: """ Calculate occurrence-sensitive n-gram coverage of the target by the text. Args: target (str): The normalized target string (at least ``n`` characters). text (str): The normalized text to search in. + n (int): The n-gram length to use. Returns: float: ``sum(min(target_count, text_count)) / total target windows``. """ - target_counts = Counter(target[i : i + self._n] for i in range(len(target) - (self._n - 1))) - text_counts = Counter(text[i : i + self._n] for i in range(len(text) - (self._n - 1))) + target_counts = Counter(target[i : i + n] for i in range(len(target) - (n - 1))) + text_counts = Counter(text[i : i + n] for i in range(len(text) - (n - 1))) matched = sum(min(count, text_counts[ngram]) for ngram, count in target_counts.items()) return matched / sum(target_counts.values()) diff --git a/tests/unit/analytics/test_text_matching.py b/tests/unit/analytics/test_text_matching.py index 2fa685f0ca..915e459d47 100644 --- a/tests/unit/analytics/test_text_matching.py +++ b/tests/unit/analytics/test_text_matching.py @@ -93,10 +93,30 @@ def test_different_n_values(self): assert matcher_n4.is_match(target=target, text=text) is True def test_target_too_short(self): - # Target shorter than n should return False + # A target shorter than n that does not appear in the text is not a match matcher = ApproximateTextMatching(threshold=0.5, n=3) assert matcher.is_match(target="hi", text="hello world") is False + @pytest.mark.parametrize("occurrence_sensitive", [False, True]) + def test_target_shorter_than_n_matches_when_contained(self, occurrence_sensitive): + # A target shorter than n has no n-grams, so it is compared as a single gram: + # it scores 1.0 when the text contains it and 0.0 otherwise. + matcher = ApproximateTextMatching(threshold=0.5, n=3, occurrence_sensitive=occurrence_sensitive) + assert matcher.get_overlap_score(target="ok", text="ok, here it is") == 1.0 + assert matcher.is_match(target="ok", text="ok, here it is") is True + assert matcher.get_overlap_score(target="OK", text="ok, here it is") == 1.0 + assert matcher.get_overlap_score(target="no", text="ok, here it is") == 0.0 + assert matcher.is_match(target="no", text="ok, here it is") is False + + def test_target_shorter_than_n_respects_case_sensitivity(self): + matcher = ApproximateTextMatching(threshold=0.5, n=4, case_sensitive=True) + assert matcher.is_match(target="abc", text="xabcx") is True + assert matcher.is_match(target="ABC", text="xabcx") is False + + def test_target_shorter_than_n_and_text_shorter_than_target(self): + matcher = ApproximateTextMatching(threshold=0.5, n=3, occurrence_sensitive=True) + assert matcher.get_overlap_score(target="ab", text="a") == 0.0 + def test_empty_text(self): matcher = ApproximateTextMatching(threshold=0.5, n=3) assert matcher.is_match(target="hello", text="") is False