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/cip/core.py
1#!/usr/bin/env python32# -*- coding: utf-8 -*-3"""4CIP Design Core - BM25 search engine for Corporate Identity Program design guidelines5"""67import csv8import re9from pathlib import Path10from math import log11from collections import defaultdict1213# ============ CONFIGURATION ============14DATA_DIR = Path(__file__).parent.parent.parent / "data" / "cip"15MAX_RESULTS = 31617CSV_CONFIG = {18"deliverable": {19"file": "deliverables.csv",20"search_cols": ["Deliverable", "Category", "Keywords", "Description", "Mockup Context"],21"output_cols": ["Deliverable", "Category", "Keywords", "Description", "Dimensions", "File Format", "Logo Placement", "Color Usage", "Typography Notes", "Mockup Context", "Best Practices", "Avoid"]22},23"style": {24"file": "styles.csv",25"search_cols": ["Style Name", "Category", "Keywords", "Description", "Mood"],26"output_cols": ["Style Name", "Category", "Keywords", "Description", "Primary Colors", "Secondary Colors", "Typography", "Materials", "Finishes", "Mood", "Best For", "Avoid For"]27},28"industry": {29"file": "industries.csv",30"search_cols": ["Industry", "Keywords", "CIP Style", "Mood"],31"output_cols": ["Industry", "Keywords", "CIP Style", "Primary Colors", "Secondary Colors", "Typography", "Key Deliverables", "Mood", "Best Practices", "Avoid"]32},33"mockup": {34"file": "mockup-contexts.csv",35"search_cols": ["Context Name", "Category", "Keywords", "Scene Description"],36"output_cols": ["Context Name", "Category", "Keywords", "Scene Description", "Lighting", "Environment", "Props", "Camera Angle", "Background", "Style Notes", "Best For", "Prompt Modifiers"]37}38}394041# ============ BM25 IMPLEMENTATION ============42class BM25:43"""BM25 ranking algorithm for text search"""4445def __init__(self, k1=1.5, b=0.75):46self.k1 = k147self.b = b48self.corpus = []49self.doc_lengths = []50self.avgdl = 051self.idf = {}52self.doc_freqs = defaultdict(int)53self.N = 05455def tokenize(self, text):56"""Lowercase, split, remove punctuation, filter short words"""57text = re.sub(r'[^\w\s]', ' ', str(text).lower())58return [w for w in text.split() if len(w) > 2]5960def fit(self, documents):61"""Build BM25 index from documents"""62self.corpus = [self.tokenize(doc) for doc in documents]63self.N = len(self.corpus)64if self.N == 0:65return66self.doc_lengths = [len(doc) for doc in self.corpus]67self.avgdl = sum(self.doc_lengths) / self.N6869for doc in self.corpus:70seen = set()71for word in doc:72if word not in seen:73self.doc_freqs[word] += 174seen.add(word)7576for word, freq in self.doc_freqs.items():77self.idf[word] = log((self.N - freq + 0.5) / (freq + 0.5) + 1)7879def score(self, query):80"""Score all documents against query"""81query_tokens = self.tokenize(query)82scores = []8384for idx, doc in enumerate(self.corpus):85score = 086doc_len = self.doc_lengths[idx]87term_freqs = defaultdict(int)88for word in doc:89term_freqs[word] += 19091for token in query_tokens:92if token in self.idf:93tf = term_freqs[token]94idf = self.idf[token]95numerator = tf * (self.k1 + 1)96denominator = tf + self.k1 * (1 - self.b + self.b * doc_len / self.avgdl)97score += idf * numerator / denominator9899scores.append((idx, score))100101return sorted(scores, key=lambda x: x[1], reverse=True)102103104# ============ SEARCH FUNCTIONS ============105def _load_csv(filepath):106"""Load CSV and return list of dicts"""107with open(filepath, 'r', encoding='utf-8') as f:108return list(csv.DictReader(f))109110111def _search_csv(filepath, search_cols, output_cols, query, max_results):112"""Core search function using BM25"""113if not filepath.exists():114return []115116data = _load_csv(filepath)117118# Build documents from search columns119documents = [" ".join(str(row.get(col, "")) for col in search_cols) for row in data]120121# BM25 search122bm25 = BM25()123bm25.fit(documents)124ranked = bm25.score(query)125126# Get top results with score > 0127results = []128for idx, score in ranked[:max_results]:129if score > 0:130row = data[idx]131results.append({col: row.get(col, "") for col in output_cols if col in row})132133return results134135136def detect_domain(query):137"""Auto-detect the most relevant domain from query"""138query_lower = query.lower()139140domain_keywords = {141"deliverable": ["card", "letterhead", "envelope", "folder", "shirt", "cap", "badge", "signage", "vehicle", "car", "van", "stationery", "uniform", "merchandise", "packaging", "banner", "booth"],142"style": ["style", "minimal", "modern", "luxury", "vintage", "industrial", "elegant", "bold", "corporate", "organic", "playful"],143"industry": ["tech", "finance", "legal", "healthcare", "hospitality", "food", "fashion", "retail", "construction", "logistics"],144"mockup": ["mockup", "scene", "context", "photo", "shot", "lighting", "background", "studio", "lifestyle"]145}146147scores = {domain: sum(1 for kw in keywords if kw in query_lower) for domain, keywords in domain_keywords.items()}148best = max(scores, key=scores.get)149return best if scores[best] > 0 else "deliverable"150151152def search(query, domain=None, max_results=MAX_RESULTS):153"""Main search function with auto-domain detection"""154if domain is None:155domain = detect_domain(query)156157config = CSV_CONFIG.get(domain, CSV_CONFIG["deliverable"])158filepath = DATA_DIR / config["file"]159160if not filepath.exists():161return {"error": f"File not found: {filepath}", "domain": domain}162163results = _search_csv(filepath, config["search_cols"], config["output_cols"], query, max_results)164165return {166"domain": domain,167"query": query,168"file": config["file"],169"count": len(results),170"results": results171}172173174def search_all(query, max_results=2):175"""Search across all domains and combine results"""176all_results = {}177for domain in CSV_CONFIG.keys():178result = search(query, domain, max_results)179if result.get("results"):180all_results[domain] = result["results"]181return all_results182183184def get_cip_brief(brand_name, industry_query, style_query=None):185"""Generate a comprehensive CIP brief for a brand"""186# Search industry187industry_results = search(industry_query, "industry", 1)188industry = industry_results.get("results", [{}])[0] if industry_results.get("results") else {}189190# Search style (use industry style if not specified)191style_query = style_query or industry.get("CIP Style", "corporate minimal")192style_results = search(style_query, "style", 1)193style = style_results.get("results", [{}])[0] if style_results.get("results") else {}194195# Get recommended deliverables for the industry196key_deliverables = industry.get("Key Deliverables", "").split()197deliverable_results = []198for d in key_deliverables[:5]:199result = search(d, "deliverable", 1)200if result.get("results"):201deliverable_results.append(result["results"][0])202203return {204"brand_name": brand_name,205"industry": industry,206"style": style,207"recommended_deliverables": deliverable_results,208"color_system": {209"primary": style.get("Primary Colors", industry.get("Primary Colors", "")),210"secondary": style.get("Secondary Colors", industry.get("Secondary Colors", ""))211},212"typography": style.get("Typography", industry.get("Typography", "")),213"materials": style.get("Materials", ""),214"finishes": style.get("Finishes", "")215}216