File size: 12,020 Bytes
d992912 | 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 | """
engine/query_parser.py
Extracted from finalized_search_engine_full_script.py (lines 776-1056).
Contains the ParsedQuery dataclass and QueryParser class responsible for
converting natural-language fashion queries into structured filter intents.
"""
import re
import logging
from dataclasses import dataclass, field
from typing import List, Optional
logger = logging.getLogger(__name__)
__all__ = ["ParsedQuery", "QueryParser"]
@dataclass
class ParsedQuery:
raw_query: str
vibe_text: str
category_filter: Optional[str] = None
color_filter: Optional[str] = None
gender_filter: Optional[str] = None
price_min: Optional[float] = None
price_max: Optional[float] = None
brand_filter: Optional[str] = None
size_filter: Optional[str] = None
material_filter: Optional[str] = None
exclusions: List[str] = field(default_factory=list)
in_stock_only: bool = True
style_tags: List[str] = field(default_factory=list)
has_image: bool = False
text_weight: float = 0.5
# Multilingual / correction metadata
original_query: Optional[str] = None
detected_language: str = "en"
was_translated: bool = False
was_spell_corrected: bool = False
spell_correction_suggestion: Optional[str] = None
class QueryParser:
"""Parses natural language fashion queries into structured intents."""
PRICE_PATTERNS = [
(r'[£$€]?\s*(\d+(?:\.\d+)?)\s*[-–to]+\s*[£$€]?\s*(\d+(?:\.\d+)?)', 'range'),
(r'(?:under|below|less\s+than|max|up\s+to|cheaper\s+than)\s*[£$€]?\s*(\d+(?:\.\d+)?)', 'max'),
(r'(?:over|above|more\s+than|min|at\s+least|from)\s*[£$€]?\s*(\d+(?:\.\d+)?)', 'min'),
(r'\b(?:budget|cheap|affordable|bargain|inexpensive|value)\b', 'budget'),
(r'\b(?:luxury|premium|high[\s-]?end|designer|expensive|splurge)\b', 'luxury'),
]
CATEGORY_TRIGGERS = {
'midi dress': 'Dresses', 'maxi dress': 'Dresses',
'mini dress': 'Dresses', 'slip dress': 'Dresses',
'bodycon': 'Dresses', 'dress': 'Dresses',
'dresses': 'Dresses', 'gown': 'Dresses',
'trench coat': 'Coats & Jackets', 'puffer jacket': 'Coats & Jackets',
'leather jacket': 'Coats & Jackets', 'denim jacket': 'Coats & Jackets',
'bomber jacket': 'Coats & Jackets',
'jacket': 'Coats & Jackets', 'coat': 'Coats & Jackets',
'blazer': 'Coats & Jackets', 'parka': 'Coats & Jackets',
't-shirt': 'Tops', 'tee': 'Tops',
'blouse': 'Tops', 'shirt': 'Tops',
'crop top': 'Tops', 'cami': 'Tops',
'bodysuit': 'Tops', 'top': 'Tops', 'tops': 'Tops',
'cardigan': 'Knitwear', 'jumper': 'Knitwear',
'sweater': 'Knitwear', 'pullover': 'Knitwear', 'knitwear': 'Knitwear',
'hoodie': 'Hoodies & Sweatshirts', 'sweatshirt': 'Hoodies & Sweatshirts',
'jeans': 'Jeans',
'trousers': 'Trousers', 'pants': 'Trousers',
'joggers': 'Trousers', 'leggings': 'Trousers', 'cargo': 'Trousers',
'shorts': 'Shorts',
'skirt': 'Skirts', 'midi skirt': 'Skirts', 'mini skirt': 'Skirts',
'trainers': 'Shoes', 'sneakers': 'Shoes',
'boots': 'Shoes', 'heels': 'Shoes',
'sandals': 'Shoes', 'loafers': 'Shoes',
'shoes': 'Shoes', 'mules': 'Shoes',
'platforms': 'Shoes', 'flats': 'Shoes',
'bag': 'Bags', 'handbag': 'Bags',
'tote': 'Bags', 'backpack': 'Bags',
'clutch': 'Bags', 'crossbody': 'Bags',
'watch': 'Accessories', 'sunglasses': 'Accessories',
'hat': 'Accessories', 'cap': 'Accessories',
'scarf': 'Accessories', 'belt': 'Accessories',
'jewellery': 'Accessories', 'jewelry': 'Accessories',
'necklace': 'Accessories', 'bracelet': 'Accessories',
'earrings': 'Accessories', 'ring': 'Accessories',
'swimsuit': 'Swimwear', 'bikini': 'Swimwear', 'swim': 'Swimwear',
'suit': 'Suits & Tailoring', 'waistcoat': 'Suits & Tailoring',
'jumpsuit': 'Jumpsuits & Playsuits', 'playsuit': 'Jumpsuits & Playsuits',
'romper': 'Jumpsuits & Playsuits',
'lingerie': 'Underwear & Socks', 'bra': 'Underwear & Socks',
'briefs': 'Underwear & Socks', 'boxers': 'Underwear & Socks',
'socks': 'Underwear & Socks',
}
# ── FIX: COLOR_MAP now outputs LOWERCASE to match actual data values ──
COLOR_MAP = {
'red': 'red', 'scarlet': 'red', 'crimson': 'red',
'blue': 'blue', 'cobalt': 'blue',
'sky blue': 'blue', 'teal': 'blue', 'aqua': 'blue',
'navy': 'navy', # data has 'navy' as its own family
'green': 'green', 'olive': 'green', 'emerald': 'green',
'sage': 'green', 'mint': 'green',
'khaki': 'khaki', # data has 'khaki' as its own family
'black': 'black', 'charcoal': 'black',
'white': 'white', 'cream': 'white', 'ivory': 'white',
'pink': 'pink', 'blush': 'pink', 'rose': 'pink',
'fuchsia': 'pink', 'magenta': 'pink', 'coral': 'pink',
'yellow': 'yellow', 'gold': 'yellow', 'mustard': 'yellow',
'orange': 'orange', 'rust': 'orange', 'terracotta': 'orange',
'brown': 'brown', 'tan': 'brown', 'camel': 'brown',
'beige': 'beige', 'taupe': 'beige', # data has 'beige' as its own family
'chocolate': 'brown',
'purple': 'purple', 'lilac': 'purple', 'plum': 'purple',
'lavender': 'purple', 'violet': 'purple', 'mauve': 'purple',
'burgundy': 'burgundy', # data has 'burgundy' as its own family
'grey': 'grey', 'gray': 'grey', 'silver': 'grey',
'multi': 'multi', 'rainbow': 'multi', 'multicolour': 'multi',
'multicolor': 'multi',
}
GENDER_TRIGGERS = {
"men's": "Men", "mens": "Men", "male": "Men", "for men": "Men",
"for him": "Men", "boys": "Men", "masculine": "Men",
"women's": "Women", "womens": "Women", "female": "Women",
"for women": "Women", "for her": "Women", "girls": "Women",
"ladies": "Women", "feminine": "Women",
"unisex": "Unisex",
}
STYLE_TAGS = [
'casual', 'formal', 'streetwear', 'boho', 'bohemian', 'minimalist',
'vintage', 'retro', 'y2k', 'goth', 'gothic', 'punk', 'preppy',
'athleisure', 'sporty', 'elegant', 'chic', 'edgy', 'romantic',
'classic', 'modern', 'oversized', 'cropped', 'fitted', 'relaxed',
'floral', 'striped', 'plaid', 'animal print', 'leopard', 'sequin',
'lace', 'denim', 'leather', 'satin', 'silk', 'velvet', 'knit',
'sustainable', 'eco', 'organic', 'recycled',
'festival', 'party', 'office', 'workwear', 'loungewear', 'sleepwear',
'coastal', 'cottagecore', 'grunge', 'cyber', 'futuristic',
'western', 'nautical', 'tropical', 'safari',
]
MATERIAL_KEYWORDS = {
'silk': 'silk', 'satin': 'satin', 'velvet': 'velvet',
'leather': 'leather', 'faux leather': 'faux leather',
'denim': 'denim', 'cotton': 'cotton', 'linen': 'linen',
'wool': 'wool', 'cashmere': 'cashmere', 'polyester': 'polyester',
'nylon': 'nylon', 'suede': 'suede', 'chiffon': 'chiffon',
'mesh': 'mesh', 'jersey': 'jersey',
'tweed': 'tweed', 'corduroy': 'corduroy', 'fleece': 'fleece',
'crochet': 'crochet', 'organza': 'organza', 'tulle': 'tulle',
}
SIZE_PATTERNS = [
(r'\bsize\s+(xx?s|xx?l|small|medium|large)\b', 'named'),
(r'\b(xx?s|xx?l)\b', 'named_bare'),
(r'\bsize\s+(\d{1,2})\b', 'numeric'),
(r'\buk\s+(\d{1,2})\b', 'numeric'),
(r'\beu\s+(\d{2})\b', 'eu'),
]
_SIZE_NORMALIZE = {
'xxs': 'XXS', 'xs': 'XS', 'x-small': 'XS', 'xsmall': 'XS',
's': 'S', 'small': 'S',
'm': 'M', 'medium': 'M',
'l': 'L', 'large': 'L',
'xl': 'XL', 'x-large': 'XL', 'xlarge': 'XL',
'xxl': 'XXL',
}
EXCLUSION_PATTERNS = [
r'\bnot\s+(\w+(?:\s+\w+)?)',
r'\bwithout\s+(\w+(?:\s+\w+)?)',
r'\bno\s+(\w+)',
r'\bexcluding\s+(\w+(?:\s+\w+)?)',
]
def parse(self, query: str) -> ParsedQuery:
raw = query.strip()
q = raw.lower()
vibe = q
# Price
price_min, price_max = None, None
for pattern, ptype in self.PRICE_PATTERNS:
m = re.search(pattern, q)
if m:
if ptype == 'range':
price_min, price_max = float(m.group(1)), float(m.group(2))
elif ptype == 'max':
price_max = float(m.group(1))
elif ptype == 'min':
price_min = float(m.group(1))
elif ptype == 'budget':
price_max = 30.0
elif ptype == 'luxury':
price_min = 100.0
vibe = vibe[:m.start()] + vibe[m.end():]
break
# Category
category = None
for trigger, cat in sorted(self.CATEGORY_TRIGGERS.items(), key=lambda x: -len(x[0])):
if re.search(r'\b' + re.escape(trigger) + r'\b', q):
category = cat
break
# Color
color = None
for color_term, family in sorted(self.COLOR_MAP.items(), key=lambda x: -len(x[0])):
if re.search(r'\b' + re.escape(color_term) + r'\b', q):
color = family
break
# Gender
gender = None
for trigger, gen in self.GENDER_TRIGGERS.items():
if trigger in q:
gender = gen
vibe = vibe.replace(trigger, '')
break
# Style tags
tags = [t for t in self.STYLE_TAGS if re.search(r'\b' + re.escape(t) + r'\b', q)]
# Material
material = None
for mat_term, mat_val in sorted(self.MATERIAL_KEYWORDS.items(), key=lambda x: -len(x[0])):
if re.search(r'\b' + re.escape(mat_term) + r'\b', q):
material = mat_val
break
# Size
size = None
for pattern, stype in self.SIZE_PATTERNS:
m = re.search(pattern, q)
if m:
raw_size = m.group(1).lower()
if stype in ('named', 'named_bare'):
size = self._SIZE_NORMALIZE.get(raw_size, raw_size.upper())
elif stype == 'numeric':
size = raw_size # keep as string "10", "12", etc.
elif stype == 'eu':
size = f"EU {raw_size}"
vibe = vibe[:m.start()] + vibe[m.end():]
break
# Exclusions ("not floral", "without black", "no heels")
exclusions = []
spans_to_remove = []
for exc_pattern in self.EXCLUSION_PATTERNS:
for m in re.finditer(exc_pattern, q):
excluded_term = m.group(1).strip()
if excluded_term and excluded_term not in exclusions:
exclusions.append(excluded_term)
spans_to_remove.append((m.start(), m.end()))
# Remove exclusion spans from vibe in reverse order to preserve positions
for start, end in sorted(spans_to_remove, reverse=True):
vibe = vibe[:start] + vibe[end:]
# Resolve material+exclusion conflict: if user says "no cotton",
# cotton is excluded, not desired as a material filter
if material and material.lower() in [e.lower() for e in exclusions]:
material = None
# Clean vibe text
vibe = re.sub(r'[£$€]\s*\d+', '', vibe)
vibe = re.sub(r'\b(under|below|over|above|less than|more than|up to)\b', '', vibe)
vibe = re.sub(r'\s+', ' ', vibe).strip()
if not vibe:
vibe = raw
return ParsedQuery(
raw_query=raw, vibe_text=vibe,
category_filter=category, color_filter=color,
gender_filter=gender, price_min=price_min, price_max=price_max,
style_tags=tags, material_filter=material,
size_filter=size, exclusions=exclusions,
)
|