Validate product attributes and decide match status

This commit is contained in:
Fiden committed 2026-08-07 13:13:11 +00:00
1 parent da8ad74498
commit a67826daa4
4 files changed
+927 -15

No files matched your search

+49
View File
@@ -5,6 +5,10 @@ from dataclasses import dataclass
from decimal import Decimal, InvalidOperation
from pathlib import Path
from app.aliases import ProductAliasIndex
from app.attributes import BrandIndex, ParsedAttributes, parse_attributes
from app.normalization import meaningful_tokens, normalize_text, tokenize
@dataclass(frozen=True, slots=True)
class RawCatalogItem:
@@ -14,6 +18,18 @@ class RawCatalogItem:
price: Decimal
@dataclass(frozen=True, slots=True)
class CatalogItem:
sku: str
name: str
unit: str
price: Decimal
normalized_name: str
search_tokens: tuple[str, ...]
product_type: str
attributes: ParsedAttributes
def load_raw_catalog(path: Path) -> tuple[RawCatalogItem, ...]:
if not path.is_file():
raise FileNotFoundError(f"Catalog file does not exist: {path}")
@@ -52,3 +68,36 @@ def load_raw_catalog(path: Path) -> tuple[RawCatalogItem, ...]:
if not items:
raise ValueError("Catalog is empty")
return tuple(items)
def build_catalog(
raw_items: tuple[RawCatalogItem, ...],
alias_index: ProductAliasIndex,
brand_index: BrandIndex,
) -> tuple[CatalogItem, ...]:
result: list[CatalogItem] = []
for raw in raw_items:
normalized = normalize_text(raw.name)
product_type = alias_index.detect_catalog_type(raw.name)
attributes = parse_attributes(
raw.name,
product_type,
unit=raw.unit,
catalog_item=True,
brand_index=brand_index,
)
result.append(
CatalogItem(
sku=raw.sku,
name=raw.name,
unit=raw.unit,
price=raw.price,
normalized_name=normalized,
search_tokens=meaningful_tokens(
tokenize(normalized, already_normalized=True)
),
product_type=product_type,
attributes=attributes,
)
)
return tuple(result)