-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshowcase.py
More file actions
397 lines (308 loc) · 12.4 KB
/
showcase.py
File metadata and controls
397 lines (308 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
"""
SanTOK Cognitive - Complete Showcase
====================================
This demonstrates ALL SanTOK Cognitive capabilities in one place:
1. Knowledge Graph - Store and query relationships
2. Knowledge Trees - Hierarchical organization
3. Unified Memory - Link everything together
4. Symbolic Reasoning - Infer new facts
5. Custom Algorithms - 100% SanTOK-original
Run:
python -m santok_cognitive.showcase
"""
from .graph import GraphStore, GraphNode, RelationType
from .trees import TreeStore, Tree
from .memory import UnifiedMemory
from .reasoning import (
SanTOKReasoner,
InferenceEngine,
ContradictionDetector,
PathFinder,
)
from .algorithms import (
SanTOKRanker,
SanTOKPatternMatcher,
SanTOK9Scorer,
SanTOKGraphWalker,
SanTOKSimilarity,
SanTOKQueryParser,
WalkMode,
)
def print_header(title: str):
"""Print a section header."""
print("\n" + "═" * 60)
print(f" {title}")
print("═" * 60)
def demo_knowledge_graph():
"""Demonstrate Knowledge Graph capabilities."""
print_header("1. KNOWLEDGE GRAPH")
graph = GraphStore()
# Create nodes
nodes = [
(1, "SanTOK", "system"),
(2, "Tokenization", "module"),
(3, "Embeddings", "module"),
(4, "AI System", "class"),
(5, "NLP Tool", "class"),
]
for nid, text, ntype in nodes:
graph.add_node(GraphNode(nid, text, node_type=ntype))
# Create edges
edges = [
(1, 2, RelationType.HAS_PART),
(1, 3, RelationType.HAS_PART),
(1, 4, RelationType.IS_A),
(4, 5, RelationType.IS_A),
(2, 3, RelationType.PRECEDES),
]
for src, tgt, rel in edges:
graph.add_edge(src, tgt, rel)
print(f"\nGraph: {graph.node_count} nodes, {graph.edge_count} edges")
print("\nRelationships:")
for edge in graph.get_all_edges():
src = graph.get_node(edge.source_id)
tgt = graph.get_node(edge.target_id)
print(f" {src.text} --[{edge.relation_type.value}]--> {tgt.text}")
return graph
def demo_knowledge_trees():
"""Demonstrate Knowledge Trees capabilities."""
print_header("2. KNOWLEDGE TREES")
trees = TreeStore()
# Create a concept tree
tree = trees.create_tree("ai_taxonomy", "AI Taxonomy")
# Add nodes
tree.add_node("ai", "Artificial Intelligence")
tree.add_node("ml", "Machine Learning", parent_id="ai")
tree.add_node("dl", "Deep Learning", parent_id="ml")
tree.add_node("nlp", "NLP", parent_id="ai")
tree.add_node("tokenization", "Tokenization", parent_id="nlp")
tree.add_node("embeddings", "Embeddings", parent_id="nlp")
tree.add_node("cv", "Computer Vision", parent_id="ai")
print(f"\nTree: {tree.name} ({len(tree)} nodes)")
print("\nHierarchy:")
def print_tree(node_id: str, indent: int = 0):
node = tree.get_node(node_id)
if node:
print(" " * indent + f"├─ {node.content}")
for child_id in node.children_ids:
print_tree(child_id, indent + 1)
print_tree("ai")
# Path finding
path = tree.get_path_from_root("embeddings")
print(f"\nPath to 'Embeddings': {' → '.join(n.content for n in path)}")
return trees
def demo_unified_memory():
"""Demonstrate Unified Memory capabilities."""
print_header("3. UNIFIED MEMORY")
memory = UnifiedMemory()
# Add knowledge
facts = [
("SanTOK is a unique tokenization system", "fact"),
("SanTOK uses 9 different tokenization methods", "fact"),
("Tokenization converts text to tokens", "concept"),
("Embeddings represent tokens as vectors", "concept"),
("SanTOK generates embeddings without neural networks", "fact"),
]
for content, ctype in facts:
memory.add(content, ctype, auto_link_graph=True)
# Add relations
graph = memory.graph
nodes = list(graph.get_all_nodes())
if len(nodes) >= 2:
graph.add_edge(nodes[0].node_id, nodes[1].node_id, RelationType.RELATED_TO)
print(f"\nMemory: {len(memory)} objects")
print(f"Graph: {memory.graph.node_count} nodes")
print(f"Trees: {len(memory.trees)} trees")
print("\nStored facts:")
for obj in list(memory.objects.values())[:5]:
print(f" - {obj.content[:50]}...")
return memory
def demo_symbolic_reasoning(memory: UnifiedMemory):
"""Demonstrate Symbolic Reasoning capabilities."""
print_header("4. SYMBOLIC REASONING")
# Build a graph for reasoning
graph = GraphStore()
nodes = [
(1, "SanTOK", "system"),
(2, "Knowledge System", "class"),
(3, "AI System", "class"),
(4, "Software", "class"),
]
for nid, text, ntype in nodes:
graph.add_node(GraphNode(nid, text, node_type=ntype))
edges = [
(1, 2, RelationType.IS_A),
(2, 3, RelationType.IS_A),
(3, 4, RelationType.IS_A),
]
for src, tgt, rel in edges:
graph.add_edge(src, tgt, rel)
# Run inference
engine = InferenceEngine(graph)
engine.rules.add_builtin_rules()
result = engine.infer_all()
print("\nDirect relationships:")
for edge in graph.get_all_edges():
src = graph.get_node(edge.source_id)
tgt = graph.get_node(edge.target_id)
print(f" {src.text} IS_A {tgt.text}")
print(f"\n✨ Inferred {len(result.inferred_facts)} new facts:")
for fact in result.inferred_facts:
src = graph.get_node(fact.source_id)
tgt = graph.get_node(fact.target_id)
print(f" {src.text} --[{fact.relation.value}]--> {tgt.text}")
print(f" (Rule: {fact.rule_id}, Confidence: {fact.confidence:.0%})")
def demo_custom_algorithms():
"""Demonstrate all custom algorithms."""
print_header("5. CUSTOM ALGORITHMS")
# 5.1 Query Parser
print("\n5.1 QUERY PARSER")
print("-" * 40)
parser = SanTOKQueryParser()
queries = [
"What is machine learning?",
"How does SanTOK work?",
"Is Python a programming language?",
"What are the parts of a computer?",
]
for query in queries:
parsed = parser.parse(query)
print(f"\n Q: \"{query}\"")
print(f" Type: {parsed.query_type.value}")
print(f" Subject: {parsed.subject}")
# 5.2 Pattern Matcher
print("\n5.2 PATTERN MATCHER")
print("-" * 40)
matcher = SanTOKPatternMatcher()
texts = [
"Python is a programming language.",
"Machine learning depends on data.",
"SanTOK uses tokenization.",
]
for text in texts:
matches = matcher.extract(text)
if matches:
print(f"\n \"{text}\"")
for match in matches:
print(f" → {match}")
# 5.3 Semantic Similarity
print("\n5.3 SEMANTIC SIMILARITY")
print("-" * 40)
sim = SanTOKSimilarity()
pairs = [
("machine learning", "deep learning"),
("dog", "cat"),
("tokenization", "embedding"),
("hello world", "goodbye universe"),
]
for text_a, text_b in pairs:
result = sim.compute(text_a, text_b)
print(f"\n \"{text_a}\" vs \"{text_b}\"")
print(f" Score: {result.score:.4f} (DR={result.digital_root})")
# 5.4 9-Centric Scorer
print("\n5.4 9-CENTRIC SCORER")
print("-" * 40)
scorer = SanTOK9Scorer()
values = [0.95, 0.72, 0.5, 0.33]
for value in values:
score = scorer.to_9(value)
meaning = scorer.interpret_root(score.digital_root)
print(f" {value:.2f} → DR={score.digital_root} ({meaning})")
# 5.5 Graph Walker
print("\n5.5 GRAPH WALKER")
print("-" * 40)
graph = GraphStore()
for i in range(1, 5):
graph.add_node(GraphNode(i, f"Node{i}", "test"))
graph.add_edge(1, 2, RelationType.RELATED_TO)
graph.add_edge(2, 3, RelationType.RELATED_TO)
graph.add_edge(3, 4, RelationType.RELATED_TO)
walker = SanTOKGraphWalker(graph)
result = walker.walk(1, 4, mode=WalkMode.WEIGHTED)
print(f"\n Path from Node1 to Node4:")
path = " → ".join(s.node_text for s in result.path)
print(f" {path}")
print(f" Score: {result.total_score:.4f}")
def demo_full_pipeline():
"""Demonstrate complete pipeline."""
print_header("6. FULL PIPELINE")
# Create memory
memory = UnifiedMemory()
# Add knowledge
texts = [
"SanTOK is a unique tokenization system",
"SanTOK uses 9 different methods",
"Tokenization is part of NLP",
"NLP is a branch of AI",
"SanTOK generates embeddings without neural networks",
]
for text in texts:
memory.add(text, "fact", auto_link_graph=True)
# Create reasoner
reasoner = SanTOKReasoner(memory)
# Answer questions
questions = [
"What is SanTOK?",
"How does tokenization work?",
]
print("\nQ&A Demo:")
for question in questions:
print(f"\n Q: {question}")
answer = reasoner.ask(question)
print(f" A: {answer.text}")
print(f" Confidence: {answer.confidence:.0%}")
if answer.inferences_made:
print(f" Inferences: {len(answer.inferences_made)}")
def main():
"""Run complete showcase."""
print("""
╔═══════════════════════════════════════════════════════════════════════════════╗
║ SANTOK COGNITIVE SHOWCASE ║
╠═══════════════════════════════════════════════════════════════════════════════╣
║ ║
║ A complete demonstration of SanTOK's cognitive capabilities. ║
║ ║
║ 100% UNIQUE. 100% SANTOK-NATIVE. ║
║ ║
║ ✗ NO GPT ✗ NO Transformers ✗ NO Neural Networks ║
║ ✗ NO PyTorch ✗ NO TensorFlow ✗ NO External AI ║
║ ║
║ ✓ Pure Symbolic ✓ Rule-Based ✓ Template Generation ║
║ ✓ Custom Algorithms ✓ 9-Centric Math ✓ 100% Explainable ║
║ ║
╚═══════════════════════════════════════════════════════════════════════════════╝
""")
# Run demos
graph = demo_knowledge_graph()
trees = demo_knowledge_trees()
memory = demo_unified_memory()
demo_symbolic_reasoning(memory)
demo_custom_algorithms()
demo_full_pipeline()
# Summary
print_header("SHOWCASE COMPLETE")
print("""
What makes SanTOK Cognitive UNIQUE:
KNOWLEDGE STORAGE
├── Graph Store → Relationships, multi-hop reasoning
├── Tree Store → Hierarchy, taxonomy, inheritance
└── Unified Memory → Links vectors, graphs, trees
REASONING ENGINE
├── Inference Engine → Rule chaining (20+ rules)
├── Contradiction Detector → Find conflicts
└── Path Finder → Multi-hop paths
CUSTOM ALGORITHMS
├── SanTOKRanker → Hybrid relevance scoring
├── SanTOKPatternMatcher → Relation extraction (no ML)
├── SanTOK9Scorer → 9-centric confidence
├── SanTOKGraphWalker → Energy-based traversal
├── SanTOKSimilarity → Semantic similarity (no neural)
└── SanTOKQueryParser → NL to structured query
VERBALIZATION
└── Template-based generation (NO neural LLM)
All of this is 100% SanTOK-original.
No borrowed algorithms. No external AI.
This is what makes SanTOK STAND OUT.
""")
if __name__ == "__main__":
main()