Style / query_processing.py
nexusbert's picture
modularization
ab2012f
import re
from typing import List
from config import COLOR_HARMONY, CLOTHING_TYPES
def extract_clothing_info(text: str) -> dict:
text_lower = text.lower()
colors = list(COLOR_HARMONY.keys())
found_color = None
for color in colors:
if color in text_lower:
found_color = color
break
found_types = []
for clothing_type in CLOTHING_TYPES:
if clothing_type in text_lower:
found_types.append(clothing_type)
existing_item = None
requested_item = None
question_patterns = ["what kind of", "what", "which", "suggest", "recommend"]
is_question = any(pattern in text_lower for pattern in question_patterns)
if is_question and len(found_types) > 0:
if "my" in text_lower or "i have" in text_lower or "i'm wearing" in text_lower:
for i, word in enumerate(text_lower.split()):
if word == "my" and i + 1 < len(text_lower.split()):
next_words = " ".join(text_lower.split()[i+1:i+4])
for ct in found_types:
if ct in next_words:
existing_item = ct
break
if existing_item:
break
for ct in found_types:
if "match" in text_lower or "go with" in text_lower or "pair" in text_lower:
ct_pos = text_lower.find(ct)
match_pos = text_lower.find("match")
if ct_pos > match_pos or "what kind of" in text_lower[:ct_pos]:
requested_item = ct
break
if not existing_item and not requested_item and found_types:
if is_question:
requested_item = found_types[0]
else:
existing_item = found_types[0]
return {
"color": found_color,
"type": existing_item or requested_item,
"existing_item": existing_item,
"requested_item": requested_item,
"is_question": is_question,
"raw_text": text
}
def get_color_matches(color: str) -> List[str]:
color_lower = color.lower()
return COLOR_HARMONY.get(color_lower, ["white", "black", "grey", "beige"])
def extract_colors_from_query(query: str) -> tuple:
query_lower = query.lower()
colors = list(COLOR_HARMONY.keys())
extended_colors = ["navy blue", "wine", "burgundy", "maroon", "crimson", "scarlet", "mauve", "taupe", "olive", "teal", "turquoise", "indigo", "cobalt"]
all_colors = extended_colors + colors
color_mapping = {
"wine": "red", "burgundy": "red", "maroon": "red",
"mauve": "purple", "taupe": "beige", "olive": "green",
"teal": "blue", "turquoise": "blue", "indigo": "navy", "cobalt": "blue",
"navy blue": "navy"
}
found_colors = []
seen_mapped = set()
for color in all_colors:
if color in query_lower:
mapped = color_mapping.get(color, color)
if mapped not in seen_mapped:
found_colors.append((color, mapped))
seen_mapped.add(mapped)
return found_colors
def detect_query_type(message: str) -> str:
message_lower = message.lower()
color_comparison_patterns = ["does", "do", "will", "can"]
comparison_keywords = ["go with", "match", "work with", "pair with", "combine with"]
color_suggestion_patterns = ["what color", "which color", "colors go with", "colors match", "better color", "color to match"]
has_two_colors = len(extract_colors_from_query(message)) >= 2
if any(pattern in message_lower for pattern in color_comparison_patterns) and any(kw in message_lower for kw in comparison_keywords) and has_two_colors:
return "color_compatibility"
outfit_request_patterns = ["suggest", "recommend", "outfit", "wear", "dress", "thinking of", "what should i wear", "what can i wear", "what to wear"]
what_matches_patterns = ["what will go", "what goes with", "what matches", "what will match", "what can go"]
if any(pattern in message_lower for pattern in what_matches_patterns) and any(item in message_lower for item in CLOTHING_TYPES):
return "outfit_suggestion"
if any(pattern in message_lower for pattern in outfit_request_patterns):
return "outfit_suggestion"
if any(pattern in message_lower for pattern in color_suggestion_patterns):
return "color_suggestion"
if any(ct in message_lower for ct in CLOTHING_TYPES) and any(c in message_lower for c in ["with", "match", "go", "pair", "style", "stylish", "look"]):
return "outfit_suggestion"
if len(extract_colors_from_query(message)) > 0 and any(word in message_lower for word in ["match", "with", "go", "pair"]):
return "color_suggestion"
if any(ct in message_lower for ct in CLOTHING_TYPES):
return "outfit_suggestion"
return "outfit_suggestion"
def is_greeting(message: str) -> bool:
message_lower = message.lower().strip()
greetings = [
"hello", "hi", "hey", "good morning", "good afternoon", "good evening",
"greetings", "howdy", "what's up", "whats up", "sup", "yo"
]
return any(message_lower.startswith(g) or message_lower == g for g in greetings)
def is_name_question(message: str) -> bool:
message_lower = message.lower().strip()
name_patterns = [
"what is your name", "what's your name", "whats your name",
"who are you", "what are you", "tell me your name", "your name"
]
return any(pattern in message_lower for pattern in name_patterns)