mucahitkantepe commited on
Commit
930ed2b
·
1 Parent(s): 14d164a

categorize: stop matching standalone "kjoler" as dress

Browse files

The dress rule used `kjole` as a substring to catch Norwegian compounds
like midikjole/chiffonkjole, but it also matched `kjoler` — which Boozt
and NA-KD use as a flat section header containing coats, jumpsuits,
skirts, and other non-dresses. Replace with two precise patterns:

\b\w*kjole\b — singular forms incl. standalone "kjole"
\b\w+kjoler\b — compound plurals only (no standalone "kjoler")

Re-categorized the cloned DB: 158 rows shift from dress to unknown.
All 158 had category="kjoler" with no garment word in the (often
truncated) product name, so unknown is the honest bucket. Outfit
composer's PAIRING_RULES never request "unknown", so these stop
appearing as wrong-bucket complements.

Files changed (2) hide show
  1. scripts/recategorize.py +109 -0
  2. taste/categorize.py +10 -4
scripts/recategorize.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Re-run `normalize_category` over every product and update `canonical_category`.
2
+
3
+ Used after a fix to `taste/categorize.py` to migrate the existing DB to the
4
+ new bucketing without re-scraping. Idempotent — running twice changes nothing
5
+ the second time.
6
+
7
+ Usage::
8
+
9
+ TASTE_DATA_DIR=~/.taste-massimo uv run --no-project --python 3.13 \\
10
+ python scripts/recategorize.py [--dry-run]
11
+
12
+ Prints:
13
+ - Per-bucket distribution before / after
14
+ - A sorted move matrix (old → new) showing how many rows shifted
15
+ - Total rows updated
16
+ """
17
+
18
+ import argparse
19
+ import sqlite3
20
+ import sys
21
+ from collections import Counter
22
+ from pathlib import Path
23
+
24
+ REPO = Path(__file__).resolve().parents[1]
25
+ sys.path.insert(0, str(REPO))
26
+
27
+ from taste.categorize import normalize_category
28
+
29
+
30
+ def _data_dir() -> Path:
31
+ import os
32
+ env = os.environ.get("TASTE_DATA_DIR")
33
+ return Path(env).expanduser() if env else Path.home() / ".taste"
34
+
35
+
36
+ def _print_dist(label: str, counts: Counter) -> None:
37
+ print(f"\n{label}")
38
+ print("-" * 32)
39
+ total = sum(counts.values())
40
+ for bucket, n in sorted(counts.items(), key=lambda x: (-x[1], x[0])):
41
+ pct = 100.0 * n / total if total else 0.0
42
+ print(f" {bucket:12s} {n:5d} {pct:5.1f}%")
43
+ print(f" {'TOTAL':12s} {total:5d}")
44
+
45
+
46
+ def main() -> int:
47
+ ap = argparse.ArgumentParser()
48
+ ap.add_argument("--dry-run", action="store_true",
49
+ help="Compute and report changes without writing to DB.")
50
+ args = ap.parse_args()
51
+
52
+ db_path = _data_dir() / "taste.db"
53
+ if not db_path.exists():
54
+ print(f"DB not found: {db_path}", file=sys.stderr)
55
+ return 1
56
+
57
+ print(f"DB: {db_path}")
58
+ print(f"Mode: {'DRY RUN' if args.dry_run else 'WRITE'}")
59
+
60
+ conn = sqlite3.connect(str(db_path))
61
+ conn.row_factory = sqlite3.Row
62
+
63
+ rows = conn.execute(
64
+ "SELECT url, category, name, canonical_category FROM products"
65
+ ).fetchall()
66
+
67
+ before = Counter(r["canonical_category"] or "unknown" for r in rows)
68
+ moves: Counter[tuple[str, str]] = Counter()
69
+ updates: list[tuple[str, str]] = []
70
+
71
+ for r in rows:
72
+ old = r["canonical_category"] or "unknown"
73
+ new = normalize_category(r["category"] or "", r["name"] or "")
74
+ if new != old:
75
+ moves[(old, new)] += 1
76
+ updates.append((new, r["url"]))
77
+
78
+ after = Counter(before)
79
+ for (old, new), n in moves.items():
80
+ after[old] -= n
81
+ after[new] += n
82
+ after = Counter({k: v for k, v in after.items() if v > 0})
83
+
84
+ _print_dist("Before", before)
85
+ _print_dist("After", after)
86
+
87
+ print(f"\nMove matrix ({sum(moves.values())} rows shift)")
88
+ print("-" * 40)
89
+ for (old, new), n in sorted(moves.items(), key=lambda x: -x[1]):
90
+ print(f" {old:12s} -> {new:12s} {n:5d}")
91
+
92
+ if not updates:
93
+ print("\nNo changes needed.")
94
+ return 0
95
+
96
+ if args.dry_run:
97
+ print(f"\n[dry-run] would update {len(updates)} rows.")
98
+ return 0
99
+
100
+ conn.executemany(
101
+ "UPDATE products SET canonical_category = ? WHERE url = ?", updates
102
+ )
103
+ conn.commit()
104
+ print(f"\nUpdated {len(updates)} rows.")
105
+ return 0
106
+
107
+
108
+ if __name__ == "__main__":
109
+ sys.exit(main())
taste/categorize.py CHANGED
@@ -65,12 +65,18 @@ _RULES: list[tuple[str, list[str]]] = [
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",
 
65
  r"\btrench", r"\banorak",
66
  ]),
67
  # Dresses — including jumpsuits as one-piece outfits.
68
+ # Norwegian "kjole" needs care: compound forms (midikjole, chiffonkjole) are
69
+ # genuine dresses, but plain "kjoler" is used by Boozt/NA-KD as a top-level
70
+ # section header that contains coats, jumpsuits, etc. — so we must NOT match
71
+ # bare "kjoler". Two precise rules instead of a leaky substring:
72
+ # - `\b\w*kjole\b` — kjole, midikjole, chiffonkjole (singular, incl. standalone)
73
+ # - `\b\w+kjoler\b` — midikjoler, chiffonkjoler (compound plurals only)
74
+ # Plain "kjoler" matches neither, so section-header categorization falls
75
+ # through to the next rules and ultimately to `unknown` if name has no signal.
76
  ("dress", [
77
  r"\bdress(es)?\b", r"\bgown", r"\bkl[äa]nning",
78
+ r"\b\w*kjole\b",
79
+ r"\b\w+kjoler\b",
80
  r"\bjumpsuit", r"one_piece_suit", r"one piece suit",
81
  r"combination_clothing", r"bridal", r"\bwedding",
82
  r"\bromper\b", r"\bplaysuit", r"\bcaftan", r"\bkaftan",