Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Comprehensive AI design skill providing design intelligence across platforms with 161 reasoning rules.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
scripts/logo/core.py
1#!/usr/bin/env python32# -*- coding: utf-8 -*-3"""4Logo Design Core - BM25 search engine for logo design guidelines5"""67import csv8import re9from pathlib import Path10from math import log11from collections import defaultdict1213# ============ CONFIGURATION ============14DATA_DIR = Path(__file__).parent.parent.parent / "data" / "logo"15MAX_RESULTS = 31617CSV_CONFIG = {18"style": {19"file": "styles.csv",20"search_cols": ["Style Name", "Category", "Keywords", "Best For"],21"output_cols": ["Style Name", "Category", "Keywords", "Primary Colors", "Secondary Colors", "Typography", "Effects", "Best For", "Avoid For", "Complexity", "Era"]22},23"color": {24"file": "colors.csv",25"search_cols": ["Palette Name", "Category", "Keywords", "Psychology", "Best For"],26"output_cols": ["Palette Name", "Category", "Keywords", "Primary Hex", "Secondary Hex", "Accent Hex", "Background Hex", "Text Hex", "Psychology", "Best For", "Avoid For"]27},28"industry": {29"file": "industries.csv",30"search_cols": ["Industry", "Keywords", "Recommended Styles", "Mood"],31"output_cols": ["Industry", "Keywords", "Recommended Styles", "Primary Colors", "Typography", "Common Symbols", "Mood", "Best Practices", "Avoid"]32}33}343536# ============ BM25 IMPLEMENTATION ============37class BM25:38"""BM25 ranking algorithm for text search"""3940def __init__(self, k1=1.5, b=0.75):41self.k1 = k142self.b = b43self.corpus = []44self.doc_lengths = []45self.avgdl = 046self.idf = {}47self.doc_freqs = defaultdict(int)48self.N = 04950def tokenize(self, text):51"""Lowercase, split, remove punctuation, filter short words"""52text = re.sub(r'[^\w\s]', ' ', str(text).lower())53return [w for w in text.split() if len(w) > 2]5455def fit(self, documents):56"""Build BM25 index from documents"""57self.corpus = [self.tokenize(doc) for doc in documents]58self.N = len(self.corpus)59if self.N == 0:60return61self.doc_lengths = [len(doc) for doc in self.corpus]62self.avgdl = sum(self.doc_lengths) / self.N6364for doc in self.corpus:65seen = set()66for word in doc:67if word not in seen:68self.doc_freqs[word] += 169seen.add(word)7071for word, freq in self.doc_freqs.items():72self.idf[word] = log((self.N - freq + 0.5) / (freq + 0.5) + 1)7374def score(self, query):75"""Score all documents against query"""76query_tokens = self.tokenize(query)77scores = []7879for idx, doc in enumerate(self.corpus):80score = 081doc_len = self.doc_lengths[idx]82term_freqs = defaultdict(int)83for word in doc:84term_freqs[word] += 18586for token in query_tokens:87if token in self.idf:88tf = term_freqs[token]89idf = self.idf[token]90numerator = tf * (self.k1 + 1)91denominator = tf + self.k1 * (1 - self.b + self.b * doc_len / self.avgdl)92score += idf * numerator / denominator9394scores.append((idx, score))9596return sorted(scores, key=lambda x: x[1], reverse=True)979899# ============ SEARCH FUNCTIONS ============100def _load_csv(filepath):101"""Load CSV and return list of dicts"""102with open(filepath, 'r', encoding='utf-8') as f:103return list(csv.DictReader(f))104105106def _search_csv(filepath, search_cols, output_cols, query, max_results):107"""Core search function using BM25"""108if not filepath.exists():109return []110111data = _load_csv(filepath)112113# Build documents from search columns114documents = [" ".join(str(row.get(col, "")) for col in search_cols) for row in data]115116# BM25 search117bm25 = BM25()118bm25.fit(documents)119ranked = bm25.score(query)120121# Get top results with score > 0122results = []123for idx, score in ranked[:max_results]:124if score > 0:125row = data[idx]126results.append({col: row.get(col, "") for col in output_cols if col in row})127128return results129130131def detect_domain(query):132"""Auto-detect the most relevant domain from query"""133query_lower = query.lower()134135domain_keywords = {136"style": ["style", "minimalist", "vintage", "modern", "retro", "geometric", "abstract", "emblem", "badge", "wordmark", "mascot", "luxury", "playful", "corporate"],137"color": ["color", "palette", "hex", "#", "rgb", "blue", "red", "green", "gold", "warm", "cool", "vibrant", "pastel"],138"industry": ["tech", "healthcare", "finance", "legal", "restaurant", "food", "fashion", "beauty", "education", "sports", "fitness", "real estate", "crypto", "gaming"]139}140141scores = {domain: sum(1 for kw in keywords if kw in query_lower) for domain, keywords in domain_keywords.items()}142best = max(scores, key=scores.get)143return best if scores[best] > 0 else "style"144145146def search(query, domain=None, max_results=MAX_RESULTS):147"""Main search function with auto-domain detection"""148if domain is None:149domain = detect_domain(query)150151config = CSV_CONFIG.get(domain, CSV_CONFIG["style"])152filepath = DATA_DIR / config["file"]153154if not filepath.exists():155return {"error": f"File not found: {filepath}", "domain": domain}156157results = _search_csv(filepath, config["search_cols"], config["output_cols"], query, max_results)158159return {160"domain": domain,161"query": query,162"file": config["file"],163"count": len(results),164"results": results165}166167168def search_all(query, max_results=2):169"""Search across all domains and combine results"""170all_results = {}171for domain in CSV_CONFIG.keys():172result = search(query, domain, max_results)173if result.get("results"):174all_results[domain] = result["results"]175return all_results176