mucahitkantepe commited on
Commit
3ef384e
·
1 Parent(s): d7f92a4

add canonical_category mapping for outfit composition

Browse files

- taste/categorize.py: rule-based normalizer mapping raw store category
+ product name → {top, bottom, dress, outerwear, shoes, bag,
accessory, intimates, swimwear, unknown}
- handles 4 taxonomies (Boozt enums, COS paths, Norwegian/Swedish
compounds like midikjole, håndveske, ryggsekk)
- store: new canonical_category column, computed in upsert
- 99.45% coverage on the live catalog (10,379 / 10,436)

Files changed (2) hide show
  1. taste/categorize.py +142 -0
  2. taste/store.py +10 -0
taste/categorize.py ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Map heterogeneous store categories to canonical buckets for outfit composition.
2
+
3
+ Stores use 4+ different taxonomies (Boozt enums, COS hierarchical paths, Norwegian
4
+ slugs, Swedish breadcrumbs) plus free-text product names. This module collapses
5
+ them into one of:
6
+
7
+ top, bottom, dress, outerwear, shoes, bag, accessory,
8
+ intimates, swimwear, unknown
9
+
10
+ Strategy: tokenize raw category + product name → match against ordered keyword
11
+ sets. Order matters — more specific buckets (shoes, bag) checked before broader
12
+ ones (top, accessory) so e.g. "shoulder bag" → bag, not accessory.
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+ import re
18
+
19
+ CANONICAL = (
20
+ "shoes", "bag", "swimwear", "intimates", "outerwear",
21
+ "dress", "bottom", "top", "accessory", "unknown",
22
+ )
23
+
24
+ # Each bucket is an ordered list of substring patterns checked against the
25
+ # normalized (lowercase, alphanumeric+space) text. First matching bucket wins.
26
+ # Patterns are plain substrings unless prefixed with `\b` for word-boundary regex.
27
+ _RULES: list[tuple[str, list[str]]] = [
28
+ # Shoes — strong signal; check first to avoid "shoe bag" false-bag.
29
+ # `boots` plural-only on purpose: "Boot Jean" / "Boot Cut" are jeans, not shoes.
30
+ ("shoes", [
31
+ r"\bshoes\b", r"\bshoe\b", r"\bsko\b", r"\bskor\b",
32
+ r"\bboots\b", r"\bsneaker", r"\bheels?\b", r"\bloafer",
33
+ r"\btrainer", r"\bpumps\b", r"\bsandal", r"\bmule",
34
+ r"\bballetpump", r"\bstileto", r"\bwedge",
35
+ ]),
36
+ # Bags — before accessory.
37
+ # Substring matching (no \b) for Norwegian/Swedish compounds: håndveske,
38
+ # rumpetaske, ryggsekk, tøypose, bærenett etc.
39
+ ("bag", [
40
+ r"\bbags?\b", r"vesk", r"v[äa]sk", r"taske", r"sekk\b",
41
+ r"\bhandbag", r"\bbackpack", r"\bclutch", r"\btote",
42
+ r"\bcrossbody", r"\bbowling", r"\bshoulderbag", r"\baxelrem",
43
+ r"t[øo]ypose", r"b[æø]renett", r"toalettmappe", r"\bkoffert",
44
+ r"\bluggage", r"\bminibag", r"\bduffle", r"\bcarryall",
45
+ r"\bsatchel", r"\bbumbag", r"\bfanny ?pack",
46
+ r"\bshopper", r"\bhobo\b", r"trillevogn",
47
+ ]),
48
+ # Swimwear — before bottom (e.g. "beach trouser") and intimates
49
+ ("swimwear", [
50
+ r"\bswimwear\b", r"\bswimsuit", r"\bbikini", r"\bbeach",
51
+ r"\bstrand", r"\bbadet", r"one_piece_beachwear", r"beach_trouser",
52
+ r"beach_accessories", r"bikini_top", r"\bbeachaccessor",
53
+ ]),
54
+ # Intimates / loungewear — before top/bottom
55
+ ("intimates", [
56
+ r"\bbra\b", r"\bbh\b", r"\bpanty\b", r"\bunderpant", r"\btruse",
57
+ r"\blingerie", r"\bnightwear", r"\bnightdress", r"\bnight_",
58
+ r"\bbathrobe", r"\bsocks?\b", r"\bsokker", r"\bstrømp",
59
+ r"\bstocking", r"\btights", r"\bunderwear", r"bustier\b",
60
+ ]),
61
+ # Outerwear — before top (cardigan/blazer overlap)
62
+ ("outerwear", [
63
+ r"\bcoat", r"\bfrakk", r"\bjacket", r"\bjakk", r"\bjackor",
64
+ r"\bblazer", r"coatsjackets", r"\bpuffer", r"\bparka",
65
+ r"\btrench", r"\banorak",
66
+ ]),
67
+ # Dresses — including jumpsuits as one-piece outfits.
68
+ # `kjole` matched as substring (no \b) — Norwegian compounds: midikjole,
69
+ # maxikjole, chiffonkjole, etc. Standalone `kjoler` is intentionally NOT
70
+ # here: NA-KD applies it as a catch-all section to non-dresses too.
71
+ ("dress", [
72
+ r"\bdress(es)?\b", r"\bgown", r"\bkl[äa]nning",
73
+ r"kjole", # substring: matches kjole, midikjole, maxikjole, etc.
74
+ r"\bjumpsuit", r"one_piece_suit", r"one piece suit",
75
+ r"combination_clothing", r"bridal", r"\bwedding",
76
+ r"\bromper\b", r"\bplaysuit", r"\bcaftan", r"\bkaftan",
77
+ ]),
78
+ # Bottoms — before top (some "top" overlap with culottes labels)
79
+ ("bottom", [
80
+ r"\btrouser", r"\bbukse", r"\bbyxor", r"\bjeans?\b", r"\bdenim\b",
81
+ r"\bskirt", r"\bskj[øo]rt", r"\bkjolar\b",
82
+ r"\bshorts?\b", r"\bculotte", r"\blegging",
83
+ r"trouser_", r"\bpant\b",
84
+ ]),
85
+ # Tops — broad bucket
86
+ ("top", [
87
+ r"\btop(s|per)?\b", r"\btopp\b", r"\bt[- ]?shirt", r"\btshirt",
88
+ r"\btee\b", r"\bshirt", r"\bskjort", r"\bblouse", r"\bbluse",
89
+ r"\bknitwear", r"\bsweater", r"\bgenser", r"\bpullover",
90
+ r"\bcardigan", r"\bkardig", r"\bvest\b", r"\btank\b",
91
+ r"polo", r"\btr[öo]jor", r"t_shirt_top", # polo as substring → maxipolo
92
+ ]),
93
+ # Accessory — catch-all last
94
+ ("accessory", [
95
+ r"\baccessor", r"\bjewell?ery", r"\bsmykk", r"\bsmycken",
96
+ r"\bbelts?\b", r"\bbelte", r"\bb[äa]lten", r"\bsunglass",
97
+ r"\bsolbrille", r"\bsolglas", r"\bglasses\b", r"\bhats?\b",
98
+ r"\bcap\b", r"\bscarf", r"\bscarves", r"\bgloves?\b",
99
+ r"\bearring", r"\bbracelet", r"\bring\b", r"\bnecklace",
100
+ r"\bbeauty\b", r"\bsk[öo]nhet", r"\bhair[- ]?accessor",
101
+ r"hatsscarvesgloves",
102
+ r"\blommebok", r"\bwallet", r"\bmobiletui", r"\bphone[- ]?case",
103
+ r"\bcoin[- ]?case", r"\bphone[- ]?pouch",
104
+ r"\bwatches?\b", r"\bklokk", r"\btie\b", r"\bslips\b",
105
+ ]),
106
+ ]
107
+
108
+ # Compile once
109
+ _COMPILED = [
110
+ (bucket, [re.compile(p, re.IGNORECASE) for p in patterns])
111
+ for bucket, patterns in _RULES
112
+ ]
113
+
114
+ _NORMALIZE_RE = re.compile(r"[^a-z0-9äöåüæø\s_-]+")
115
+
116
+
117
+ def _normalize_text(s: str) -> str:
118
+ s = (s or "").lower().replace(">", " ").replace("/", " ")
119
+ return _NORMALIZE_RE.sub(" ", s)
120
+
121
+
122
+ def normalize_category(raw_category: str = "", product_name: str = "") -> str:
123
+ """Map raw store category + optional product name to a canonical bucket.
124
+
125
+ Searches the **product name first**, then falls back to category. Reasoning:
126
+ NA-KD (and likely other stores) apply a section header like 'kjoler' to
127
+ every SKU regardless of actual type, so the name is a more reliable signal
128
+ when present. Empty/missing name → category-only fallback still works.
129
+ """
130
+ name_text = _normalize_text(product_name)
131
+ if name_text:
132
+ for bucket, patterns in _COMPILED:
133
+ if any(p.search(name_text) for p in patterns):
134
+ return bucket
135
+
136
+ cat_text = _normalize_text(raw_category)
137
+ if cat_text:
138
+ for bucket, patterns in _COMPILED:
139
+ if any(p.search(cat_text) for p in patterns):
140
+ return bucket
141
+
142
+ return "unknown"
taste/store.py CHANGED
@@ -5,6 +5,8 @@ import sqlite3
5
  from datetime import datetime, timezone
6
  from pathlib import Path
7
 
 
 
8
  _SCHEMA_V2 = """
9
  CREATE TABLE IF NOT EXISTS products (
10
  url TEXT PRIMARY KEY,
@@ -31,6 +33,7 @@ CREATE TABLE IF NOT EXISTS products (
31
  score REAL,
32
  image_path TEXT DEFAULT '',
33
  image_paths TEXT DEFAULT '[]',
 
34
 
35
  first_seen_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
36
  last_seen_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
@@ -77,6 +80,7 @@ class ProductStore:
77
  "on_sale": "BOOLEAN DEFAULT 0",
78
  "image_urls": "TEXT DEFAULT '[]'",
79
  "image_paths": "TEXT DEFAULT '[]'",
 
80
  "in_stock": "BOOLEAN DEFAULT 1",
81
  "sizes": "TEXT DEFAULT '[]'",
82
  "first_seen_at": "TIMESTAMP DEFAULT ''",
@@ -162,16 +166,20 @@ class ProductStore:
162
  if not isinstance(image_paths, str):
163
  image_paths = json.dumps(image_paths if isinstance(image_paths, list) else [])
164
 
 
 
165
  self.conn.execute("""
166
  INSERT INTO products (
167
  url, external_id, store, brand, name, color, material, category,
168
  price_nok, original_price_nok, price_text, currency, on_sale,
169
  image_url, image_urls, in_stock, sizes, score, image_path, image_paths,
 
170
  first_seen_at, last_seen_at, updated_at
171
  ) VALUES (
172
  :url, :external_id, :store, :brand, :name, :color, :material, :category,
173
  :price_nok, :original_price_nok, :price_text, :currency, :on_sale,
174
  :image_url, :image_urls, :in_stock, :sizes, :score, :image_path, :image_paths,
 
175
  CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
176
  )
177
  ON CONFLICT(url) DO UPDATE SET
@@ -192,6 +200,7 @@ class ProductStore:
192
  sizes = CASE WHEN excluded.sizes != '[]' THEN excluded.sizes ELSE products.sizes END,
193
  image_path = COALESCE(NULLIF(excluded.image_path, ''), products.image_path),
194
  image_paths = CASE WHEN excluded.image_paths != '[]' THEN excluded.image_paths ELSE products.image_paths END,
 
195
  last_seen_at = CURRENT_TIMESTAMP,
196
  updated_at = CURRENT_TIMESTAMP
197
  """, {
@@ -215,6 +224,7 @@ class ProductStore:
215
  "score": p.get("score"),
216
  "image_path": p.get("image_path", ""),
217
  "image_paths": image_paths,
 
218
  })
219
 
220
  # --- Sync (idempotent scrape ingestion) ---
 
5
  from datetime import datetime, timezone
6
  from pathlib import Path
7
 
8
+ from taste.categorize import normalize_category
9
+
10
  _SCHEMA_V2 = """
11
  CREATE TABLE IF NOT EXISTS products (
12
  url TEXT PRIMARY KEY,
 
33
  score REAL,
34
  image_path TEXT DEFAULT '',
35
  image_paths TEXT DEFAULT '[]',
36
+ canonical_category TEXT DEFAULT '',
37
 
38
  first_seen_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
39
  last_seen_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
 
80
  "on_sale": "BOOLEAN DEFAULT 0",
81
  "image_urls": "TEXT DEFAULT '[]'",
82
  "image_paths": "TEXT DEFAULT '[]'",
83
+ "canonical_category": "TEXT DEFAULT ''",
84
  "in_stock": "BOOLEAN DEFAULT 1",
85
  "sizes": "TEXT DEFAULT '[]'",
86
  "first_seen_at": "TIMESTAMP DEFAULT ''",
 
166
  if not isinstance(image_paths, str):
167
  image_paths = json.dumps(image_paths if isinstance(image_paths, list) else [])
168
 
169
+ canonical = normalize_category(p.get("category", ""), p.get("name", ""))
170
+
171
  self.conn.execute("""
172
  INSERT INTO products (
173
  url, external_id, store, brand, name, color, material, category,
174
  price_nok, original_price_nok, price_text, currency, on_sale,
175
  image_url, image_urls, in_stock, sizes, score, image_path, image_paths,
176
+ canonical_category,
177
  first_seen_at, last_seen_at, updated_at
178
  ) VALUES (
179
  :url, :external_id, :store, :brand, :name, :color, :material, :category,
180
  :price_nok, :original_price_nok, :price_text, :currency, :on_sale,
181
  :image_url, :image_urls, :in_stock, :sizes, :score, :image_path, :image_paths,
182
+ :canonical_category,
183
  CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
184
  )
185
  ON CONFLICT(url) DO UPDATE SET
 
200
  sizes = CASE WHEN excluded.sizes != '[]' THEN excluded.sizes ELSE products.sizes END,
201
  image_path = COALESCE(NULLIF(excluded.image_path, ''), products.image_path),
202
  image_paths = CASE WHEN excluded.image_paths != '[]' THEN excluded.image_paths ELSE products.image_paths END,
203
+ canonical_category = excluded.canonical_category,
204
  last_seen_at = CURRENT_TIMESTAMP,
205
  updated_at = CURRENT_TIMESTAMP
206
  """, {
 
224
  "score": p.get("score"),
225
  "image_path": p.get("image_path", ""),
226
  "image_paths": image_paths,
227
+ "canonical_category": canonical,
228
  })
229
 
230
  # --- Sync (idempotent scrape ingestion) ---