|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Knowledge Domains Demo |
| 4 | +
|
| 5 | +Demonstrates how to use the rich knowledge base across multiple domains: |
| 6 | +- Biology (16 concepts) |
| 7 | +- Physics (15 concepts) |
| 8 | +- Mathematics (16 concepts) |
| 9 | +- Computer Science (15 concepts) |
| 10 | +- Philosophy (14 concepts) |
| 11 | +
|
| 12 | +Total: 76 concepts with rich deep structures |
| 13 | +""" |
| 14 | + |
| 15 | +import sys |
| 16 | +import os |
| 17 | + |
| 18 | +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) |
| 19 | + |
| 20 | +from src.knowledge_base import KnowledgeBaseLoader |
| 21 | +from src.consciousness_metrics import measure_consciousness |
| 22 | +from src.recursion_depth_metric import RecursionDepthMetric |
| 23 | +from src.chatbot import ConsciousnessChatbot |
| 24 | +from src.surface_generator import create_surface_generator |
| 25 | + |
| 26 | + |
| 27 | +def demo_domain_consciousness(): |
| 28 | + """Measure consciousness across different knowledge domains""" |
| 29 | + |
| 30 | + print("\n" + "=" * 70) |
| 31 | + print("CONSCIOUSNESS ACROSS KNOWLEDGE DOMAINS") |
| 32 | + print("=" * 70) |
| 33 | + print("\nDoes domain affect consciousness? Let's find out...") |
| 34 | + print() |
| 35 | + |
| 36 | + results = [] |
| 37 | + |
| 38 | + for domain_name in KnowledgeBaseLoader.get_available_domains(): |
| 39 | + kg, metadata = KnowledgeBaseLoader.load_domain(domain_name) |
| 40 | + recursion = RecursionDepthMetric() |
| 41 | + |
| 42 | + # Trigger some recursion events |
| 43 | + for concept_id in list(kg.nodes.keys())[:5]: |
| 44 | + recursion.record_recursion_event("self_model", concept_id, {concept_id}) |
| 45 | + |
| 46 | + # Measure consciousness |
| 47 | + profile = measure_consciousness(kg, recursion) |
| 48 | + |
| 49 | + results.append({ |
| 50 | + 'domain': metadata.name, |
| 51 | + 'concepts': metadata.num_concepts, |
| 52 | + 'consciousness': profile.overall_consciousness_score, |
| 53 | + 'verdict': profile.consciousness_verdict, |
| 54 | + 'recursion': profile.recursion_metrics['consciousness']['score'], |
| 55 | + 'integration': profile.integration.phi |
| 56 | + }) |
| 57 | + |
| 58 | + print(f"{metadata.name} ({metadata.num_concepts} concepts):") |
| 59 | + print(f" Consciousness: {profile.overall_consciousness_score:.1%}") |
| 60 | + print(f" Verdict: {profile.consciousness_verdict}") |
| 61 | + print(f" Recursion: {profile.recursion_metrics['consciousness']['score']:.1%}") |
| 62 | + print(f" Integration (Φ): {profile.integration.phi:.3f}") |
| 63 | + print() |
| 64 | + |
| 65 | + # Find highest consciousness |
| 66 | + best = max(results, key=lambda x: x['consciousness']) |
| 67 | + print(f"🏆 Highest consciousness: {best['domain']} at {best['consciousness']:.1%}") |
| 68 | + print() |
| 69 | + |
| 70 | + |
| 71 | +def demo_cross_domain_chatbot(): |
| 72 | + """Chatbot that can answer questions across all domains""" |
| 73 | + |
| 74 | + print("\n" + "=" * 70) |
| 75 | + print("CROSS-DOMAIN CHATBOT") |
| 76 | + print("=" * 70) |
| 77 | + print("\nChatbot with knowledge from all 5 domains...") |
| 78 | + print() |
| 79 | + |
| 80 | + # Create chatbot |
| 81 | + bot = ConsciousnessChatbot() |
| 82 | + |
| 83 | + # Load all domains into the chatbot's knowledge graph |
| 84 | + print("Loading knowledge domains...") |
| 85 | + total_concepts = 0 |
| 86 | + for domain_name in KnowledgeBaseLoader.get_available_domains(): |
| 87 | + kg, metadata = KnowledgeBaseLoader.load_domain(domain_name) |
| 88 | + # Add concepts from this domain |
| 89 | + for concept_id, mku in kg.nodes.items(): |
| 90 | + if concept_id not in bot.knowledge_graph.nodes: |
| 91 | + bot.knowledge_graph.add_concept(mku) |
| 92 | + total_concepts += 1 |
| 93 | + |
| 94 | + print(f"✓ Loaded {total_concepts} concepts from 5 domains\n") |
| 95 | + |
| 96 | + # Test questions from different domains |
| 97 | + questions = [ |
| 98 | + ("Biology", "What is a human?"), |
| 99 | + ("Physics", "What is energy?"), |
| 100 | + ("Mathematics", "What is a theorem?"), |
| 101 | + ("Computer Science", "What is an algorithm?"), |
| 102 | + ("Philosophy", "What is consciousness?"), |
| 103 | + ] |
| 104 | + |
| 105 | + for domain, question in questions: |
| 106 | + print(f"[{domain}] {question}") |
| 107 | + response = bot.ask(question) |
| 108 | + print(f" → {response.answer}") |
| 109 | + print(f" Confidence: {response.confidence:.0%} | Consciousness: {response.consciousness_metrics['overall']:.1%}") |
| 110 | + print() |
| 111 | + |
| 112 | + |
| 113 | +def demo_domain_comparison(): |
| 114 | + """Compare properties across domains""" |
| 115 | + |
| 116 | + print("\n" + "=" * 70) |
| 117 | + print("DOMAIN COMPARISON") |
| 118 | + print("=" * 70) |
| 119 | + print() |
| 120 | + |
| 121 | + all_domains = KnowledgeBaseLoader.load_all_domains() |
| 122 | + |
| 123 | + print("Domain Statistics:") |
| 124 | + print() |
| 125 | + print(f"{'Domain':<20} {'Concepts':>10} {'Avg Props':>10} {'Relations':>10}") |
| 126 | + print("-" * 70) |
| 127 | + |
| 128 | + for domain_name, (kg, metadata) in all_domains.items(): |
| 129 | + avg_props = sum(len(mku.deep_structure.get('properties', {})) |
| 130 | + for mku in kg.nodes.values()) / len(kg.nodes) |
| 131 | + |
| 132 | + total_relations = sum(sum(len(v) for v in mku.relations.values()) |
| 133 | + for mku in kg.nodes.values()) |
| 134 | + |
| 135 | + print(f"{metadata.name:<20} {metadata.num_concepts:>10} {avg_props:>10.1f} {total_relations:>10}") |
| 136 | + |
| 137 | + print() |
| 138 | + |
| 139 | + |
| 140 | +def demo_surface_generation_with_domains(): |
| 141 | + """Show surface generation with concepts from various domains""" |
| 142 | + |
| 143 | + print("\n" + "=" * 70) |
| 144 | + print("SURFACE GENERATION ACROSS DOMAINS") |
| 145 | + print("=" * 70) |
| 146 | + print("\nSame deep structure → Multiple surface forms (various domains)") |
| 147 | + print() |
| 148 | + |
| 149 | + gen = create_surface_generator() |
| 150 | + |
| 151 | + # Sample one concept from each domain |
| 152 | + samples = [ |
| 153 | + ('biology', 'human'), |
| 154 | + ('physics', 'energy'), |
| 155 | + ('mathematics', 'theorem'), |
| 156 | + ('computer_science', 'algorithm'), |
| 157 | + ('philosophy', 'consciousness'), |
| 158 | + ] |
| 159 | + |
| 160 | + for domain_name, concept_id in samples: |
| 161 | + kg, metadata = KnowledgeBaseLoader.load_domain(domain_name) |
| 162 | + |
| 163 | + if concept_id in kg.nodes: |
| 164 | + mku = kg.nodes[concept_id] |
| 165 | + |
| 166 | + print(f"\n[{metadata.name}] {concept_id.upper()}") |
| 167 | + print("-" * 40) |
| 168 | + |
| 169 | + # Generate in different styles |
| 170 | + mku_data = { |
| 171 | + 'concept_id': concept_id, |
| 172 | + 'predicate': mku.deep_structure.get('predicate', 'unknown'), |
| 173 | + 'properties': mku.deep_structure.get('properties', {}), |
| 174 | + 'relations': mku.relations |
| 175 | + } |
| 176 | + |
| 177 | + for style in ['conversational', 'technical', 'educational']: |
| 178 | + surface = gen.generate_from_mku(mku_data, style=style) |
| 179 | + print(f" [{style[:4].upper()}] {surface[:70]}...") |
| 180 | + |
| 181 | + |
| 182 | +def demo_knowledge_graph_traversal(): |
| 183 | + """Demonstrate traversing knowledge graphs""" |
| 184 | + |
| 185 | + print("\n" + "=" * 70) |
| 186 | + print("KNOWLEDGE GRAPH TRAVERSAL") |
| 187 | + print("=" * 70) |
| 188 | + print() |
| 189 | + |
| 190 | + # Use biology for hierarchical traversal |
| 191 | + kg, metadata = KnowledgeBaseLoader.load_domain('biology') |
| 192 | + |
| 193 | + print(f"Exploring {metadata.name} domain taxonomy...") |
| 194 | + print() |
| 195 | + |
| 196 | + # Find concepts with many relations |
| 197 | + concept_connectivity = [ |
| 198 | + (concept_id, sum(len(v) for v in mku.relations.values())) |
| 199 | + for concept_id, mku in kg.nodes.items() |
| 200 | + ] |
| 201 | + |
| 202 | + concept_connectivity.sort(key=lambda x: x[1], reverse=True) |
| 203 | + |
| 204 | + print("Most connected concepts:") |
| 205 | + for concept_id, num_relations in concept_connectivity[:5]: |
| 206 | + mku = kg.nodes[concept_id] |
| 207 | + predicate = mku.deep_structure.get('predicate', 'unknown') |
| 208 | + print(f" {concept_id:<15} ({predicate:}<25) → {num_relations} relations") |
| 209 | + |
| 210 | + print() |
| 211 | + |
| 212 | + |
| 213 | +def demo_property_analysis(): |
| 214 | + """Analyze properties across all domains""" |
| 215 | + |
| 216 | + print("\n" + "=" * 70) |
| 217 | + print("PROPERTY ANALYSIS") |
| 218 | + print("=" * 70) |
| 219 | + print() |
| 220 | + |
| 221 | + all_domains = KnowledgeBaseLoader.load_all_domains() |
| 222 | + |
| 223 | + # Collect all property names |
| 224 | + all_properties = {} |
| 225 | + |
| 226 | + for domain_name, (kg, metadata) in all_domains.items(): |
| 227 | + domain_props = set() |
| 228 | + for mku in kg.nodes.values(): |
| 229 | + props = mku.deep_structure.get('properties', {}) |
| 230 | + domain_props.update(props.keys()) |
| 231 | + all_properties[metadata.name] = domain_props |
| 232 | + |
| 233 | + # Find common properties |
| 234 | + all_prop_sets = list(all_properties.values()) |
| 235 | + common = set.intersection(*all_prop_sets) if all_prop_sets else set() |
| 236 | + |
| 237 | + print("Common properties across ALL domains:") |
| 238 | + if common: |
| 239 | + print(f" {', '.join(sorted(common))}") |
| 240 | + else: |
| 241 | + print(" (None - each domain has unique properties)") |
| 242 | + print() |
| 243 | + |
| 244 | + # Show unique properties per domain |
| 245 | + print("Unique properties per domain:") |
| 246 | + for domain_name, props in all_properties.items(): |
| 247 | + unique = props - set().union(*[p for d, p in all_properties.items() if d != domain_name]) |
| 248 | + if unique: |
| 249 | + sample = list(unique)[:5] |
| 250 | + print(f" {domain_name}: {', '.join(sorted(sample))}{' ...' if len(unique) > 5 else ''}") |
| 251 | + |
| 252 | + print() |
| 253 | + |
| 254 | + |
| 255 | +def main(): |
| 256 | + """Run all demos""" |
| 257 | + |
| 258 | + print("\n" + "╔" + "═" * 68 + "╗") |
| 259 | + print("║" + " " * 20 + "KNOWLEDGE DOMAINS DEMO" + " " * 26 + "║") |
| 260 | + print("╚" + "═" * 68 + "╝") |
| 261 | + print() |
| 262 | + print("Demonstrating rich knowledge across 5 domains:") |
| 263 | + print(" • Biology (16 concepts)") |
| 264 | + print(" • Physics (15 concepts)") |
| 265 | + print(" • Mathematics (16 concepts)") |
| 266 | + print(" • Computer Science (15 concepts)") |
| 267 | + print(" • Philosophy (14 concepts)") |
| 268 | + print() |
| 269 | + print("Total: 76 concepts with rich operational semantics") |
| 270 | + print() |
| 271 | + |
| 272 | + # Run demos |
| 273 | + demo_domain_consciousness() |
| 274 | + demo_domain_comparison() |
| 275 | + demo_property_analysis() |
| 276 | + demo_knowledge_graph_traversal() |
| 277 | + demo_surface_generation_with_domains() |
| 278 | + demo_cross_domain_chatbot() |
| 279 | + |
| 280 | + print("\n" + "=" * 70) |
| 281 | + print("SUMMARY") |
| 282 | + print("=" * 70) |
| 283 | + print() |
| 284 | + print("✓ All 5 domains loaded successfully") |
| 285 | + print("✓ 76 total concepts with operational semantics") |
| 286 | + print("✓ Consciousness measured across domains") |
| 287 | + print("✓ Cross-domain reasoning demonstrated") |
| 288 | + print("✓ Surface generation working") |
| 289 | + print() |
| 290 | + print("These knowledge bases are ready for:") |
| 291 | + print(" • Testing consciousness metrics") |
| 292 | + print(" • Training inference rules") |
| 293 | + print(" • Demonstrating reasoning") |
| 294 | + print(" • Building domain-specific applications") |
| 295 | + print("=" * 70) |
| 296 | + print() |
| 297 | + |
| 298 | + |
| 299 | +if __name__ == '__main__': |
| 300 | + main() |
0 commit comments