File size: 9,834 Bytes
38136ed | 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 | """Aspect keyword dictionary for clothing domain.
Seed words for each aspect. Can be extended using metadata's `features`/`categories` text.
"""
import json
import re
from collections import Counter
from typing import Dict, List, Iterable
from .config import ASPECT_DICT_PATH
# Seed dictionary - manually curated for clothing domain
SEED_ASPECT_DICT: Dict[str, List[str]] = {
"SIZE": [
"alterations",
"baggy",
"big",
"boxy fit",
"fit",
"fit perfectly",
"fits",
"fitting",
"great fit",
"huge",
"large",
"length",
"long",
"loose",
"loose fit",
"narrow",
"oversized",
"perfect fit",
"petite",
"poor fit",
"right size",
"roomy",
"runs big",
"runs large",
"runs small",
"runs tight",
"short",
"shorter than expected",
"size",
"size down",
"size up",
"sizing",
"small",
"snug",
"stretched out",
"tight",
"tiny",
"too big",
"too large",
"too long",
"too loose",
"too short",
"too small",
"too tight",
"true to size",
"tts",
"waist",
"wide",
"wrong size",
],
"MATERIAL": [
"blend",
"breathable",
"bulky",
"cashmere",
"cheap fabric",
"cheap material",
"comfortable fabric",
"cotton",
"denim",
"elastic",
"fabric",
"fabric feels",
"feel",
"feeling",
"feels",
"fleece",
"heavy",
"heavier",
"heavier than expected",
"heavy fabric",
"itchy",
"leather",
"lightweight",
"linen",
"lycra",
"material",
"nylon",
"polyester",
"rayon",
"rough",
"scratchy",
"see through",
"see-through",
"sheer",
"silk",
"soft",
"spandex",
"stiff",
"stretch",
"stretchy",
"suede",
"synthetic",
"texture",
"thick",
"thin",
"too heavy",
"transparent",
"uncomfortable fabric",
"velvet",
"warm",
"weight",
"wool",
],
"QUALITY": [
"after wash",
"after washing",
"broke",
"broken",
"button",
"buttons",
"cheaply made",
"construction",
"craftsmanship",
"defect",
"defective",
"durability",
"durable",
"faded",
"faded after wash",
"fading",
"falling apart",
"fell apart",
"first wash",
"flimsy",
"fragile",
"hem",
"holes",
"lasted",
"lasts",
"loose thread",
"loose threads",
"made well",
"pilled",
"pilling",
"poor quality",
"poorly made",
"quality",
"rip",
"ripped",
"seam",
"seams",
"shedding",
"shrink",
"shrunk",
"shrunk after wash",
"shrunk after washing",
"stitch",
"stitching",
"sturdy",
"tear",
"threads",
"tore",
"torn",
"well constructed",
"well made",
"well-made",
"zipper",
],
"APPEARANCE": [
"as advertised",
"as described",
"as pictured",
"as shown",
"beautiful",
"bright",
"color",
"color is off",
"color off",
"colors",
"colour",
"darker",
"design",
"different color",
"different from picture",
"different from photo",
"dull",
"faded color",
"graphic",
"gorgeous",
"lighter",
"logo",
"looked like",
"looks like",
"matches the picture",
"misleading photo",
"off color",
"pattern",
"photo",
"picture",
"print",
"shade",
"stunning",
"tone",
"true color",
"true to color",
"ugly",
"vibrant",
],
"STYLE": [
"boho",
"casual",
"chic",
"classic",
"collar",
"compliment",
"complimented",
"compliments",
"cut",
"cute",
"elegant",
"fashion",
"fashionable",
"flattering",
"formal",
"modern",
"neckline",
"outfit",
"preppy",
"professional",
"received compliments",
"shape",
"shapeless",
"silhouette",
"sleeves",
"style",
"stylish",
"trendy",
"unflattering",
"vintage",
],
"VALUE": [
"affordable",
"bargain",
"cheap price",
"cost",
"discount",
"exchanged",
"expensive",
"for the price",
"for this price",
"good deal",
"great deal",
"money",
"money back",
"money well spent",
"not worth",
"not worth it",
"overpriced",
"price",
"pricey",
"refund",
"return",
"returning",
"value",
"value for money",
"waste of money",
"wasted money",
"worth",
"worth it",
],
}
def get_aspect_dict() -> Dict[str, List[str]]:
"""Return seed dict (callers can extend it)."""
return {k: list(v) for k, v in SEED_ASPECT_DICT.items()}
# Tokens / phrases we consider noise when mining metadata text
_STOPWORDS = set("""
the and for with from this that have has had not your you our their these
those there here when where what which who whom how why because all any
its his her item product use used using also more most less other than just
new cost free buy bought made make piece pieces set sets pack two one three
inch inches cm mm gram fl oz lb pounds black white grey gray
are can will would could should closure imported machine women men kids ladies
amazon brand sleeve sleeves shirt shirts dress dresses pants jacket jackets
""".split())
_GENERIC_MINED_TOKENS = {
"are", "can", "will", "would", "could", "should", "closure", "imported",
"machine", "women", "woman", "men", "kids", "ladies", "perfect", "nice",
"great", "good", "comfortable", "casual", "design", "wear", "wash",
"fit", "fabric", "material", "polyester", "cotton", "soft",
}
_AMBIGUOUS_SINGLE_TOKENS = {
"cheap", "perfect", "comfortable", "design", "style", "fit", "wash",
}
def _tokenize(text: str) -> List[str]:
return re.findall(r"[a-z][a-z\-]+", text.lower())
def extend_aspect_dict_from_metadata(
meta_texts: Iterable[str],
aspect_dict: Dict[str, List[str]] = None,
top_k: int = 20,
min_count: int = 5,
) -> Dict[str, List[str]]:
"""Mine frequent domain words from metadata text to extend aspect seed dict.
For each aspect, if a metadata token co-occurs frequently with any seed word
in the same metadata blob, add it to the aspect's vocabulary.
"""
if aspect_dict is None:
aspect_dict = get_aspect_dict()
# Build per-aspect co-occurrence counter
aspect_cooc = {a: Counter() for a in aspect_dict}
for text in meta_texts:
if not isinstance(text, str) or not text:
continue
tokens = set(_tokenize(text))
if not tokens:
continue
# For each aspect, check if any seed appears in this blob
for aspect, seeds in aspect_dict.items():
seed_set = set(_tokenize(" ".join(seeds)))
if tokens & seed_set:
# Co-occurring tokens -> aspect candidates
for t in tokens:
if t in _STOPWORDS or t in seed_set:
continue
if t in _GENERIC_MINED_TOKENS or t in _AMBIGUOUS_SINGLE_TOKENS:
continue
if len(t) < 4:
continue
aspect_cooc[aspect][t] += 1
# Add top-k high frequency co-occurring tokens to each aspect
extended = {a: list(v) for a, v in aspect_dict.items()}
for aspect, counter in aspect_cooc.items():
new_words = [w for w, c in counter.most_common(top_k * 3) if c >= min_count]
# Filter words that look generic or ambiguous outside local context.
new_words = [
w for w in new_words
if w not in _STOPWORDS
and w not in _GENERIC_MINED_TOKENS
and w not in _AMBIGUOUS_SINGLE_TOKENS
][:top_k]
extended[aspect].extend(new_words)
# Dedup
for a in extended:
extended[a] = sorted(set(extended[a]))
return extended
def save_aspect_dict(aspect_dict: Dict[str, List[str]], path=ASPECT_DICT_PATH):
with open(path, "w", encoding="utf-8") as f:
json.dump(aspect_dict, f, indent=2)
def load_aspect_dict(path=ASPECT_DICT_PATH) -> Dict[str, List[str]]:
with open(path, encoding="utf-8") as f:
return json.load(f)
def compile_aspect_patterns(aspect_dict: Dict[str, List[str]]):
"""Compile regex patterns for each aspect (whole-phrase matching, case-insensitive)."""
patterns = {}
for aspect, keywords in aspect_dict.items():
# Sort longest first so multi-word phrases match before single tokens
sorted_kws = sorted(set(keywords), key=lambda s: -len(s))
escaped = [re.escape(kw) for kw in sorted_kws]
pattern = r"\b(?:" + "|".join(escaped) + r")\b"
patterns[aspect] = re.compile(pattern, flags=re.IGNORECASE)
return patterns
|