@aleph-ai/tinyaleph 1.5.7 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. package/core/alexander-module.js +1469 -0
  2. package/core/arithmetic-link-kernel.js +1338 -0
  3. package/core/index.js +95 -2
  4. package/examples/01-hello-world.js +69 -0
  5. package/examples/02-basic-hash.js +90 -0
  6. package/examples/02-observer-stack.js +385 -0
  7. package/examples/03-quantum-coin.js +136 -0
  8. package/examples/05-symbolic-resonance.js +146 -0
  9. package/examples/06-symbol-database.js +150 -0
  10. package/examples/07-semantic-inference.js +223 -0
  11. package/examples/08-compound-symbols.js +219 -0
  12. package/examples/README.md +170 -0
  13. package/examples/ai/01-embeddings.js +155 -0
  14. package/examples/ai/02-semantic-memory.js +243 -0
  15. package/examples/ai/03-reasoning.js +243 -0
  16. package/examples/ai/04-knowledge-graph.js +279 -0
  17. package/examples/ai/05-llm-integration.js +333 -0
  18. package/examples/ai/06-agent.js +294 -0
  19. package/examples/ai/07-hybrid-ai.js +223 -0
  20. package/examples/ai/08-entropy-reasoning.js +259 -0
  21. package/examples/ai/09-concept-learning.js +271 -0
  22. package/examples/ai/10-prompt-primes.js +312 -0
  23. package/examples/ai/11-rag.js +332 -0
  24. package/examples/ai/12-neuro-symbolic.js +321 -0
  25. package/examples/ai/README.md +80 -0
  26. package/examples/arithmetic-topology/01-legendre-symbol.js +78 -0
  27. package/examples/arithmetic-topology/02-redei-symbol.js +126 -0
  28. package/examples/arithmetic-topology/03-alk-kuramoto.js +138 -0
  29. package/examples/arithmetic-topology/04-alexander-module.js +117 -0
  30. package/examples/arithmetic-topology/05-signature-memory.js +118 -0
  31. package/examples/arithmetic-topology/README.md +291 -0
  32. package/examples/bioinformatics/01-dna-encoding.js +108 -0
  33. package/examples/bioinformatics/02-central-dogma.js +162 -0
  34. package/examples/bioinformatics/03-protein-folding.js +206 -0
  35. package/examples/bioinformatics/04-dna-computing.js +192 -0
  36. package/examples/bioinformatics/05-molecular-binding.js +209 -0
  37. package/examples/chat.js +105 -0
  38. package/examples/crt-homology/01-residue-encoding.js +87 -0
  39. package/examples/crt-homology/02-birkhoff-attention.js +100 -0
  40. package/examples/crt-homology/03-homology-loss.js +132 -0
  41. package/examples/crt-homology/04-crt-resoformer.js +132 -0
  42. package/examples/crt-homology/README.md +67 -0
  43. package/examples/crypto/01-password-hash.js +210 -0
  44. package/examples/crypto/02-key-derivation.js +210 -0
  45. package/examples/crypto/03-hmac.js +229 -0
  46. package/examples/crypto/04-file-integrity.js +263 -0
  47. package/examples/crypto/05-content-hash.js +263 -0
  48. package/examples/crypto/README.md +99 -0
  49. package/examples/demo-modular.js +223 -0
  50. package/examples/demo-two-layer.js +196 -0
  51. package/examples/discrete/01-integer-sine-table.js +120 -0
  52. package/examples/discrete/02-codebook-tunneling.js +118 -0
  53. package/examples/discrete/03-canonical-fusion.js +135 -0
  54. package/examples/discrete/04-tick-gate.js +139 -0
  55. package/examples/discrete/README.md +142 -0
  56. package/examples/formal-semantics/01-typed-terms.js +156 -0
  57. package/examples/formal-semantics/02-reduction.js +202 -0
  58. package/examples/formal-semantics/03-lambda-translation.js +206 -0
  59. package/examples/formal-semantics/04-enochian-language.js +257 -0
  60. package/examples/formal-semantics/README.md +98 -0
  61. package/examples/math/01-quaternions.js +237 -0
  62. package/examples/math/02-octonions.js +192 -0
  63. package/examples/math/03-prime-factorization.js +215 -0
  64. package/examples/math/04-vector-spaces.js +210 -0
  65. package/examples/math/05-gaussian-primes.js +234 -0
  66. package/examples/math/README.md +93 -0
  67. package/examples/physics/01-oscillator.js +177 -0
  68. package/examples/physics/02-lyapunov.js +201 -0
  69. package/examples/physics/03-collapse.js +183 -0
  70. package/examples/physics/04-kuramoto.js +212 -0
  71. package/examples/physics/05-entropy.js +226 -0
  72. package/examples/physics/05-sync-models.js +298 -0
  73. package/examples/physics/06-primeon-ladder.js +233 -0
  74. package/examples/physics/07-kuramoto-coupled-ladder.js +298 -0
  75. package/examples/physics/README.md +126 -0
  76. package/examples/resonance/01-prime-hilbert-space.js +140 -0
  77. package/examples/resonance/02-prime-resonance-network.js +221 -0
  78. package/examples/resonance/03-resoformer.js +349 -0
  79. package/examples/resonance/04-resoformer-training.js +329 -0
  80. package/examples/resonance/05-language-model.js +484 -0
  81. package/examples/resonance/README.md +238 -0
  82. package/examples/run-examples.js +417 -0
  83. package/examples/scientific/01-single-qubit.js +185 -0
  84. package/examples/scientific/02-two-qubit.js +209 -0
  85. package/examples/scientific/03-quantum-circuits.js +270 -0
  86. package/examples/scientific/04-measurement.js +229 -0
  87. package/examples/scientific/05-algorithms.js +245 -0
  88. package/examples/scientific/06-random.js +225 -0
  89. package/examples/scientific/07-wavefunction.js +192 -0
  90. package/examples/scientific/README.md +118 -0
  91. package/examples/semantic/01-vocabulary.js +186 -0
  92. package/examples/semantic/02-similarity.js +263 -0
  93. package/examples/semantic/03-word-algebra.js +295 -0
  94. package/examples/semantic/04-clustering.js +348 -0
  95. package/examples/semantic/05-classification.js +386 -0
  96. package/examples/semantic/06-dna-encoding.js +228 -0
  97. package/examples/semantic/07-search.js +304 -0
  98. package/examples/semantic/08-qa-system.js +278 -0
  99. package/examples/semantic/README.md +116 -0
  100. package/examples/topology/01-108-invariant.js +81 -0
  101. package/examples/topology/02-trefoil-constants.js +112 -0
  102. package/examples/topology/03-gauge-symmetry.js +112 -0
  103. package/examples/topology/04-free-energy-dynamics.js +124 -0
  104. package/examples/topology/README.md +129 -0
  105. package/index.js +32 -0
  106. package/modular.js +63 -2
  107. package/package.json +8 -3
  108. package/physics/alk-kuramoto.js +817 -0
  109. package/physics/index.js +23 -2
@@ -0,0 +1,136 @@
1
+ /**
2
+ * @example Quantum Coin Flip
3
+ * @description Create a superposition, measure, and see quantum randomness
4
+ *
5
+ * This example demonstrates TinyAleph's scientific backend for
6
+ * quantum computing simulation. It shows:
7
+ * - Creating quantum superposition states
8
+ * - Applying quantum gates
9
+ * - Measurement and collapse
10
+ * - True quantum randomness
11
+ */
12
+
13
+ const { ScientificBackend, Hypercomplex } = require('../modular');
14
+
15
+ // ===========================================
16
+ // SETUP
17
+ // ===========================================
18
+
19
+ // Create a scientific backend for quantum simulation
20
+ const backend = new ScientificBackend({ dimension: 16 });
21
+
22
+ // ===========================================
23
+ // QUANTUM COIN FLIP
24
+ // ===========================================
25
+
26
+ console.log('TinyAleph Quantum Coin Flip Example');
27
+ console.log('====================================\n');
28
+
29
+ // Start with |0⟩ state (classical "heads up" coin)
30
+ let zeroPrimes = backend.encode('|0⟩');
31
+ let zeroState = backend.primesToState(zeroPrimes);
32
+
33
+ console.log('Initial state: |0⟩ (coin heads up)');
34
+ console.log('Primes:', zeroPrimes);
35
+ console.log('First 4 components:', zeroState.c.slice(0, 4).map(c => c.toFixed(3)).join(', '));
36
+
37
+ // Apply Hadamard gate to create superposition
38
+ // H|0⟩ = (|0⟩ + |1⟩) / √2
39
+ const superpositionPrimes = backend.applyGate(zeroPrimes, 'H');
40
+ const superpositionState = backend.primesToState(superpositionPrimes);
41
+
42
+ console.log('\nAfter Hadamard gate: |+⟩ = (|0⟩ + |1⟩)/√2 (coin spinning!)');
43
+ console.log('Primes:', superpositionPrimes);
44
+ console.log('First 4 components:', superpositionState.c.slice(0, 4).map(c => c.toFixed(3)).join(', '));
45
+
46
+ // ===========================================
47
+ // MULTIPLE COIN FLIPS
48
+ // ===========================================
49
+
50
+ console.log('\n====================================');
51
+ console.log('FLIPPING THE QUANTUM COIN 10 TIMES');
52
+ console.log('====================================\n');
53
+
54
+ const results = [];
55
+
56
+ for (let i = 0; i < 10; i++) {
57
+ // Create fresh |0⟩ state
58
+ const coinPrimes = backend.encode('|0⟩');
59
+
60
+ // Apply Hadamard to create superposition
61
+ const spinningPrimes = backend.applyGate(coinPrimes, 'H');
62
+ const spinningState = backend.primesToState(spinningPrimes);
63
+
64
+ // Measure (collapse the superposition)
65
+ const measurement = backend.measure(spinningState);
66
+
67
+ // Interpret result
68
+ const outcome = measurement.outcome === 0 ? 'Heads' : 'Tails';
69
+ results.push(outcome);
70
+
71
+ console.log(`Flip ${i + 1}: ${outcome} (probability: ${(measurement.probability * 100).toFixed(0)}%)`);
72
+ }
73
+
74
+ // Count results
75
+ const heads = results.filter(r => r === 'Heads').length;
76
+ const tails = results.filter(r => r === 'Tails').length;
77
+
78
+ console.log('\n------------------------------------');
79
+ console.log(`Results: ${heads} Heads, ${tails} Tails`);
80
+ console.log(`Ratio: ${(heads / results.length * 100).toFixed(0)}% / ${(tails / results.length * 100).toFixed(0)}%`);
81
+
82
+ // ===========================================
83
+ // QUANTUM VS CLASSICAL
84
+ // ===========================================
85
+
86
+ console.log('\n====================================');
87
+ console.log('QUANTUM VS CLASSICAL RANDOMNESS');
88
+ console.log('====================================\n');
89
+
90
+ // Classical: no superposition = deterministic
91
+ console.log('Classical coin (no Hadamard, always |0⟩):');
92
+ for (let i = 0; i < 3; i++) {
93
+ const classicalPrimes = backend.encode('|0⟩');
94
+ const classicalState = backend.primesToState(classicalPrimes);
95
+ const result = backend.measure(classicalState);
96
+ console.log(` Flip ${i + 1}: ${result.outcome === 0 ? 'Heads' : 'Tails'} (prob: ${(result.probability * 100).toFixed(0)}%)`);
97
+ }
98
+
99
+ // Quantum: superposition gives randomness
100
+ console.log('\nQuantum coin (with Hadamard):');
101
+ for (let i = 0; i < 3; i++) {
102
+ const qCoinPrimes = backend.encode('|0⟩');
103
+ const qSpinningPrimes = backend.applyGate(qCoinPrimes, 'H');
104
+ const qSpinningState = backend.primesToState(qSpinningPrimes);
105
+ const qResult = backend.measure(qSpinningState);
106
+ console.log(` Flip ${i + 1}: ${qResult.outcome === 0 ? 'Heads' : 'Tails'} (prob: ${(qResult.probability * 100).toFixed(0)}%)`);
107
+ }
108
+
109
+ // ===========================================
110
+ // BELL STATE (ENTANGLEMENT)
111
+ // ===========================================
112
+
113
+ console.log('\n====================================');
114
+ console.log('BONUS: BELL STATE (Entanglement)');
115
+ console.log('====================================\n');
116
+
117
+ const bellPrimes = backend.createEntangledPair('Φ+');
118
+ const bellState = backend.primesToState(bellPrimes);
119
+ const bellName = backend.decode(bellPrimes);
120
+
121
+ console.log('Bell state |Φ+⟩ (maximally entangled):');
122
+ console.log(' Primes:', bellPrimes);
123
+ console.log(' Decoded:', bellName);
124
+ console.log(' Components:', bellState.c.slice(0, 4).map(c => c.toFixed(3)).join(', '));
125
+
126
+ // ===========================================
127
+ // KEY TAKEAWAYS
128
+ // ===========================================
129
+
130
+ console.log('\n====================================');
131
+ console.log('KEY TAKEAWAYS:');
132
+ console.log('1. |0⟩ and |1⟩ are basis states (like Heads/Tails)');
133
+ console.log('2. Hadamard gate creates superposition |+⟩');
134
+ console.log('3. Measurement collapses superposition probabilistically');
135
+ console.log('4. Each measurement is genuinely random');
136
+ console.log('5. Bell states demonstrate quantum entanglement');
@@ -0,0 +1,146 @@
1
+ /**
2
+ * Example: Golden Ratio Resonance
3
+ *
4
+ * Demonstrates the resonance calculator and its ability to detect
5
+ * naturally harmonic relationships between primes based on the golden ratio.
6
+ */
7
+
8
+ const {
9
+ ResonanceCalculator,
10
+ resonanceSignature,
11
+ findFibonacciSequences,
12
+ PHI,
13
+ calculateResonance,
14
+ findGoldenPairs
15
+ } = require('../core');
16
+
17
+ console.log('='.repeat(60));
18
+ console.log('Golden Ratio Resonance Calculator');
19
+ console.log('='.repeat(60));
20
+ console.log(`\nGolden Ratio (φ): ${PHI.toFixed(10)}`);
21
+
22
+ // ─────────────────────────────────────────────────────────────────
23
+ // Basic Resonance Calculation
24
+ // ─────────────────────────────────────────────────────────────────
25
+
26
+ console.log('\n1. Basic Resonance Between Primes');
27
+ console.log('-'.repeat(40));
28
+
29
+ const pairs = [
30
+ [2, 3],
31
+ [3, 5], // Fibonacci pair - ratio ≈ 1.667
32
+ [5, 8], // Fibonacci pair - ratio = 1.6
33
+ [7, 11],
34
+ [11, 13]
35
+ ];
36
+
37
+ for (const [p1, p2] of pairs) {
38
+ const r = calculateResonance(p1, p2);
39
+ const ratio = p2 / p1;
40
+ const isGolden = Math.abs(ratio - PHI) < 0.1;
41
+ console.log(
42
+ ` ${p1} × ${p2}: ratio=${ratio.toFixed(3)}, ` +
43
+ `resonance=${r.toFixed(4)}${isGolden ? ' ★ golden!' : ''}`
44
+ );
45
+ }
46
+
47
+ // ─────────────────────────────────────────────────────────────────
48
+ // Find Golden Pairs
49
+ // ─────────────────────────────────────────────────────────────────
50
+
51
+ console.log('\n2. Golden Ratio Pairs (First 20 Primes)');
52
+ console.log('-'.repeat(40));
53
+
54
+ const first20 = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71];
55
+ const goldenPairs = findGoldenPairs(first20);
56
+
57
+ if (goldenPairs.length === 0) {
58
+ console.log(' No exact golden ratio pairs found (φ threshold: 0.1)');
59
+ } else {
60
+ for (const pair of goldenPairs.slice(0, 5)) {
61
+ console.log(` ${pair.p1}/${pair.p2} = ${pair.ratio.toFixed(5)} (resonance: ${pair.resonance.toFixed(4)})`);
62
+ }
63
+ }
64
+
65
+ // Fibonacci numbers for comparison
66
+ console.log('\n Fibonacci numbers for reference:');
67
+ const fib = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89];
68
+ for (let i = 1; i < fib.length - 1; i++) {
69
+ const ratio = fib[i + 1] / fib[i];
70
+ console.log(` ${fib[i]}/${fib[i+1]} = ${ratio.toFixed(5)}`);
71
+ }
72
+
73
+ // ─────────────────────────────────────────────────────────────────
74
+ // Resonance Signature
75
+ // ─────────────────────────────────────────────────────────────────
76
+
77
+ console.log('\n3. Resonance Signatures');
78
+ console.log('-'.repeat(40));
79
+
80
+ const sets = [
81
+ { name: 'Small primes', primes: [2, 3, 5, 7] },
82
+ { name: 'Twin primes', primes: [3, 5, 11, 13, 17, 19] },
83
+ { name: 'Large primes', primes: [97, 101, 103, 107, 109] },
84
+ { name: 'Fibonacci primes', primes: [2, 3, 5, 13, 89, 233] }
85
+ ];
86
+
87
+ for (const set of sets) {
88
+ const sig = resonanceSignature(set.primes);
89
+ console.log(` ${set.name}: mean=${sig.mean.toFixed(4)}, variance=${sig.variance.toFixed(6)}, goldenPairs=${sig.goldenCount}`);
90
+ }
91
+
92
+ // ─────────────────────────────────────────────────────────────────
93
+ // Resonance Matrix
94
+ // ─────────────────────────────────────────────────────────────────
95
+
96
+ console.log('\n4. Resonance Matrix (First 5 Primes)');
97
+ console.log('-'.repeat(40));
98
+
99
+ const calc = new ResonanceCalculator();
100
+ const primes5 = [2, 3, 5, 7, 11];
101
+ const matrix = calc.calculateMatrix(primes5);
102
+
103
+ // Header
104
+ console.log(' ' + primes5.map(p => p.toString().padStart(6)).join(' '));
105
+
106
+ // Rows
107
+ for (let i = 0; i < primes5.length; i++) {
108
+ const row = matrix[i].map(v => v.toFixed(3).padStart(6)).join(' ');
109
+ console.log(` ${primes5[i].toString().padStart(3)} ${row}`);
110
+ }
111
+
112
+ // ─────────────────────────────────────────────────────────────────
113
+ // Find Most Resonant
114
+ // ─────────────────────────────────────────────────────────────────
115
+
116
+ console.log('\n5. Most Resonant Prime');
117
+ console.log('-'.repeat(40));
118
+
119
+ const target = 5;
120
+ const candidates = [2, 7, 11, 13, 17, 19, 23];
121
+ const most = calc.findMostResonant(target, candidates);
122
+
123
+ console.log(` Target: ${target}`);
124
+ console.log(` Candidates: [${candidates.join(', ')}]`);
125
+ console.log(` Most resonant: ${most.prime} (resonance: ${most.resonance.toFixed(4)})`);
126
+
127
+ // ─────────────────────────────────────────────────────────────────
128
+ // Resonance Clusters
129
+ // ─────────────────────────────────────────────────────────────────
130
+
131
+ console.log('\n6. Resonance Clusters');
132
+ console.log('-'.repeat(40));
133
+
134
+ const manyPrimes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29];
135
+ const clusters = calc.findClusters(manyPrimes, 0.4);
136
+
137
+ console.log(` Primes: [${manyPrimes.join(', ')}]`);
138
+ console.log(` Threshold: 0.4`);
139
+ console.log(` Clusters found: ${clusters.length}`);
140
+ for (let i = 0; i < clusters.length; i++) {
141
+ console.log(` Cluster ${i + 1}: [${clusters[i].join(', ')}]`);
142
+ }
143
+
144
+ console.log('\n' + '='.repeat(60));
145
+ console.log('Resonance theory: Primes with ratios near φ have natural harmony');
146
+ console.log('='.repeat(60));
@@ -0,0 +1,150 @@
1
+ /**
2
+ * Example: Symbol Database
3
+ *
4
+ * Demonstrates the symbolic AI database with 200+ emoji symbols,
5
+ * their prime assignments, and cultural tagging.
6
+ */
7
+
8
+ const {
9
+ SymbolDatabase,
10
+ SymbolCategory,
11
+ symbolDatabase,
12
+ getSymbol,
13
+ getSymbolByPrime
14
+ } = require('../core');
15
+
16
+ console.log('='.repeat(60));
17
+ console.log('Symbolic AI Database');
18
+ console.log('='.repeat(60));
19
+
20
+ // ─────────────────────────────────────────────────────────────────
21
+ // Database Overview
22
+ // ─────────────────────────────────────────────────────────────────
23
+
24
+ console.log('\n1. Database Statistics');
25
+ console.log('-'.repeat(40));
26
+
27
+ const stats = symbolDatabase.getStats();
28
+ console.log(` Total symbols: ${stats.totalSymbols}`);
29
+ console.log('\n By category:');
30
+ for (const [category, count] of Object.entries(stats.byCategory)) {
31
+ console.log(` ${category}: ${count} symbols`);
32
+ }
33
+ console.log('\n Top cultural tags:');
34
+ for (const { tag, count } of stats.topTags) {
35
+ console.log(` ${tag}: ${count} symbols`);
36
+ }
37
+
38
+ // ─────────────────────────────────────────────────────────────────
39
+ // Symbol Lookup
40
+ // ─────────────────────────────────────────────────────────────────
41
+
42
+ console.log('\n2. Symbol Lookup');
43
+ console.log('-'.repeat(40));
44
+
45
+ const examples = ['hero', 'warrior', 'mountain', 'fire', 'athena', 'samurai'];
46
+ for (const id of examples) {
47
+ const s = getSymbol(id);
48
+ if (s) {
49
+ console.log(` ${s.unicode} ${s.id} (prime: ${s.prime})`);
50
+ console.log(` "${s.meaning}"`);
51
+ console.log(` tags: [${s.culturalTags.join(', ')}]`);
52
+ }
53
+ }
54
+
55
+ // ─────────────────────────────────────────────────────────────────
56
+ // Reverse Lookup by Prime
57
+ // ─────────────────────────────────────────────────────────────────
58
+
59
+ console.log('\n3. Reverse Lookup (by Prime)');
60
+ console.log('-'.repeat(40));
61
+
62
+ const testPrimes = [2, 3, 5, 7, 11, 13];
63
+ for (const p of testPrimes) {
64
+ const s = getSymbolByPrime(p);
65
+ if (s) {
66
+ console.log(` Prime ${p} → ${s.unicode} ${s.id}`);
67
+ }
68
+ }
69
+
70
+ // ─────────────────────────────────────────────────────────────────
71
+ // Category Browsing
72
+ // ─────────────────────────────────────────────────────────────────
73
+
74
+ console.log('\n4. Browse by Category');
75
+ console.log('-'.repeat(40));
76
+
77
+ for (const category of Object.values(SymbolCategory)) {
78
+ const symbols = symbolDatabase.getSymbolsByCategory(category);
79
+ const sample = symbols.slice(0, 5).map(s => s.unicode).join(' ');
80
+ console.log(` ${category}: ${sample}... (${symbols.length} total)`);
81
+ }
82
+
83
+ // ─────────────────────────────────────────────────────────────────
84
+ // Cultural Filtering
85
+ // ─────────────────────────────────────────────────────────────────
86
+
87
+ console.log('\n5. Cultural Symbols');
88
+ console.log('-'.repeat(40));
89
+
90
+ const cultures = ['greek', 'norse', 'japanese', 'egyptian', 'hindu'];
91
+ for (const culture of cultures) {
92
+ const symbols = symbolDatabase.getSymbolsByTag(culture);
93
+ if (symbols.length > 0) {
94
+ const sample = symbols.slice(0, 5).map(s => s.unicode).join(' ');
95
+ console.log(` ${culture}: ${sample} (${symbols.length} symbols)`);
96
+ }
97
+ }
98
+
99
+ // ─────────────────────────────────────────────────────────────────
100
+ // Search
101
+ // ─────────────────────────────────────────────────────────────────
102
+
103
+ console.log('\n6. Symbol Search');
104
+ console.log('-'.repeat(40));
105
+
106
+ const queries = ['warrior', 'wisdom', 'fire'];
107
+ for (const query of queries) {
108
+ const results = symbolDatabase.search(query);
109
+ console.log(` "${query}": ${results.length} results`);
110
+ for (const r of results.slice(0, 3)) {
111
+ console.log(` ${r.unicode} ${r.id}: ${r.meaning.slice(0, 40)}...`);
112
+ }
113
+ }
114
+
115
+ // ─────────────────────────────────────────────────────────────────
116
+ // Prime Encoding
117
+ // ─────────────────────────────────────────────────────────────────
118
+
119
+ console.log('\n7. Prime Encoding');
120
+ console.log('-'.repeat(40));
121
+
122
+ const concept1 = ['hero', 'journey', 'mountain'];
123
+ const encoded1 = symbolDatabase.encode(concept1);
124
+ console.log(` Concept: [${concept1.join(', ')}]`);
125
+ console.log(` Encoded: ${encoded1.toString()}`);
126
+ console.log(` Decoded: ${symbolDatabase.decode(encoded1).map(s => s.id).join(', ')}`);
127
+
128
+ const concept2 = ['fire', 'water', 'earth_element'];
129
+ const encoded2 = symbolDatabase.encode(concept2);
130
+ console.log(`\n Concept: [${concept2.join(', ')}]`);
131
+ console.log(` Encoded: ${encoded2.toString()}`);
132
+ console.log(` Decoded: ${symbolDatabase.decode(encoded2).map(s => s.id).join(', ')}`);
133
+
134
+ // ─────────────────────────────────────────────────────────────────
135
+ // Universal vs Cultural Archetypes
136
+ // ─────────────────────────────────────────────────────────────────
137
+
138
+ console.log('\n8. Universal Archetypes');
139
+ console.log('-'.repeat(40));
140
+
141
+ const universal = symbolDatabase.getSymbolsByTag('universal');
142
+ console.log(` Total universal symbols: ${universal.length}`);
143
+ console.log(' Sample archetypes:');
144
+ for (const s of universal.slice(0, 10)) {
145
+ console.log(` ${s.unicode} ${s.id}: ${s.meaning}`);
146
+ }
147
+
148
+ console.log('\n' + '='.repeat(60));
149
+ console.log('Each symbol has a unique prime - concepts combine via multiplication');
150
+ console.log('='.repeat(60));
@@ -0,0 +1,223 @@
1
+ /**
2
+ * Example: Semantic Inference with Resonance Attention
3
+ *
4
+ * Demonstrates the semantic inference engine that maps natural language
5
+ * to symbols through patterns, direct matching, semantic similarity,
6
+ * AND resonance-based disambiguation using ResoFormer attention.
7
+ */
8
+
9
+ const {
10
+ SemanticInference,
11
+ EntityExtractor,
12
+ semanticInference,
13
+ inferSymbol,
14
+ inferSymbols,
15
+ extractEntities,
16
+ symbolDatabase,
17
+ inferWithResonance,
18
+ inferMostResonant,
19
+ getSymbol
20
+ } = require('../core');
21
+
22
+ console.log('='.repeat(60));
23
+ console.log('Semantic Inference Engine');
24
+ console.log('='.repeat(60));
25
+
26
+ // ─────────────────────────────────────────────────────────────────
27
+ // Direct Symbol Inference
28
+ // ─────────────────────────────────────────────────────────────────
29
+
30
+ console.log('\n1. Direct Symbol Inference');
31
+ console.log('-'.repeat(40));
32
+
33
+ const directQueries = ['warrior', 'hero', 'fire', 'mountain', 'wisdom'];
34
+ for (const query of directQueries) {
35
+ const result = inferSymbol(query);
36
+ if (result) {
37
+ console.log(` "${query}" → ${result.symbol.unicode} ${result.symbol.id} [${result.method}]`);
38
+ } else {
39
+ console.log(` "${query}" → (no match)`);
40
+ }
41
+ }
42
+
43
+ // ─────────────────────────────────────────────────────────────────
44
+ // Multi-Word Inference
45
+ // ─────────────────────────────────────────────────────────────────
46
+
47
+ console.log('\n2. Multi-Word Text Inference');
48
+ console.log('-'.repeat(40));
49
+
50
+ const texts = [
51
+ 'The hero climbs the mountain',
52
+ 'A brave warrior fights in battle',
53
+ 'The sage seeks wisdom in the temple',
54
+ 'Fire and water combine in transformation',
55
+ 'The king rules from his throne'
56
+ ];
57
+
58
+ for (const text of texts) {
59
+ console.log(`\n "${text}"`);
60
+ const symbols = inferSymbols(text);
61
+ if (symbols.length > 0) {
62
+ const icons = symbols.map(s => s.symbol.unicode).join(' ');
63
+ const ids = symbols.map(s => s.symbol.id).join(', ');
64
+ console.log(` → ${icons}`);
65
+ console.log(` → [${ids}]`);
66
+ } else {
67
+ console.log(' → (no symbols matched)');
68
+ }
69
+ }
70
+
71
+ // ─────────────────────────────────────────────────────────────────
72
+ // Pattern Matching
73
+ // ─────────────────────────────────────────────────────────────────
74
+
75
+ console.log('\n3. Pattern-Based Inference');
76
+ console.log('-'.repeat(40));
77
+
78
+ const patternTests = [
79
+ 'a mighty knight in shining armor',
80
+ 'the old wizard with a long beard',
81
+ 'flames dancing in the night',
82
+ 'deep underground caverns',
83
+ 'stormy seas and crashing waves'
84
+ ];
85
+
86
+ for (const text of patternTests) {
87
+ const result = inferSymbol(text);
88
+ if (result) {
89
+ console.log(` "${text.slice(0, 30)}..." → ${result.symbol.unicode} ${result.symbol.id} [${result.method}]`);
90
+ } else {
91
+ console.log(` "${text.slice(0, 30)}..." → (no match)`);
92
+ }
93
+ }
94
+
95
+ // ─────────────────────────────────────────────────────────────────
96
+ // Entity Extraction
97
+ // ─────────────────────────────────────────────────────────────────
98
+
99
+ console.log('\n4. Entity Extraction');
100
+ console.log('-'.repeat(40));
101
+
102
+ const entityTexts = [
103
+ 'John traveled to Paris on Monday',
104
+ 'Dr. Smith discovered the ancient temple in 1985',
105
+ 'The battle occurred at Mount Olympus during summer'
106
+ ];
107
+
108
+ for (const text of entityTexts) {
109
+ console.log(`\n "${text}"`);
110
+ const entities = extractEntities(text);
111
+ if (entities) {
112
+ for (const [type, values] of Object.entries(entities)) {
113
+ if (Array.isArray(values) && values.length > 0) {
114
+ console.log(` ${type}: [${values.join(', ')}]`);
115
+ } else if (typeof values === 'string') {
116
+ console.log(` ${type}: ${values}`);
117
+ }
118
+ }
119
+ }
120
+ }
121
+
122
+ // ─────────────────────────────────────────────────────────────────
123
+ // Symbolic Narrative
124
+ // ─────────────────────────────────────────────────────────────────
125
+
126
+ console.log('\n5. Symbolic Narrative');
127
+ console.log('-'.repeat(40));
128
+
129
+ const narrative = `
130
+ The young hero left his village and traveled through the dark forest.
131
+ He crossed the great river and climbed the sacred mountain.
132
+ There he found the ancient sage who revealed the hidden truth.
133
+ Armed with wisdom, he descended to face the shadow within.
134
+ Through fire and transformation, he emerged victorious.
135
+ `;
136
+
137
+ console.log(' Input narrative:');
138
+ console.log(' ' + narrative.trim().split('\n').map(l => l.trim()).join('\n '));
139
+
140
+ const storySymbols = inferSymbols(narrative);
141
+ console.log(`\n Extracted symbols (${storySymbols.length}):`);
142
+
143
+ // Group by method
144
+ const byMethod = {};
145
+ for (const s of storySymbols) {
146
+ if (!byMethod[s.method]) byMethod[s.method] = [];
147
+ byMethod[s.method].push(s.symbol);
148
+ }
149
+
150
+ for (const [method, symbols] of Object.entries(byMethod)) {
151
+ const icons = symbols.map(s => s.unicode).join(' ');
152
+ console.log(` ${method}: ${icons}`);
153
+ }
154
+
155
+ // Calculate resonance of the narrative
156
+ const { resonanceSignature } = require('../core');
157
+ const primes = storySymbols.map(s => s.symbol.prime);
158
+ if (primes.length >= 2) {
159
+ const sig = resonanceSignature(primes);
160
+ console.log(`\n Narrative resonance: mean=${sig.mean.toFixed(4)}, goldenPairs=${sig.goldenCount}`);
161
+ }
162
+
163
+ // ─────────────────────────────────────────────────────────────────
164
+ // Resonance-Enhanced Inference
165
+ // ─────────────────────────────────────────────────────────────────
166
+
167
+ console.log('\n6. Resonance-Enhanced Inference (ResoFormer Attention)');
168
+ console.log('-'.repeat(40));
169
+
170
+ const resonanceText = 'The brave hero fought the shadow in the temple of fire';
171
+ console.log(` Text: "${resonanceText}"`);
172
+
173
+ const resonanceResults = inferWithResonance(resonanceText);
174
+ console.log('\n Symbols ranked by resonance harmony:');
175
+ for (const r of resonanceResults.slice(0, 5)) {
176
+ const bonus = r.resonanceBonus ? `, resonance: ${r.resonanceBonus.toFixed(3)}` : '';
177
+ console.log(` ${r.symbol.unicode} ${r.symbol.id} (confidence: ${r.confidence.toFixed(2)}${bonus})`);
178
+ }
179
+
180
+ // ─────────────────────────────────────────────────────────────────
181
+ // Context-Aware Symbol Selection
182
+ // ─────────────────────────────────────────────────────────────────
183
+
184
+ console.log('\n7. Context-Aware Symbol Selection');
185
+ console.log('-'.repeat(40));
186
+
187
+ const contextSymbols = [getSymbol('warrior'), getSymbol('temple')];
188
+ console.log(' Context:', contextSymbols.map(s => `${s.unicode} ${s.id}`).join(', '));
189
+
190
+ const testWords = ['sword', 'fire', 'shadow', 'wisdom'];
191
+ console.log('\n Finding most resonant symbols for context:');
192
+
193
+ for (const word of testWords) {
194
+ const best = inferMostResonant(word, contextSymbols);
195
+ if (best) {
196
+ const contextR = best.contextResonance ? best.contextResonance.toFixed(4) : 'N/A';
197
+ console.log(` "${word}" → ${best.symbol.unicode} ${best.symbol.id} (context resonance: ${contextR})`);
198
+ }
199
+ }
200
+
201
+ // ─────────────────────────────────────────────────────────────────
202
+ // Resonance Attention for Disambiguation
203
+ // ─────────────────────────────────────────────────────────────────
204
+
205
+ console.log('\n8. Resonance Attention for Disambiguation');
206
+ console.log('-'.repeat(40));
207
+
208
+ console.log(' When multiple symbols could match, resonance picks');
209
+ console.log(' the one that "harmonizes" best with context.\n');
210
+
211
+ const ambiguousText = 'The warrior king held the sacred ring of power';
212
+ console.log(` Text: "${ambiguousText}"`);
213
+
214
+ const disambiguated = inferWithResonance(ambiguousText);
215
+ console.log('\n Disambiguated symbols (using resonance attention):');
216
+ for (const r of disambiguated) {
217
+ const weight = r.attentionWeight ? `, attention: ${r.attentionWeight.toFixed(3)}` : '';
218
+ console.log(` ${r.symbol.unicode} ${r.symbol.id}${weight}`);
219
+ }
220
+
221
+ console.log('\n' + '='.repeat(60));
222
+ console.log('Resonance attention: Symbols that harmonize together resonate');
223
+ console.log('='.repeat(60));