@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,304 @@
1
+ /**
2
+ * @example Semantic Search
3
+ * @description Build a semantic search engine using TinyAleph
4
+ *
5
+ * TinyAleph enables semantic search that finds results by meaning,
6
+ * not just keyword matching:
7
+ * - Index documents as hypercomplex embeddings
8
+ * - Query using natural language
9
+ * - Rank results by semantic similarity
10
+ */
11
+
12
+ const { SemanticBackend, Hypercomplex } = require('../../modular');
13
+
14
+ // ===========================================
15
+ // SETUP
16
+ // ===========================================
17
+
18
+ const backend = new SemanticBackend({ dimension: 16 });
19
+
20
+ console.log('TinyAleph Semantic Search Example');
21
+ console.log('==================================\n');
22
+
23
+ // ===========================================
24
+ // SEARCH ENGINE CLASS
25
+ // ===========================================
26
+
27
+ class SemanticSearchEngine {
28
+ constructor(backend) {
29
+ this.backend = backend;
30
+ this.documents = [];
31
+ this.index = [];
32
+ }
33
+
34
+ // Add a document to the index
35
+ addDocument(id, title, content, metadata) {
36
+ var fullText = title + ' ' + content;
37
+ var embedding = this.backend.textToOrderedState(fullText);
38
+
39
+ this.documents.push({
40
+ id: id,
41
+ title: title,
42
+ content: content,
43
+ metadata: metadata || {}
44
+ });
45
+
46
+ this.index.push({
47
+ docId: id,
48
+ embedding: embedding
49
+ });
50
+ }
51
+
52
+ // Compute similarity between two states
53
+ similarity(state1, state2) {
54
+ var dot = 0, mag1 = 0, mag2 = 0;
55
+ for (var i = 0; i < state1.c.length; i++) {
56
+ dot += state1.c[i] * state2.c[i];
57
+ mag1 += state1.c[i] * state1.c[i];
58
+ mag2 += state2.c[i] * state2.c[i];
59
+ }
60
+ return dot / (Math.sqrt(mag1) * Math.sqrt(mag2) || 1);
61
+ }
62
+
63
+ // Search for documents matching query
64
+ search(query, topK) {
65
+ topK = topK || 5;
66
+ var queryEmbedding = this.backend.textToOrderedState(query);
67
+
68
+ var self = this;
69
+ var results = this.index.map(function(item) {
70
+ return {
71
+ docId: item.docId,
72
+ score: self.similarity(queryEmbedding, item.embedding)
73
+ };
74
+ });
75
+
76
+ results.sort(function(a, b) { return b.score - a.score; });
77
+
78
+ var topResults = results.slice(0, topK);
79
+
80
+ return topResults.map(function(result) {
81
+ var doc = self.documents.find(function(d) { return d.id === result.docId; });
82
+ return {
83
+ id: result.docId,
84
+ title: doc.title,
85
+ content: doc.content.substring(0, 100) + '...',
86
+ score: result.score,
87
+ metadata: doc.metadata
88
+ };
89
+ });
90
+ }
91
+
92
+ // Search with filters
93
+ searchWithFilters(query, filters, topK) {
94
+ topK = topK || 5;
95
+ var queryEmbedding = this.backend.textToOrderedState(query);
96
+
97
+ var self = this;
98
+
99
+ // Filter documents first
100
+ var filteredIndices = this.index.filter(function(item) {
101
+ var doc = self.documents.find(function(d) { return d.id === item.docId; });
102
+
103
+ for (var key in filters) {
104
+ if (filters.hasOwnProperty(key)) {
105
+ if (doc.metadata[key] !== filters[key]) {
106
+ return false;
107
+ }
108
+ }
109
+ }
110
+ return true;
111
+ });
112
+
113
+ // Score filtered documents
114
+ var results = filteredIndices.map(function(item) {
115
+ return {
116
+ docId: item.docId,
117
+ score: self.similarity(queryEmbedding, item.embedding)
118
+ };
119
+ });
120
+
121
+ results.sort(function(a, b) { return b.score - a.score; });
122
+
123
+ return results.slice(0, topK).map(function(result) {
124
+ var doc = self.documents.find(function(d) { return d.id === result.docId; });
125
+ return {
126
+ id: result.docId,
127
+ title: doc.title,
128
+ score: result.score,
129
+ metadata: doc.metadata
130
+ };
131
+ });
132
+ }
133
+ }
134
+
135
+ // ===========================================
136
+ // BUILD SAMPLE INDEX
137
+ // ===========================================
138
+
139
+ console.log('Building Document Index:');
140
+ console.log('-'.repeat(50) + '\n');
141
+
142
+ var engine = new SemanticSearchEngine(backend);
143
+
144
+ // Add sample documents
145
+ var sampleDocs = [
146
+ {
147
+ id: 'doc1',
148
+ title: 'Introduction to Machine Learning',
149
+ content: 'Machine learning is a subset of artificial intelligence that enables computers to learn from data without explicit programming. It includes supervised learning, unsupervised learning, and reinforcement learning.',
150
+ metadata: { category: 'tech', year: 2023 }
151
+ },
152
+ {
153
+ id: 'doc2',
154
+ title: 'Deep Learning Neural Networks',
155
+ content: 'Deep learning uses neural networks with multiple layers to process complex patterns. Convolutional neural networks are used for image recognition, while recurrent networks handle sequential data.',
156
+ metadata: { category: 'tech', year: 2023 }
157
+ },
158
+ {
159
+ id: 'doc3',
160
+ title: 'Natural Language Processing',
161
+ content: 'NLP enables computers to understand human language. Techniques include tokenization, parsing, sentiment analysis, and machine translation. Modern NLP uses transformer architectures.',
162
+ metadata: { category: 'tech', year: 2022 }
163
+ },
164
+ {
165
+ id: 'doc4',
166
+ title: 'Healthy Mediterranean Diet',
167
+ content: 'The Mediterranean diet emphasizes fruits, vegetables, whole grains, and olive oil. It has been linked to reduced heart disease, improved cognitive function, and longevity.',
168
+ metadata: { category: 'health', year: 2023 }
169
+ },
170
+ {
171
+ id: 'doc5',
172
+ title: 'Exercise and Mental Health',
173
+ content: 'Regular physical exercise has been shown to reduce symptoms of depression and anxiety. It releases endorphins and promotes neuroplasticity in the brain.',
174
+ metadata: { category: 'health', year: 2022 }
175
+ },
176
+ {
177
+ id: 'doc6',
178
+ title: 'Quantum Computing Fundamentals',
179
+ content: 'Quantum computers use qubits that can exist in superposition. This allows them to solve certain problems exponentially faster than classical computers.',
180
+ metadata: { category: 'tech', year: 2023 }
181
+ },
182
+ {
183
+ id: 'doc7',
184
+ title: 'Climate Change Solutions',
185
+ content: 'Addressing climate change requires reducing carbon emissions, transitioning to renewable energy, and implementing carbon capture technologies. International cooperation is essential.',
186
+ metadata: { category: 'environment', year: 2023 }
187
+ },
188
+ {
189
+ id: 'doc8',
190
+ title: 'Renewable Energy Sources',
191
+ content: 'Solar, wind, and hydroelectric power are major renewable energy sources. They produce no direct carbon emissions and are becoming increasingly cost-competitive with fossil fuels.',
192
+ metadata: { category: 'environment', year: 2022 }
193
+ }
194
+ ];
195
+
196
+ for (var i = 0; i < sampleDocs.length; i++) {
197
+ var doc = sampleDocs[i];
198
+ engine.addDocument(doc.id, doc.title, doc.content, doc.metadata);
199
+ console.log(' Indexed: ' + doc.title);
200
+ }
201
+
202
+ console.log('\n Total documents: ' + engine.documents.length);
203
+
204
+ // ===========================================
205
+ // BASIC SEARCH
206
+ // ===========================================
207
+
208
+ console.log('\n' + '='.repeat(50));
209
+ console.log('Basic Semantic Search:');
210
+ console.log('='.repeat(50) + '\n');
211
+
212
+ var queries = [
213
+ 'how do computers learn from data',
214
+ 'staying healthy with diet and exercise',
215
+ 'clean energy and sustainability',
216
+ 'understanding AI and neural networks'
217
+ ];
218
+
219
+ for (var q = 0; q < queries.length; q++) {
220
+ var query = queries[q];
221
+ console.log('Query: "' + query + '"');
222
+ var results = engine.search(query, 3);
223
+
224
+ for (var r = 0; r < results.length; r++) {
225
+ var result = results[r];
226
+ console.log(' ' + (r + 1) + '. ' + result.title + ' (' + (result.score * 100).toFixed(1) + '%)');
227
+ }
228
+ console.log('');
229
+ }
230
+
231
+ // ===========================================
232
+ // FILTERED SEARCH
233
+ // ===========================================
234
+
235
+ console.log('='.repeat(50));
236
+ console.log('Filtered Search (by category):');
237
+ console.log('='.repeat(50) + '\n');
238
+
239
+ var filteredQueries = [
240
+ { query: 'learning algorithms', filter: { category: 'tech' } },
241
+ { query: 'wellness and longevity', filter: { category: 'health' } },
242
+ { query: 'reducing emissions', filter: { category: 'environment' } }
243
+ ];
244
+
245
+ for (var fq = 0; fq < filteredQueries.length; fq++) {
246
+ var item = filteredQueries[fq];
247
+ console.log('Query: "' + item.query + '" [category=' + item.filter.category + ']');
248
+ var results = engine.searchWithFilters(item.query, item.filter, 3);
249
+
250
+ for (var r = 0; r < results.length; r++) {
251
+ var result = results[r];
252
+ console.log(' ' + (r + 1) + '. ' + result.title + ' (' + (result.score * 100).toFixed(1) + '%)');
253
+ }
254
+ console.log('');
255
+ }
256
+
257
+ // ===========================================
258
+ // SEMANTIC VS KEYWORD
259
+ // ===========================================
260
+
261
+ console.log('='.repeat(50));
262
+ console.log('Semantic vs Keyword Search:');
263
+ console.log('='.repeat(50) + '\n');
264
+
265
+ // Queries that would fail keyword search but succeed semantically
266
+ var semanticQueries = [
267
+ {
268
+ query: 'teaching computers without programming them',
269
+ expected: 'Machine Learning'
270
+ },
271
+ {
272
+ query: 'brain patterns and mood improvement',
273
+ expected: 'Exercise and Mental Health'
274
+ },
275
+ {
276
+ query: 'alternative power generation',
277
+ expected: 'Renewable Energy'
278
+ }
279
+ ];
280
+
281
+ console.log('Queries that work semantically but might fail keyword matching:\n');
282
+
283
+ for (var sq = 0; sq < semanticQueries.length; sq++) {
284
+ var item = semanticQueries[sq];
285
+ var results = engine.search(item.query, 1);
286
+ var topResult = results[0];
287
+
288
+ console.log('Query: "' + item.query + '"');
289
+ console.log(' Expected: ' + item.expected);
290
+ console.log(' Found: ' + topResult.title + ' (' + (topResult.score * 100).toFixed(1) + '%)');
291
+ console.log('');
292
+ }
293
+
294
+ // ===========================================
295
+ // KEY TAKEAWAYS
296
+ // ===========================================
297
+
298
+ console.log('='.repeat(50));
299
+ console.log('KEY TAKEAWAYS:');
300
+ console.log('1. Semantic search finds results by meaning, not keywords');
301
+ console.log('2. Documents are indexed as hypercomplex embeddings');
302
+ console.log('3. Queries are embedded and compared to document embeddings');
303
+ console.log('4. Filters can narrow results before ranking');
304
+ console.log('5. Works even when query words differ from document words');
@@ -0,0 +1,278 @@
1
+ /**
2
+ * @example Question-Answering System
3
+ * @description Build a QA system using semantic embeddings
4
+ *
5
+ * TinyAleph can power question-answering systems:
6
+ * - Embed knowledge base passages
7
+ * - Match questions to relevant passages
8
+ * - Extract and rank answers
9
+ */
10
+
11
+ const { SemanticBackend, Hypercomplex } = require('../../modular');
12
+
13
+ // ===========================================
14
+ // SETUP
15
+ // ===========================================
16
+
17
+ const backend = new SemanticBackend({ dimension: 16 });
18
+
19
+ console.log('TinyAleph Question-Answering System Example');
20
+ console.log('============================================\n');
21
+
22
+ // ===========================================
23
+ // KNOWLEDGE BASE
24
+ // ===========================================
25
+
26
+ var knowledgeBase = [
27
+ {
28
+ id: 'kb1',
29
+ topic: 'Solar System',
30
+ passage: 'The Solar System consists of the Sun and the objects that orbit it, including eight planets, dwarf planets, moons, asteroids, and comets. The four inner planets are Mercury, Venus, Earth, and Mars. The four outer planets are Jupiter, Saturn, Uranus, and Neptune.'
31
+ },
32
+ {
33
+ id: 'kb2',
34
+ topic: 'Earth',
35
+ passage: 'Earth is the third planet from the Sun and the only astronomical object known to harbor life. It has one natural satellite called the Moon. Earth is about 4.5 billion years old and has a diameter of approximately 12,742 kilometers.'
36
+ },
37
+ {
38
+ id: 'kb3',
39
+ topic: 'Moon',
40
+ passage: 'The Moon is Earth\'s only natural satellite. It is the fifth-largest satellite in the Solar System. The Moon orbits Earth at an average distance of 384,400 kilometers. It takes about 27.3 days to complete one orbit around Earth.'
41
+ },
42
+ {
43
+ id: 'kb4',
44
+ topic: 'Photosynthesis',
45
+ passage: 'Photosynthesis is the process by which plants and other organisms convert sunlight, water, and carbon dioxide into glucose and oxygen. Chlorophyll, the green pigment in plants, absorbs light energy for this process.'
46
+ },
47
+ {
48
+ id: 'kb5',
49
+ topic: 'Water Cycle',
50
+ passage: 'The water cycle describes how water evaporates from oceans and land, rises into the atmosphere, condenses into clouds, and falls back as precipitation. This cycle continuously moves water around the Earth.'
51
+ },
52
+ {
53
+ id: 'kb6',
54
+ topic: 'DNA',
55
+ passage: 'DNA (deoxyribonucleic acid) is the molecule that carries genetic information in all living organisms. It consists of two strands that form a double helix structure. DNA is made up of four nucleotide bases: adenine, thymine, guanine, and cytosine.'
56
+ },
57
+ {
58
+ id: 'kb7',
59
+ topic: 'Gravity',
60
+ passage: 'Gravity is a fundamental force that attracts any two objects with mass. On Earth, gravity gives objects weight and causes them to fall toward the ground. The gravitational force between objects depends on their masses and the distance between them.'
61
+ },
62
+ {
63
+ id: 'kb8',
64
+ topic: 'Evolution',
65
+ passage: 'Evolution is the process by which species change over time through natural selection. Organisms with traits better suited to their environment are more likely to survive and reproduce, passing those traits to offspring.'
66
+ }
67
+ ];
68
+
69
+ // ===========================================
70
+ // QA SYSTEM CLASS
71
+ // ===========================================
72
+
73
+ function similarity(state1, state2) {
74
+ var dot = 0, mag1 = 0, mag2 = 0;
75
+ for (var i = 0; i < state1.c.length; i++) {
76
+ dot += state1.c[i] * state2.c[i];
77
+ mag1 += state1.c[i] * state1.c[i];
78
+ mag2 += state2.c[i] * state2.c[i];
79
+ }
80
+ return dot / (Math.sqrt(mag1) * Math.sqrt(mag2) || 1);
81
+ }
82
+
83
+ function QASystem(backend) {
84
+ this.backend = backend;
85
+ this.passages = [];
86
+ this.embeddings = [];
87
+ }
88
+
89
+ QASystem.prototype.loadKnowledge = function(passages) {
90
+ var self = this;
91
+ this.passages = passages;
92
+ this.embeddings = passages.map(function(p) {
93
+ return {
94
+ id: p.id,
95
+ embedding: self.backend.textToOrderedState(p.passage)
96
+ };
97
+ });
98
+ console.log('Loaded ' + passages.length + ' knowledge passages');
99
+ };
100
+
101
+ QASystem.prototype.findRelevantPassages = function(question, topK) {
102
+ topK = topK || 3;
103
+ var questionEmb = this.backend.textToOrderedState(question);
104
+
105
+ var self = this;
106
+ var scored = this.embeddings.map(function(e) {
107
+ return {
108
+ id: e.id,
109
+ score: similarity(questionEmb, e.embedding)
110
+ };
111
+ });
112
+
113
+ scored.sort(function(a, b) { return b.score - a.score; });
114
+
115
+ return scored.slice(0, topK).map(function(item) {
116
+ var passage = self.passages.find(function(p) { return p.id === item.id; });
117
+ return {
118
+ passage: passage,
119
+ score: item.score
120
+ };
121
+ });
122
+ };
123
+
124
+ QASystem.prototype.answer = function(question) {
125
+ var relevantPassages = this.findRelevantPassages(question, 2);
126
+
127
+ if (relevantPassages.length === 0 || relevantPassages[0].score < 0.2) {
128
+ return {
129
+ answer: 'I don\'t have enough information to answer that question.',
130
+ confidence: 0,
131
+ sources: []
132
+ };
133
+ }
134
+
135
+ // In a real system, you'd use NLP to extract the answer
136
+ // Here we return the most relevant passage as the answer
137
+ var topPassage = relevantPassages[0];
138
+
139
+ return {
140
+ answer: topPassage.passage.passage,
141
+ confidence: topPassage.score,
142
+ topic: topPassage.passage.topic,
143
+ sources: relevantPassages.map(function(r) {
144
+ return { topic: r.passage.topic, score: r.score };
145
+ })
146
+ };
147
+ };
148
+
149
+ // ===========================================
150
+ // BUILD QA SYSTEM
151
+ // ===========================================
152
+
153
+ console.log('Building QA System:');
154
+ console.log('-'.repeat(50) + '\n');
155
+
156
+ var qaSystem = new QASystem(backend);
157
+ qaSystem.loadKnowledge(knowledgeBase);
158
+
159
+ // ===========================================
160
+ // TEST QUESTIONS
161
+ // ===========================================
162
+
163
+ console.log('\n' + '='.repeat(50));
164
+ console.log('Testing Questions:');
165
+ console.log('='.repeat(50) + '\n');
166
+
167
+ var questions = [
168
+ 'How many planets are in our solar system?',
169
+ 'What is the Moon and how far is it from Earth?',
170
+ 'How do plants make their food?',
171
+ 'What is DNA made of?',
172
+ 'Why do things fall down?',
173
+ 'How do species change over time?',
174
+ 'What is the water cycle?'
175
+ ];
176
+
177
+ for (var q = 0; q < questions.length; q++) {
178
+ var question = questions[q];
179
+ console.log('Q: ' + question);
180
+
181
+ var result = qaSystem.answer(question);
182
+
183
+ console.log('Topic: ' + result.topic);
184
+ console.log('Confidence: ' + (result.confidence * 100).toFixed(1) + '%');
185
+ console.log('Answer: ' + result.answer.substring(0, 120) + '...');
186
+ console.log('');
187
+ }
188
+
189
+ // ===========================================
190
+ // MULTI-HOP QUESTIONS
191
+ // ===========================================
192
+
193
+ console.log('='.repeat(50));
194
+ console.log('Multi-Hop Question Answering:');
195
+ console.log('='.repeat(50) + '\n');
196
+
197
+ console.log('For complex questions, combine multiple passages:\n');
198
+
199
+ var complexQuestion = 'What orbits Earth and how does gravity affect it?';
200
+ console.log('Q: ' + complexQuestion + '\n');
201
+
202
+ var passages = qaSystem.findRelevantPassages(complexQuestion, 3);
203
+ console.log('Relevant passages found:');
204
+ for (var p = 0; p < passages.length; p++) {
205
+ var passage = passages[p];
206
+ console.log(' ' + (p + 1) + '. ' + passage.passage.topic + ' (' + (passage.score * 100).toFixed(1) + '%)');
207
+ }
208
+
209
+ console.log('\nCombined answer would draw from:');
210
+ console.log(' - Moon passage (for what orbits Earth)');
211
+ console.log(' - Gravity passage (for gravitational effects)');
212
+
213
+ // ===========================================
214
+ // QUESTION TYPES
215
+ // ===========================================
216
+
217
+ console.log('\n' + '='.repeat(50));
218
+ console.log('Handling Different Question Types:');
219
+ console.log('='.repeat(50) + '\n');
220
+
221
+ var questionTypes = [
222
+ { type: 'What', question: 'What is photosynthesis?' },
223
+ { type: 'How', question: 'How does water cycle work?' },
224
+ { type: 'Why', question: 'Why do organisms evolve?' },
225
+ { type: 'When', question: 'When did Earth form?' },
226
+ { type: 'How many', question: 'How many bases are in DNA?' }
227
+ ];
228
+
229
+ for (var qt = 0; qt < questionTypes.length; qt++) {
230
+ var item = questionTypes[qt];
231
+ var result = qaSystem.answer(item.question);
232
+
233
+ console.log('[' + item.type + '] ' + item.question);
234
+ console.log(' Found: ' + result.topic + ' (' + (result.confidence * 100).toFixed(1) + '%)');
235
+ console.log('');
236
+ }
237
+
238
+ // ===========================================
239
+ // CONFIDENCE THRESHOLDING
240
+ // ===========================================
241
+
242
+ console.log('='.repeat(50));
243
+ console.log('Confidence Thresholding:');
244
+ console.log('='.repeat(50) + '\n');
245
+
246
+ var outOfDomainQuestions = [
247
+ 'What is the capital of France?',
248
+ 'How do I bake a cake?',
249
+ 'What is the stock price today?'
250
+ ];
251
+
252
+ console.log('Questions outside the knowledge base:\n');
253
+
254
+ for (var odq = 0; odq < outOfDomainQuestions.length; odq++) {
255
+ var question = outOfDomainQuestions[odq];
256
+ var result = qaSystem.answer(question);
257
+
258
+ console.log('Q: ' + question);
259
+ console.log(' Best match: ' + (result.topic || 'None') + ' (' + (result.confidence * 100).toFixed(1) + '%)');
260
+
261
+ if (result.confidence < 0.3) {
262
+ console.log(' Status: LOW CONFIDENCE - may not be answerable');
263
+ }
264
+ console.log('');
265
+ }
266
+
267
+ // ===========================================
268
+ // KEY TAKEAWAYS
269
+ // ===========================================
270
+
271
+ console.log('='.repeat(50));
272
+ console.log('KEY TAKEAWAYS:');
273
+ console.log('1. Embed knowledge passages for semantic retrieval');
274
+ console.log('2. Questions are matched to passages by similarity');
275
+ console.log('3. Confidence scores indicate answer reliability');
276
+ console.log('4. Multi-hop questions need multiple passages');
277
+ console.log('5. Low confidence indicates out-of-domain queries');
278
+ console.log('6. Combine with NLP for answer extraction');
@@ -0,0 +1,116 @@
1
+ # Semantic Examples
2
+
3
+ This directory contains examples demonstrating TinyAleph's semantic processing capabilities.
4
+
5
+ ## Examples
6
+
7
+ ### 01-vocabulary.js
8
+ **Word-to-Prime Mapping** - Explore how words map to prime numbers in TinyAleph's vocabulary space.
9
+
10
+ ```bash
11
+ node examples/semantic/01-vocabulary.js
12
+ ```
13
+
14
+ ### 02-similarity.js
15
+ **Semantic Similarity** - Compute similarity between texts using hypercomplex embeddings with various metrics.
16
+
17
+ ```bash
18
+ node examples/semantic/02-similarity.js
19
+ ```
20
+
21
+ ### 03-word-algebra.js
22
+ **Word Algebra** - Perform algebraic operations on word embeddings (king - man + woman = queen).
23
+
24
+ ```bash
25
+ node examples/semantic/03-word-algebra.js
26
+ ```
27
+
28
+ ### 04-clustering.js
29
+ **Text Clustering** - Group similar texts using K-means and hierarchical clustering algorithms.
30
+
31
+ ```bash
32
+ node examples/semantic/04-clustering.js
33
+ ```
34
+
35
+ ### 05-classification.js
36
+ **Text Classification** - Classify texts into categories using prototype-based and KNN classifiers.
37
+
38
+ ```bash
39
+ node examples/semantic/05-classification.js
40
+ ```
41
+
42
+ ### 06-dna-encoding.js
43
+ **DNA-Inspired Encoding** - Explore biological-inspired text processing with bidirectional encoding, codons, and reading frames.
44
+
45
+ ```bash
46
+ node examples/semantic/06-dna-encoding.js
47
+ ```
48
+
49
+ ### 07-search.js
50
+ **Semantic Search** - Build a semantic search engine that finds results by meaning, not just keywords.
51
+
52
+ ```bash
53
+ node examples/semantic/07-search.js
54
+ ```
55
+
56
+ ### 08-qa-system.js
57
+ **Question-Answering System** - Build a QA system using semantic embeddings to match questions to knowledge.
58
+
59
+ ```bash
60
+ node examples/semantic/08-qa-system.js
61
+ ```
62
+
63
+ ## Key Concepts
64
+
65
+ ### Prime Encoding
66
+ Every word token maps to a unique prime number, creating a mathematical vocabulary space where:
67
+ - Each word has a unique prime identity
68
+ - Word order affects encoding through prime powers
69
+ - Semantic relationships emerge from mathematical structure
70
+
71
+ ### Hypercomplex Embeddings
72
+ Text is embedded into hypercomplex (high-dimensional) vectors where:
73
+ - Similar texts have similar embeddings
74
+ - Vector operations (add, subtract, multiply) are meaningful
75
+ - Cosine similarity measures semantic relatedness
76
+
77
+ ### DNA-Inspired Processing
78
+ Biological concepts enhance text processing:
79
+ - **Bidirectional**: Alternating reading direction
80
+ - **Codons**: Triplet grouping of tokens
81
+ - **Reading Frames**: 6 different starting positions
82
+ - **Sense/Antisense**: Forward and reversed representations
83
+
84
+ ## Usage Pattern
85
+
86
+ ```javascript
87
+ const { SemanticBackend } = require('../../modular');
88
+
89
+ // Create backend
90
+ const backend = new SemanticBackend({ dimension: 16 });
91
+
92
+ // Get text embedding
93
+ const state = backend.textToOrderedState('your text here');
94
+
95
+ // Access components
96
+ console.log(state.c[0]); // First component
97
+ console.log(state.norm()); // Magnitude
98
+
99
+ // Compute similarity
100
+ function similarity(s1, s2) {
101
+ let dot = 0, m1 = 0, m2 = 0;
102
+ for (let i = 0; i < s1.c.length; i++) {
103
+ dot += s1.c[i] * s2.c[i];
104
+ m1 += s1.c[i] * s1.c[i];
105
+ m2 += s2.c[i] * s2.c[i];
106
+ }
107
+ return dot / (Math.sqrt(m1) * Math.sqrt(m2) || 1);
108
+ }
109
+ ```
110
+
111
+ ## Next Steps
112
+
113
+ After understanding semantic processing, explore:
114
+ - `../crypto/` - Cryptographic hashing and key derivation
115
+ - `../scientific/` - Quantum computing simulation
116
+ - `../ai/` - AI and machine learning applications