Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/insight_extractor/stemmer.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def finditer(self, text: str) -> Iterator[re.Match[str]]:
matches: list[re.Match[str]] = []
for pattern in self.patterns:
matches.extend(pattern.finditer(text))
yield from sorted(matches, key=lambda match: (match.start(), match.end()))
matches.sort(key=lambda match: (match.start(), match.end()))
yield from matches


type KeywordPattern = re.Pattern[str] | ChunkedKeywordPattern
Expand Down
29 changes: 28 additions & 1 deletion tests/unit/test_stemmer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,38 @@
import pytest

from insight_extractor.config import StemMode
from insight_extractor.stemmer import DynamicKeywordStemmer, KeywordPatternRegistry
from insight_extractor.stemmer import (
ChunkedKeywordPattern,
DynamicKeywordStemmer,
KeywordPatternRegistry,
)

# ── DynamicKeywordStemmer tests ──────────────────────────────────────────────


@pytest.mark.parametrize("text", ["", "no matches", "alpha beta alpha"])
def test_chunked_finditer_preserves_span_order_and_tie_priority(text: str) -> None:
patterns = [
re.compile(pattern) for pattern in ("alpha beta", "alpha", "(?:alpha)", "(?=alpha)")
]
matches = list(ChunkedKeywordPattern(patterns).finditer(text))

expected = (
[
((0, 0), "", "(?=alpha)"),
((0, 5), "alpha", "alpha"),
((0, 5), "alpha", "(?:alpha)"),
((0, 10), "alpha beta", "alpha beta"),
((11, 11), "", "(?=alpha)"),
((11, 16), "alpha", "alpha"),
((11, 16), "alpha", "(?:alpha)"),
]
if text == "alpha beta alpha"
else []
)
assert [(match.span(), match.group(), match.re.pattern) for match in matches] == expected


class TestInit:
"""Construction and default attributes."""

Expand Down
Loading