core.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. UI/UX Pro Max Core - BM25 search engine for UI/UX style guides
  5. """
  6. import csv
  7. import re
  8. from pathlib import Path
  9. from math import log
  10. from collections import defaultdict
  11. # ============ CONFIGURATION ============
  12. DATA_DIR = Path(__file__).parent.parent / "data"
  13. MAX_RESULTS = 3
  14. CSV_CONFIG = {
  15. "style": {
  16. "file": "styles.csv",
  17. "search_cols": ["Style Category", "Keywords", "Best For", "Type", "AI Prompt Keywords"],
  18. "output_cols": ["Style Category", "Type", "Keywords", "Primary Colors", "Effects & Animation", "Best For", "Performance", "Accessibility", "Framework Compatibility", "Complexity", "AI Prompt Keywords", "CSS/Technical Keywords", "Implementation Checklist", "Design System Variables"]
  19. },
  20. "color": {
  21. "file": "colors.csv",
  22. "search_cols": ["Product Type", "Notes"],
  23. "output_cols": ["Product Type", "Primary (Hex)", "Secondary (Hex)", "CTA (Hex)", "Background (Hex)", "Text (Hex)", "Notes"]
  24. },
  25. "chart": {
  26. "file": "charts.csv",
  27. "search_cols": ["Data Type", "Keywords", "Best Chart Type", "Accessibility Notes"],
  28. "output_cols": ["Data Type", "Keywords", "Best Chart Type", "Secondary Options", "Color Guidance", "Accessibility Notes", "Library Recommendation", "Interactive Level"]
  29. },
  30. "landing": {
  31. "file": "landing.csv",
  32. "search_cols": ["Pattern Name", "Keywords", "Conversion Optimization", "Section Order"],
  33. "output_cols": ["Pattern Name", "Keywords", "Section Order", "Primary CTA Placement", "Color Strategy", "Conversion Optimization"]
  34. },
  35. "product": {
  36. "file": "products.csv",
  37. "search_cols": ["Product Type", "Keywords", "Primary Style Recommendation", "Key Considerations"],
  38. "output_cols": ["Product Type", "Keywords", "Primary Style Recommendation", "Secondary Styles", "Landing Page Pattern", "Dashboard Style (if applicable)", "Color Palette Focus"]
  39. },
  40. "ux": {
  41. "file": "ux-guidelines.csv",
  42. "search_cols": ["Category", "Issue", "Description", "Platform"],
  43. "output_cols": ["Category", "Issue", "Platform", "Description", "Do", "Don't", "Code Example Good", "Code Example Bad", "Severity"]
  44. },
  45. "typography": {
  46. "file": "typography.csv",
  47. "search_cols": ["Font Pairing Name", "Category", "Mood/Style Keywords", "Best For", "Heading Font", "Body Font"],
  48. "output_cols": ["Font Pairing Name", "Category", "Heading Font", "Body Font", "Mood/Style Keywords", "Best For", "Google Fonts URL", "CSS Import", "Tailwind Config", "Notes"]
  49. },
  50. "icons": {
  51. "file": "icons.csv",
  52. "search_cols": ["Category", "Icon Name", "Keywords", "Best For"],
  53. "output_cols": ["Category", "Icon Name", "Keywords", "Library", "Import Code", "Usage", "Best For", "Style"]
  54. },
  55. "react": {
  56. "file": "react-performance.csv",
  57. "search_cols": ["Category", "Issue", "Keywords", "Description"],
  58. "output_cols": ["Category", "Issue", "Platform", "Description", "Do", "Don't", "Code Example Good", "Code Example Bad", "Severity"]
  59. },
  60. "web": {
  61. "file": "web-interface.csv",
  62. "search_cols": ["Category", "Issue", "Keywords", "Description"],
  63. "output_cols": ["Category", "Issue", "Platform", "Description", "Do", "Don't", "Code Example Good", "Code Example Bad", "Severity"]
  64. }
  65. }
  66. STACK_CONFIG = {
  67. "html-tailwind": {"file": "stacks/html-tailwind.csv"},
  68. "react": {"file": "stacks/react.csv"},
  69. "nextjs": {"file": "stacks/nextjs.csv"},
  70. "astro": {"file": "stacks/astro.csv"},
  71. "vue": {"file": "stacks/vue.csv"},
  72. "nuxtjs": {"file": "stacks/nuxtjs.csv"},
  73. "nuxt-ui": {"file": "stacks/nuxt-ui.csv"},
  74. "svelte": {"file": "stacks/svelte.csv"},
  75. "swiftui": {"file": "stacks/swiftui.csv"},
  76. "react-native": {"file": "stacks/react-native.csv"},
  77. "flutter": {"file": "stacks/flutter.csv"},
  78. "shadcn": {"file": "stacks/shadcn.csv"},
  79. "jetpack-compose": {"file": "stacks/jetpack-compose.csv"}
  80. }
  81. # Common columns for all stacks
  82. _STACK_COLS = {
  83. "search_cols": ["Category", "Guideline", "Description", "Do", "Don't"],
  84. "output_cols": ["Category", "Guideline", "Description", "Do", "Don't", "Code Good", "Code Bad", "Severity", "Docs URL"]
  85. }
  86. AVAILABLE_STACKS = list(STACK_CONFIG.keys())
  87. # ============ BM25 IMPLEMENTATION ============
  88. class BM25:
  89. """BM25 ranking algorithm for text search"""
  90. def __init__(self, k1=1.5, b=0.75):
  91. self.k1 = k1
  92. self.b = b
  93. self.corpus = []
  94. self.doc_lengths = []
  95. self.avgdl = 0
  96. self.idf = {}
  97. self.doc_freqs = defaultdict(int)
  98. self.N = 0
  99. def tokenize(self, text):
  100. """Lowercase, split, remove punctuation, filter short words"""
  101. text = re.sub(r'[^\w\s]', ' ', str(text).lower())
  102. return [w for w in text.split() if len(w) > 2]
  103. def fit(self, documents):
  104. """Build BM25 index from documents"""
  105. self.corpus = [self.tokenize(doc) for doc in documents]
  106. self.N = len(self.corpus)
  107. if self.N == 0:
  108. return
  109. self.doc_lengths = [len(doc) for doc in self.corpus]
  110. self.avgdl = sum(self.doc_lengths) / self.N
  111. for doc in self.corpus:
  112. seen = set()
  113. for word in doc:
  114. if word not in seen:
  115. self.doc_freqs[word] += 1
  116. seen.add(word)
  117. for word, freq in self.doc_freqs.items():
  118. self.idf[word] = log((self.N - freq + 0.5) / (freq + 0.5) + 1)
  119. def score(self, query):
  120. """Score all documents against query"""
  121. query_tokens = self.tokenize(query)
  122. scores = []
  123. for idx, doc in enumerate(self.corpus):
  124. score = 0
  125. doc_len = self.doc_lengths[idx]
  126. term_freqs = defaultdict(int)
  127. for word in doc:
  128. term_freqs[word] += 1
  129. for token in query_tokens:
  130. if token in self.idf:
  131. tf = term_freqs[token]
  132. idf = self.idf[token]
  133. numerator = tf * (self.k1 + 1)
  134. denominator = tf + self.k1 * (1 - self.b + self.b * doc_len / self.avgdl)
  135. score += idf * numerator / denominator
  136. scores.append((idx, score))
  137. return sorted(scores, key=lambda x: x[1], reverse=True)
  138. # ============ SEARCH FUNCTIONS ============
  139. def _load_csv(filepath):
  140. """Load CSV and return list of dicts"""
  141. with open(filepath, 'r', encoding='utf-8') as f:
  142. return list(csv.DictReader(f))
  143. def _search_csv(filepath, search_cols, output_cols, query, max_results):
  144. """Core search function using BM25"""
  145. if not filepath.exists():
  146. return []
  147. data = _load_csv(filepath)
  148. # Build documents from search columns
  149. documents = [" ".join(str(row.get(col, "")) for col in search_cols) for row in data]
  150. # BM25 search
  151. bm25 = BM25()
  152. bm25.fit(documents)
  153. ranked = bm25.score(query)
  154. # Get top results with score > 0
  155. results = []
  156. for idx, score in ranked[:max_results]:
  157. if score > 0:
  158. row = data[idx]
  159. results.append({col: row.get(col, "") for col in output_cols if col in row})
  160. return results
  161. def detect_domain(query):
  162. """Auto-detect the most relevant domain from query"""
  163. query_lower = query.lower()
  164. domain_keywords = {
  165. "color": ["color", "palette", "hex", "#", "rgb"],
  166. "chart": ["chart", "graph", "visualization", "trend", "bar", "pie", "scatter", "heatmap", "funnel"],
  167. "landing": ["landing", "page", "cta", "conversion", "hero", "testimonial", "pricing", "section"],
  168. "product": ["saas", "ecommerce", "e-commerce", "fintech", "healthcare", "gaming", "portfolio", "crypto", "dashboard"],
  169. "style": ["style", "design", "ui", "minimalism", "glassmorphism", "neumorphism", "brutalism", "dark mode", "flat", "aurora", "prompt", "css", "implementation", "variable", "checklist", "tailwind"],
  170. "ux": ["ux", "usability", "accessibility", "wcag", "touch", "scroll", "animation", "keyboard", "navigation", "mobile"],
  171. "typography": ["font", "typography", "heading", "serif", "sans"],
  172. "icons": ["icon", "icons", "lucide", "heroicons", "symbol", "glyph", "pictogram", "svg icon"],
  173. "react": ["react", "next.js", "nextjs", "suspense", "memo", "usecallback", "useeffect", "rerender", "bundle", "waterfall", "barrel", "dynamic import", "rsc", "server component"],
  174. "web": ["aria", "focus", "outline", "semantic", "virtualize", "autocomplete", "form", "input type", "preconnect"]
  175. }
  176. scores = {domain: sum(1 for kw in keywords if kw in query_lower) for domain, keywords in domain_keywords.items()}
  177. best = max(scores, key=scores.get)
  178. return best if scores[best] > 0 else "style"
  179. def search(query, domain=None, max_results=MAX_RESULTS):
  180. """Main search function with auto-domain detection"""
  181. if domain is None:
  182. domain = detect_domain(query)
  183. config = CSV_CONFIG.get(domain, CSV_CONFIG["style"])
  184. filepath = DATA_DIR / config["file"]
  185. if not filepath.exists():
  186. return {"error": f"File not found: {filepath}", "domain": domain}
  187. results = _search_csv(filepath, config["search_cols"], config["output_cols"], query, max_results)
  188. return {
  189. "domain": domain,
  190. "query": query,
  191. "file": config["file"],
  192. "count": len(results),
  193. "results": results
  194. }
  195. def search_stack(query, stack, max_results=MAX_RESULTS):
  196. """Search stack-specific guidelines"""
  197. if stack not in STACK_CONFIG:
  198. return {"error": f"Unknown stack: {stack}. Available: {', '.join(AVAILABLE_STACKS)}"}
  199. filepath = DATA_DIR / STACK_CONFIG[stack]["file"]
  200. if not filepath.exists():
  201. return {"error": f"Stack file not found: {filepath}", "stack": stack}
  202. results = _search_csv(filepath, _STACK_COLS["search_cols"], _STACK_COLS["output_cols"], query, max_results)
  203. return {
  204. "domain": "stack",
  205. "stack": stack,
  206. "query": query,
  207. "file": STACK_CONFIG[stack]["file"],
  208. "count": len(results),
  209. "results": results
  210. }