|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Demo: How semantic naming helps the Harmonizer |
| 4 | +Shows practical application of the validated mixing formula |
| 5 | +""" |
| 6 | + |
| 7 | +from harmonizer.divine_invitation_engine_V2 import DivineInvitationSemanticEngine, Coordinates |
| 8 | +from harmonizer.semantic_naming import SemanticNamingEngine |
| 9 | + |
| 10 | + |
| 11 | +def demo_1_detect_and_suggest(): |
| 12 | + """Demo 1: Detect bad name and suggest better ones""" |
| 13 | + print("=" * 70) |
| 14 | + print("DEMO 1: DETECT BAD NAME + SUGGEST IMPROVEMENTS") |
| 15 | + print("=" * 70) |
| 16 | + |
| 17 | + # Analyze a disharmonious function name |
| 18 | + engine = DivineInvitationSemanticEngine() |
| 19 | + namer = SemanticNamingEngine() |
| 20 | + |
| 21 | + # Bad example: function name says "get" but does "delete" |
| 22 | + print("\n❌ BAD CODE:") |
| 23 | + print("def get_user(user_id):") |
| 24 | + print(" database.delete(user_id)") |
| 25 | + print(" return True") |
| 26 | + |
| 27 | + # Analyze intent vs execution |
| 28 | + intent_result = engine.analyze_text("get user") |
| 29 | + execution_result = engine.analyze_text("delete") |
| 30 | + |
| 31 | + print(f"\nIntent (function name): {intent_result.coordinates}") |
| 32 | + print(f"Execution (body): {execution_result.coordinates}") |
| 33 | + |
| 34 | + distance = engine.get_distance(intent_result.coordinates, execution_result.coordinates) |
| 35 | + print(f"Disharmony distance: {distance:.3f} ⚠️ HIGH!") |
| 36 | + |
| 37 | + # Suggest better names based on actual execution |
| 38 | + print("\n✅ SUGGESTED FIXES based on execution semantics:") |
| 39 | + suggestions = namer.suggest_names(execution_result.coordinates, context="user", top_n=5) |
| 40 | + |
| 41 | + for name, score in suggestions: |
| 42 | + print(f" • {name:25s} (match: {score:.1%})") |
| 43 | + |
| 44 | + |
| 45 | +def demo_2_generate_from_intent(): |
| 46 | + """Demo 2: Generate function structure from semantic intent""" |
| 47 | + print("\n" + "=" * 70) |
| 48 | + print("DEMO 2: GENERATE FUNCTION FROM SEMANTIC INTENT") |
| 49 | + print("=" * 70) |
| 50 | + |
| 51 | + engine = DivineInvitationSemanticEngine() |
| 52 | + namer = SemanticNamingEngine() |
| 53 | + |
| 54 | + # User describes what they want |
| 55 | + intent = "validate user input compassionately" |
| 56 | + print(f"\n📝 User intent: '{intent}'") |
| 57 | + |
| 58 | + # Analyze semantic profile |
| 59 | + result = engine.analyze_text(intent) |
| 60 | + print(f"Semantic profile: {result.coordinates}") |
| 61 | + print(f"Explanation: {namer.explain_coordinates(result.coordinates)}") |
| 62 | + |
| 63 | + # Suggest function names |
| 64 | + print("\n💡 Suggested function names:") |
| 65 | + suggestions = namer.suggest_names(result.coordinates, context="input", top_n=3) |
| 66 | + for name, score in suggestions: |
| 67 | + print(f" • {name} (match: {score:.1%})") |
| 68 | + |
| 69 | + # Show what the function should do |
| 70 | + print("\n📋 Generated function skeleton:") |
| 71 | + print(f""" |
| 72 | +def {suggestions[0][0]}(user_input): |
| 73 | + ''' |
| 74 | + {namer.explain_coordinates(result.coordinates)} |
| 75 | + ''' |
| 76 | + # Justice component: Validation logic |
| 77 | + if not user_input: |
| 78 | + # Love component: Helpful error message |
| 79 | + raise ValueError("Please provide valid input") |
| 80 | +
|
| 81 | + # Wisdom component: Check against rules |
| 82 | + if not meets_requirements(user_input): |
| 83 | + # Love component: Explain what's wrong |
| 84 | + return ValidationResult( |
| 85 | + valid=False, |
| 86 | + message="Your input needs: [specific guidance]" |
| 87 | + ) |
| 88 | +
|
| 89 | + return ValidationResult(valid=True) |
| 90 | +""") |
| 91 | + |
| 92 | + |
| 93 | +def demo_3_refactoring_suggestion(): |
| 94 | + """Demo 3: Suggest refactoring with semantic names""" |
| 95 | + print("=" * 70) |
| 96 | + print("DEMO 3: SEMANTIC-AWARE REFACTORING") |
| 97 | + print("=" * 70) |
| 98 | + |
| 99 | + engine = DivineInvitationSemanticEngine() |
| 100 | + namer = SemanticNamingEngine() |
| 101 | + |
| 102 | + # Analyze a multi-purpose function |
| 103 | + print("\n⚠️ PROBLEMATIC CODE (does too much):") |
| 104 | + print(""" |
| 105 | +def process_user(user_data): |
| 106 | + # Validation (Justice) |
| 107 | + if not user_data.is_valid: |
| 108 | + raise ValueError("Invalid data") |
| 109 | +
|
| 110 | + # Data transformation (Power) |
| 111 | + transformed = transform_data(user_data) |
| 112 | +
|
| 113 | + # Storage (Power) |
| 114 | + save_to_database(transformed) |
| 115 | +
|
| 116 | + # Notification (Love) |
| 117 | + send_welcome_email(user_data.email) |
| 118 | +
|
| 119 | + # Analytics (Wisdom) |
| 120 | + log_user_metrics(user_data) |
| 121 | +""") |
| 122 | + |
| 123 | + # Analyze each operation |
| 124 | + operations = { |
| 125 | + 'validation': 'validate user data', |
| 126 | + 'transformation': 'transform data', |
| 127 | + 'storage': 'save database', |
| 128 | + 'notification': 'send email', |
| 129 | + 'analytics': 'log metrics' |
| 130 | + } |
| 131 | + |
| 132 | + print("\n🔍 SEMANTIC ANALYSIS OF OPERATIONS:") |
| 133 | + coords_map = {} |
| 134 | + for op_name, op_text in operations.items(): |
| 135 | + result = engine.analyze_text(op_text) |
| 136 | + coords_map[op_name] = result.coordinates |
| 137 | + dominant = namer._get_dominant_dimension(result.coordinates) |
| 138 | + print(f" {op_name:15s}: {result.coordinates} [{dominant.upper()}]") |
| 139 | + |
| 140 | + # Suggest splitting |
| 141 | + print("\n✅ SUGGESTED REFACTORING:") |
| 142 | + print("\nSplit into semantically coherent functions:") |
| 143 | + |
| 144 | + # Group by semantic similarity |
| 145 | + groups = { |
| 146 | + 'validation': ['validation'], |
| 147 | + 'processing': ['transformation', 'storage'], |
| 148 | + 'notification': ['notification', 'analytics'] |
| 149 | + } |
| 150 | + |
| 151 | + for group_name, ops in groups.items(): |
| 152 | + print(f"\n{group_name.upper()} GROUP:") |
| 153 | + # Get average coordinates for this group |
| 154 | + group_coords = Coordinates( |
| 155 | + love=sum(coords_map[op].love for op in ops) / len(ops), |
| 156 | + justice=sum(coords_map[op].justice for op in ops) / len(ops), |
| 157 | + power=sum(coords_map[op].power for op in ops) / len(ops), |
| 158 | + wisdom=sum(coords_map[op].wisdom for op in ops) / len(ops) |
| 159 | + ) |
| 160 | + |
| 161 | + suggestions = namer.suggest_names(group_coords, context="user", top_n=3) |
| 162 | + print(f" Suggested names:") |
| 163 | + for name, score in suggestions: |
| 164 | + print(f" • {name} (match: {score:.1%})") |
| 165 | + |
| 166 | + |
| 167 | +def demo_4_code_review(): |
| 168 | + """Demo 4: Automated semantic code review""" |
| 169 | + print("\n" + "=" * 70) |
| 170 | + print("DEMO 4: SEMANTIC CODE REVIEW") |
| 171 | + print("=" * 70) |
| 172 | + |
| 173 | + engine = DivineInvitationSemanticEngine() |
| 174 | + namer = SemanticNamingEngine() |
| 175 | + |
| 176 | + # Check if function name matches execution |
| 177 | + functions_to_review = [ |
| 178 | + ("calculate_total", "sum values"), # ✅ Good match |
| 179 | + ("process_data", "delete records"), # ❌ Mismatch |
| 180 | + ("validate_input", "check rules"), # ✅ Good match |
| 181 | + ("get_user", "create user"), # ❌ Mismatch |
| 182 | + ] |
| 183 | + |
| 184 | + print("\n📊 REVIEWING FUNCTION NAMES:\n") |
| 185 | + |
| 186 | + for func_name, execution in functions_to_review: |
| 187 | + intent = engine.analyze_text(func_name) |
| 188 | + actual = engine.analyze_text(execution) |
| 189 | + distance = engine.get_distance(intent.coordinates, actual.coordinates) |
| 190 | + |
| 191 | + if distance < 0.3: |
| 192 | + status = "✅ GOOD" |
| 193 | + icon = "✅" |
| 194 | + elif distance < 0.7: |
| 195 | + status = "⚠️ REVIEW" |
| 196 | + icon = "⚠️" |
| 197 | + else: |
| 198 | + status = "❌ BAD" |
| 199 | + icon = "❌" |
| 200 | + |
| 201 | + print(f"{icon} {func_name:20s} ({execution:20s})") |
| 202 | + print(f" Disharmony: {distance:.3f} - {status}") |
| 203 | + |
| 204 | + if distance > 0.3: |
| 205 | + # Suggest better name |
| 206 | + suggestions = namer.suggest_names(actual.coordinates, top_n=1) |
| 207 | + if suggestions: |
| 208 | + print(f" 💡 Suggest: {suggestions[0][0]} (match: {suggestions[0][1]:.1%})") |
| 209 | + print() |
| 210 | + |
| 211 | + |
| 212 | +if __name__ == "__main__": |
| 213 | + print("\n🎯 SEMANTIC NAMING ENGINE DEMONSTRATIONS") |
| 214 | + print("Showing how the validated mixing formula helps the Harmonizer\n") |
| 215 | + |
| 216 | + demo_1_detect_and_suggest() |
| 217 | + demo_2_generate_from_intent() |
| 218 | + demo_3_refactoring_suggestion() |
| 219 | + demo_4_code_review() |
| 220 | + |
| 221 | + print("\n" + "=" * 70) |
| 222 | + print("✅ DEMOS COMPLETE") |
| 223 | + print("=" * 70) |
| 224 | + print("\nThese demos show how the mixing formula enables:") |
| 225 | + print(" 1. Better refactoring suggestions") |
| 226 | + print(" 2. Function name generation from intent") |
| 227 | + print(" 3. Semantic-aware code splitting") |
| 228 | + print(" 4. Automated code review with suggestions") |
| 229 | + print("\nAll powered by the validated LJWP mixing formula! 🚀") |
0 commit comments