Spaces:
Sleeping
Sleeping
Download taste/categorize.py from Creatiste/taste-engine: direct link, hf CLI and curl.
- Browser
- Download file 6.61 kB
-
https://huggingface.co/spaces/Creatiste/taste-engine/resolve/main/taste/categorize.py
- Command line
-
hf download hf://spaces/Creatiste/taste-engine/taste/categorize.py
-
curl -L -o categorize.py https://huggingface.co/spaces/Creatiste/taste-engine/resolve/main/taste/categorize.py
6.61 kB
| """Map heterogeneous store categories to canonical buckets for outfit composition. | |
| Stores use 4+ different taxonomies (Boozt enums, COS hierarchical paths, Norwegian | |
| slugs, Swedish breadcrumbs) plus free-text product names. This module collapses | |
| them into one of: | |
| top, bottom, dress, outerwear, shoes, bag, accessory, | |
| intimates, swimwear, unknown | |
| Strategy: tokenize raw category + product name → match against ordered keyword | |
| sets. Order matters — more specific buckets (shoes, bag) checked before broader | |
| ones (top, accessory) so e.g. "shoulder bag" → bag, not accessory. | |
| """ | |
| from __future__ import annotations | |
| import re | |
| CANONICAL = ( | |
| "shoes", "bag", "swimwear", "intimates", "outerwear", | |
| "dress", "bottom", "top", "accessory", "unknown", | |
| ) | |
| # Each bucket is an ordered list of substring patterns checked against the | |
| # normalized (lowercase, alphanumeric+space) text. First matching bucket wins. | |
| # Patterns are plain substrings unless prefixed with `\b` for word-boundary regex. | |
| _RULES: list[tuple[str, list[str]]] = [ | |
| # Shoes — strong signal; check first to avoid "shoe bag" false-bag. | |
| # `boots` plural-only on purpose: "Boot Jean" / "Boot Cut" are jeans, not shoes. | |
| ("shoes", [ | |
| r"\bshoes\b", r"\bshoe\b", r"\bsko\b", r"\bskor\b", | |
| r"\bboots\b", r"\bsneaker", r"\bheels?\b", r"\bloafer", | |
| r"\btrainer", r"\bpumps\b", r"\bsandal", r"\bmule", | |
| r"\bballetpump", r"\bstileto", r"\bwedge", | |
| ]), | |
| # Bags — before accessory. | |
| # Substring matching (no \b) for Norwegian/Swedish compounds: håndveske, | |
| # rumpetaske, ryggsekk, tøypose, bærenett etc. | |
| ("bag", [ | |
| r"\bbags?\b", r"vesk", r"v[äa]sk", r"taske", r"sekk\b", | |
| r"\bhandbag", r"\bbackpack", r"\bclutch", r"\btote", | |
| r"\bcrossbody", r"\bbowling", r"\bshoulderbag", r"\baxelrem", | |
| r"t[øo]ypose", r"b[æø]renett", r"toalettmappe", r"\bkoffert", | |
| r"\bluggage", r"\bminibag", r"\bduffle", r"\bcarryall", | |
| r"\bsatchel", r"\bbumbag", r"\bfanny ?pack", | |
| r"\bshopper", r"\bhobo\b", r"trillevogn", | |
| ]), | |
| # Swimwear — before bottom (e.g. "beach trouser") and intimates | |
| ("swimwear", [ | |
| r"\bswimwear\b", r"\bswimsuit", r"\bbikini", r"\bbeach", | |
| r"\bstrand", r"\bbadet", r"one_piece_beachwear", r"beach_trouser", | |
| r"beach_accessories", r"bikini_top", r"\bbeachaccessor", | |
| ]), | |
| # Intimates / loungewear — before top/bottom | |
| ("intimates", [ | |
| r"\bbra\b", r"\bbh\b", r"\bpanty\b", r"\bunderpant", r"\btruse", | |
| r"\blingerie", r"\bnightwear", r"\bnightdress", r"\bnight_", | |
| r"\bbathrobe", r"\bsocks?\b", r"\bsokker", r"\bstrømp", | |
| r"\bstocking", r"\btights", r"\bunderwear", r"bustier\b", | |
| ]), | |
| # Outerwear — before top (cardigan/blazer overlap) | |
| ("outerwear", [ | |
| r"\bcoat", r"\bfrakk", r"\bjacket", r"\bjakk", r"\bjackor", | |
| r"\bblazer", r"coatsjackets", r"\bpuffer", r"\bparka", | |
| r"\btrench", r"\banorak", | |
| ]), | |
| # Dresses — including jumpsuits as one-piece outfits. | |
| # Norwegian "kjole" needs care: compound forms (midikjole, chiffonkjole) are | |
| # genuine dresses, but plain "kjoler" is used by Boozt/NA-KD as a top-level | |
| # section header that contains coats, jumpsuits, etc. — so we must NOT match | |
| # bare "kjoler". Two precise rules instead of a leaky substring: | |
| # - `\b\w*kjole\b` — kjole, midikjole, chiffonkjole (singular, incl. standalone) | |
| # - `\b\w+kjoler\b` — midikjoler, chiffonkjoler (compound plurals only) | |
| # Plain "kjoler" matches neither, so section-header categorization falls | |
| # through to the next rules and ultimately to `unknown` if name has no signal. | |
| ("dress", [ | |
| r"\bdress(es)?\b", r"\bgown", r"\bkl[äa]nning", | |
| r"\b\w*kjole\b", | |
| r"\b\w+kjoler\b", | |
| r"\bjumpsuit", r"one_piece_suit", r"one piece suit", | |
| r"combination_clothing", r"bridal", r"\bwedding", | |
| r"\bromper\b", r"\bplaysuit", r"\bcaftan", r"\bkaftan", | |
| ]), | |
| # Bottoms — before top (some "top" overlap with culottes labels) | |
| ("bottom", [ | |
| r"\btrouser", r"\bbukse", r"\bbyxor", r"\bjeans?\b", r"\bdenim\b", | |
| r"\bskirt", r"\bskj[øo]rt", r"\bkjolar\b", | |
| r"\bshorts?\b", r"\bculotte", r"\blegging", | |
| r"trouser_", r"\bpant\b", | |
| ]), | |
| # Tops — broad bucket | |
| ("top", [ | |
| r"\btop(s|per)?\b", r"\btopp\b", r"\bt[- ]?shirt", r"\btshirt", | |
| r"\btee\b", r"\bshirt", r"\bskjort", r"\bblouse", r"\bbluse", | |
| r"\bknitwear", r"\bsweater", r"\bgenser", r"\bpullover", | |
| r"\bcardigan", r"\bkardig", r"\bvest\b", r"\btank\b", | |
| r"polo", r"\btr[öo]jor", r"t_shirt_top", # polo as substring → maxipolo | |
| ]), | |
| # Accessory — catch-all last | |
| ("accessory", [ | |
| r"\baccessor", r"\bjewell?ery", r"\bsmykk", r"\bsmycken", | |
| r"\bbelts?\b", r"\bbelte", r"\bb[äa]lten", r"\bsunglass", | |
| r"\bsolbrille", r"\bsolglas", r"\bglasses\b", r"\bhats?\b", | |
| r"\bcap\b", r"\bscarf", r"\bscarves", r"\bgloves?\b", | |
| r"\bearring", r"\bbracelet", r"\bring\b", r"\bnecklace", | |
| r"\bbeauty\b", r"\bsk[öo]nhet", r"\bhair[- ]?accessor", | |
| r"hatsscarvesgloves", | |
| r"\blommebok", r"\bwallet", r"\bmobiletui", r"\bphone[- ]?case", | |
| r"\bcoin[- ]?case", r"\bphone[- ]?pouch", | |
| r"\bwatches?\b", r"\bklokk", r"\btie\b", r"\bslips\b", | |
| ]), | |
| ] | |
| # Compile once | |
| _COMPILED = [ | |
| (bucket, [re.compile(p, re.IGNORECASE) for p in patterns]) | |
| for bucket, patterns in _RULES | |
| ] | |
| _NORMALIZE_RE = re.compile(r"[^a-z0-9äöåüæø\s_-]+") | |
| def _normalize_text(s: str) -> str: | |
| s = (s or "").lower().replace(">", " ").replace("/", " ") | |
| return _NORMALIZE_RE.sub(" ", s) | |
| def normalize_category(raw_category: str = "", product_name: str = "") -> str: | |
| """Map raw store category + optional product name to a canonical bucket. | |
| Searches the **product name first**, then falls back to category. Reasoning: | |
| NA-KD (and likely other stores) apply a section header like 'kjoler' to | |
| every SKU regardless of actual type, so the name is a more reliable signal | |
| when present. Empty/missing name → category-only fallback still works. | |
| """ | |
| name_text = _normalize_text(product_name) | |
| if name_text: | |
| for bucket, patterns in _COMPILED: | |
| if any(p.search(name_text) for p in patterns): | |
| return bucket | |
| cat_text = _normalize_text(raw_category) | |
| if cat_text: | |
| for bucket, patterns in _COMPILED: | |
| if any(p.search(cat_text) for p in patterns): | |
| return bucket | |
| return "unknown" | |