Skip to content

Commit 4ca8623

Browse files
committed
_expand_intents: round-robin across patterns, dedupe, raise cap to 256
1 parent ffab626 commit 4ca8623

2 files changed

Lines changed: 37 additions & 12 deletions

File tree

custom_components/closest_intent/const.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def _read_version() -> str:
4646
SERVICE_PARSE = "parse_sentence"
4747

4848
# Hard ceiling on candidates kept per intent after pattern expansion.
49-
PER_INTENT_CANDIDATE_CAP = 32
49+
PER_INTENT_CANDIDATE_CAP = 256
5050

5151
# Marker substituted in for `{slot}` placeholders during pattern expansion.
5252
# Matched as a wildcard during scoring; mined out for slot extraction.

custom_components/closest_intent/conversation.py

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -489,15 +489,31 @@ def _expand_intents(
489489
intents: dict[str, list[str]],
490490
resolver: Resolver,
491491
) -> list[Candidate]:
492+
"""Expand ``intents`` into candidates, round-robin across sentence patterns."""
492493
candidates: list[Candidate] = []
493494
for intent_name, patterns in intents.items():
494-
kept = 0
495+
per_pattern: list[tuple[int, list[tuple[str, str, list[str]]]]] = []
495496
for idx, pat in enumerate(patterns):
496-
if kept >= PER_INTENT_CANDIDATE_CAP:
497-
break
498-
for text, display_text, slot_names in expand_pattern(
499-
pat, self._expansion_cap, resolver=resolver
500-
):
497+
forms = list(expand_pattern(pat, self._expansion_cap, resolver=resolver))
498+
if forms:
499+
per_pattern.append((idx, forms))
500+
if not per_pattern:
501+
continue
502+
503+
# Dedupe across patterns within this intent.
504+
seen_texts: set[str] = set()
505+
kept = 0
506+
depth = 0
507+
while kept < PER_INTENT_CANDIDATE_CAP:
508+
progress = False
509+
for idx, forms in per_pattern:
510+
if depth >= len(forms):
511+
continue
512+
text, display_text, slot_names = forms[depth]
513+
progress = True
514+
if text in seen_texts:
515+
continue
516+
seen_texts.add(text)
501517
candidates.append(
502518
Candidate(
503519
intent=intent_name,
@@ -509,12 +525,21 @@ def _expand_intents(
509525
)
510526
kept += 1
511527
if kept >= PER_INTENT_CANDIDATE_CAP:
512-
_LOGGER.debug(
513-
"closest_intent: %s hit per-intent cap (%d), truncating",
514-
intent_name,
515-
PER_INTENT_CANDIDATE_CAP,
516-
)
517528
break
529+
if not progress:
530+
break
531+
depth += 1
532+
533+
total = sum(len(forms) for _, forms in per_pattern)
534+
if kept < total:
535+
_LOGGER.debug(
536+
"closest_intent: %s hit per-intent cap (%d) or duplicate ceiling, "
537+
"kept %d of %d generated expansions",
538+
intent_name,
539+
PER_INTENT_CANDIDATE_CAP,
540+
kept,
541+
total,
542+
)
518543
return candidates
519544

520545
def _build_resolver(

0 commit comments

Comments
 (0)