style_intent.py 14.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
"""
Style intent detection for query understanding.
"""

from __future__ import annotations

from dataclasses import dataclass, field
from typing import Any, Callable, Dict, Iterable, List, Optional, Sequence, Set, Tuple

from .tokenization import QueryTextAnalysisCache, TokenizedText, normalize_query_text, tokenize_text


@dataclass(frozen=True)
class StyleIntentTermDefinition:
    canonical_value: str
    en_terms: Tuple[str, ...]
    zh_terms: Tuple[str, ...]
    attribute_terms: Tuple[str, ...]


@dataclass(frozen=True)
class StyleIntentDefinition:
    intent_type: str
    terms: Tuple[StyleIntentTermDefinition, ...]
    dimension_aliases: Tuple[str, ...]
    en_synonym_to_term: Dict[str, StyleIntentTermDefinition]
    zh_synonym_to_term: Dict[str, StyleIntentTermDefinition]
    max_term_ngram: int = 3

    @classmethod
    def from_rows(
        cls,
        intent_type: str,
        rows: Sequence[Dict[str, List[str]]],
        dimension_aliases: Sequence[str],
    ) -> "StyleIntentDefinition":
        terms: List[StyleIntentTermDefinition] = []
        en_synonym_to_term: Dict[str, StyleIntentTermDefinition] = {}
        zh_synonym_to_term: Dict[str, StyleIntentTermDefinition] = {}
        max_ngram = 1

        for row in rows:
            normalized_en = tuple(
                dict.fromkeys(
                    term
                    for term in (normalize_query_text(raw) for raw in row.get("en_terms", []))
                    if term
                )
            )
            normalized_zh = tuple(
                dict.fromkeys(
                    term
                    for term in (normalize_query_text(raw) for raw in row.get("zh_terms", []))
                    if term
                )
            )
            normalized_attribute = tuple(
                dict.fromkeys(
                    term
                    for term in (normalize_query_text(raw) for raw in row.get("attribute_terms", []))
                    if term
                )
            )
            if not normalized_en and not normalized_zh and not normalized_attribute:
                continue

            canonical = (
                normalized_attribute[0]
                if normalized_attribute
                else normalized_en[0]
                if normalized_en
                else normalized_zh[0]
            )
            term_definition = StyleIntentTermDefinition(
                canonical_value=canonical,
                en_terms=normalized_en,
                zh_terms=normalized_zh,
                attribute_terms=normalized_attribute,
            )
            terms.append(term_definition)

            for term in normalized_en:
                en_synonym_to_term[term] = term_definition
                max_ngram = max(max_ngram, len(term.split()))
            for term in normalized_zh:
                zh_synonym_to_term[term] = term_definition
                max_ngram = max(max_ngram, len(term.split()))

        aliases = tuple(
            dict.fromkeys(
                term
                for term in (
                    normalize_query_text(alias)
                    for alias in dimension_aliases
                )
                if term
            )
        )

        return cls(
            intent_type=intent_type,
            terms=tuple(terms),
            dimension_aliases=aliases,
            en_synonym_to_term=en_synonym_to_term,
            zh_synonym_to_term=zh_synonym_to_term,
            max_term_ngram=max_ngram,
        )

    def match_candidates(self, candidates: Iterable[str], *, language: str) -> Set[StyleIntentTermDefinition]:
        mapping = self.zh_synonym_to_term if language == "zh" else self.en_synonym_to_term
        matched: Set[StyleIntentTermDefinition] = set()
        for candidate in candidates:
            term_definition = mapping.get(normalize_query_text(candidate))
            if term_definition:
                matched.add(term_definition)
        return matched

    def match_text(
        self,
        text: str,
        *,
        language: str,
        tokenizer: Optional[Callable[[str], Any]] = None,
    ) -> Set[StyleIntentTermDefinition]:
        bundle = tokenize_text(text, tokenizer=tokenizer, max_ngram=self.max_term_ngram)
        return self.match_candidates(bundle.candidates, language=language)


@dataclass(frozen=True)
class DetectedStyleIntent:
    intent_type: str
    canonical_value: str
    matched_term: str
    matched_query_text: str
    attribute_terms: Tuple[str, ...]
    dimension_aliases: Tuple[str, ...]
    # Union of zh_terms + en_terms + attribute_terms for the matched term definition.
    # Downstream SKU-selection treats every entry as a valid attribute-value match candidate
    # so a Chinese user query like "卡其色" can match a Chinese option value "卡其色裙".
    all_terms: Tuple[str, ...] = ()

    def to_dict(self) -> Dict[str, Any]:
        return {
            "intent_type": self.intent_type,
            "canonical_value": self.canonical_value,
            "matched_term": self.matched_term,
            "matched_query_text": self.matched_query_text,
            "attribute_terms": list(self.attribute_terms),
            "dimension_aliases": list(self.dimension_aliases),
            "all_terms": list(self.all_terms),
        }

    @property
    def matching_terms(self) -> Tuple[str, ...]:
        """Terms usable for attribute-value matching; falls back to attribute_terms for old callers."""
        return self.all_terms or self.attribute_terms


@dataclass(frozen=True)
class StyleIntentProfile:
    query_variants: Tuple[TokenizedText, ...] = field(default_factory=tuple)
    intents: Tuple[DetectedStyleIntent, ...] = field(default_factory=tuple)

    @property
    def is_active(self) -> bool:
        return bool(self.intents)

    def get_intents(self, intent_type: Optional[str] = None) -> List[DetectedStyleIntent]:
        if intent_type is None:
            return list(self.intents)
        normalized = normalize_query_text(intent_type)
        return [intent for intent in self.intents if intent.intent_type == normalized]

    def get_canonical_values(self, intent_type: str) -> Set[str]:
        return {intent.canonical_value for intent in self.get_intents(intent_type)}

    def to_dict(self) -> Dict[str, Any]:
        return {
            "active": self.is_active,
            "intents": [intent.to_dict() for intent in self.intents],
            "query_variants": [
                {
                    "text": variant.text,
                    "normalized_text": variant.normalized_text,
                    "fine_tokens": list(variant.fine_tokens),
                    "coarse_tokens": list(variant.coarse_tokens),
                    "candidates": list(variant.candidates),
                }
                for variant in self.query_variants
            ],
        }


class StyleIntentRegistry:
    """Holds style intent vocabularies and matching helpers."""

    def __init__(
        self,
        definitions: Dict[str, StyleIntentDefinition],
        *,
        enabled: bool = True,
    ) -> None:
        self.definitions = definitions
        self.enabled = bool(enabled)

    @classmethod
    def from_query_config(cls, query_config: Any) -> "StyleIntentRegistry":
        style_terms = getattr(query_config, "style_intent_terms", {}) or {}
        dimension_aliases = getattr(query_config, "style_intent_dimension_aliases", {}) or {}
        definitions: Dict[str, StyleIntentDefinition] = {}

        for intent_type, rows in style_terms.items():
            definition = StyleIntentDefinition.from_rows(
                intent_type=normalize_query_text(intent_type),
                rows=rows or [],
                dimension_aliases=dimension_aliases.get(intent_type, []),
            )
            if definition.terms:
                definitions[definition.intent_type] = definition

        return cls(
            definitions,
            enabled=bool(getattr(query_config, "style_intent_enabled", True)),
        )

    def get_definition(self, intent_type: str) -> Optional[StyleIntentDefinition]:
        return self.definitions.get(normalize_query_text(intent_type))

    def get_dimension_aliases(self, intent_type: str) -> Tuple[str, ...]:
        definition = self.get_definition(intent_type)
        return definition.dimension_aliases if definition else tuple()


class StyleIntentDetector:
    """Detects style intents from parsed query variants."""

    def __init__(
        self,
        registry: StyleIntentRegistry,
        *,
        tokenizer: Optional[Callable[[str], Any]] = None,
    ) -> None:
        self.registry = registry
        self.tokenizer = tokenizer

    def _max_term_ngram(self) -> int:
        return max(
            (definition.max_term_ngram for definition in self.registry.definitions.values()),
            default=3,
        )

    def _tokenize_text(
        self,
        text: str,
        *,
        analysis_cache: Optional[QueryTextAnalysisCache] = None,
    ) -> TokenizedText:
        max_term_ngram = self._max_term_ngram()
        if analysis_cache is not None:
            return analysis_cache.get_tokenized_text(text, max_ngram=max_term_ngram)
        return tokenize_text(
            text,
            tokenizer=self.tokenizer,
            max_ngram=max_term_ngram,
        )

    def _build_language_variants(
        self,
        parsed_query: Any,
        *,
        analysis_cache: Optional[QueryTextAnalysisCache] = None,
    ) -> Dict[str, TokenizedText]:
        variants: Dict[str, TokenizedText] = {}
        for language in ("zh", "en"):
            text = self._get_language_query_text(parsed_query, language).strip()
            if not text:
                continue
            variants[language] = self._tokenize_text(
                text,
                analysis_cache=analysis_cache,
            )
        return variants

    def _build_query_variants(
        self,
        parsed_query: Any,
        *,
        language_variants: Optional[Dict[str, TokenizedText]] = None,
        analysis_cache: Optional[QueryTextAnalysisCache] = None,
    ) -> Tuple[TokenizedText, ...]:
        seen = set()
        variants: List[TokenizedText] = []

        for variant in (language_variants or self._build_language_variants(
            parsed_query,
            analysis_cache=analysis_cache,
        )).values():
            normalized = variant.normalized_text
            if not normalized or normalized in seen:
                continue
            seen.add(normalized)
            variants.append(variant)

        return tuple(variants)

    @staticmethod
    def _get_language_query_text(parsed_query: Any, language: str) -> str:
        translations = getattr(parsed_query, "translations", {}) or {}
        if isinstance(translations, dict):
            translated = translations.get(language)
            if translated:
                return str(translated)
        return str(getattr(parsed_query, "original_query", "") or "")

    def _tokenize_language_query(
        self,
        parsed_query: Any,
        language: str,
        *,
        language_variants: Optional[Dict[str, TokenizedText]] = None,
        analysis_cache: Optional[QueryTextAnalysisCache] = None,
    ) -> Optional[TokenizedText]:
        if language_variants is not None:
            return language_variants.get(language)
        text = self._get_language_query_text(parsed_query, language).strip()
        if not text:
            return None
        return self._tokenize_text(
            text,
            analysis_cache=analysis_cache,
        )

    def detect(self, parsed_query: Any) -> StyleIntentProfile:
        if not self.registry.enabled or not self.registry.definitions:
            return StyleIntentProfile()

        analysis_cache = getattr(parsed_query, "_text_analysis_cache", None)
        language_variants = self._build_language_variants(
            parsed_query,
            analysis_cache=analysis_cache,
        )
        query_variants = self._build_query_variants(
            parsed_query,
            language_variants=language_variants,
            analysis_cache=analysis_cache,
        )
        zh_variant = self._tokenize_language_query(
            parsed_query,
            "zh",
            language_variants=language_variants,
            analysis_cache=analysis_cache,
        )
        en_variant = self._tokenize_language_query(
            parsed_query,
            "en",
            language_variants=language_variants,
            analysis_cache=analysis_cache,
        )
        detected: List[DetectedStyleIntent] = []
        seen_pairs = set()

        for intent_type, definition in self.registry.definitions.items():
            for language, variant, mapping in (
                ("zh", zh_variant, definition.zh_synonym_to_term),
                ("en", en_variant, definition.en_synonym_to_term),
            ):
                if variant is None or not mapping:
                    continue

                matched_terms = definition.match_candidates(variant.candidates, language=language)
                if not matched_terms:
                    continue

                for candidate in variant.candidates:
                    normalized_candidate = normalize_query_text(candidate)
                    term_definition = mapping.get(normalized_candidate)
                    if term_definition is None or term_definition not in matched_terms:
                        continue
                    pair = (intent_type, term_definition.canonical_value)
                    if pair in seen_pairs:
                        continue
                    seen_pairs.add(pair)
                    all_terms = tuple(
                        dict.fromkeys(
                            (
                                *term_definition.zh_terms,
                                *term_definition.en_terms,
                                *term_definition.attribute_terms,
                            )
                        )
                    )
                    detected.append(
                        DetectedStyleIntent(
                            intent_type=intent_type,
                            canonical_value=term_definition.canonical_value,
                            matched_term=normalized_candidate,
                            matched_query_text=variant.text,
                            attribute_terms=term_definition.attribute_terms,
                            dimension_aliases=definition.dimension_aliases,
                            all_terms=all_terms,
                        )
                    )
                    break

        return StyleIntentProfile(
            query_variants=query_variants,
            intents=tuple(detected),
        )