Spaces:
Sleeping
Sleeping
Upload fso_content_shatterer.py with huggingface_hub
Browse files- fso_content_shatterer.py +79 -0
fso_content_shatterer.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import hashlib
|
| 2 |
+
import re
|
| 3 |
+
|
| 4 |
+
class ContentShatterer:
|
| 5 |
+
"""
|
| 6 |
+
Law XII Component: The Atomic Shatterer
|
| 7 |
+
Breaks down flat files into multi-dimensional topological atoms.
|
| 8 |
+
"""
|
| 9 |
+
def __init__(self, m=256, k=4):
|
| 10 |
+
self.m = m
|
| 11 |
+
self.k = k
|
| 12 |
+
|
| 13 |
+
def _get_coord(self, atom_data, target_fiber):
|
| 14 |
+
"""Map an atom's data to a Z_m^k coordinate on a specific fiber."""
|
| 15 |
+
h = hashlib.sha256(atom_data.encode('utf-8') if isinstance(atom_data, str) else atom_data).digest()
|
| 16 |
+
coords = []
|
| 17 |
+
for i in range(self.k - 1):
|
| 18 |
+
coords.append(h[i % len(h)] % self.m)
|
| 19 |
+
|
| 20 |
+
# Closure Lemma (Law III)
|
| 21 |
+
w = (target_fiber - sum(coords)) % self.m
|
| 22 |
+
coords.append(w)
|
| 23 |
+
return tuple(coords)
|
| 24 |
+
|
| 25 |
+
def shatter(self, filename, content):
|
| 26 |
+
"""Shatters content based on file type."""
|
| 27 |
+
atoms = []
|
| 28 |
+
|
| 29 |
+
if filename.endswith(('.py', '.js', '.c', '.rs', '.go')):
|
| 30 |
+
# Fiber 1: Code / Logic
|
| 31 |
+
fiber = 1
|
| 32 |
+
# Simple logical block splitting (e.g., by double newlines or common keywords)
|
| 33 |
+
blocks = re.split(r'\n\s*\n', content.decode('utf-8', errors='ignore'))
|
| 34 |
+
for b in blocks:
|
| 35 |
+
if b.strip():
|
| 36 |
+
atoms.append({
|
| 37 |
+
"data": b.strip(),
|
| 38 |
+
"fiber": fiber,
|
| 39 |
+
"coord": self._get_coord(b.strip(), fiber),
|
| 40 |
+
"type": "logic_block"
|
| 41 |
+
})
|
| 42 |
+
|
| 43 |
+
elif filename.endswith(('.json', '.csv', '.txt', '.pdf', '.md')):
|
| 44 |
+
# Fiber 2: Knowledge / Data
|
| 45 |
+
fiber = 2
|
| 46 |
+
text = content.decode('utf-8', errors='ignore')
|
| 47 |
+
# Split by sentences (simple version)
|
| 48 |
+
sentences = re.split(r'(?<=[.!?])\s+', text)
|
| 49 |
+
for s in sentences:
|
| 50 |
+
if s.strip():
|
| 51 |
+
atoms.append({
|
| 52 |
+
"data": s.strip(),
|
| 53 |
+
"fiber": fiber,
|
| 54 |
+
"coord": self._get_coord(s.strip(), fiber),
|
| 55 |
+
"type": "knowledge_atom"
|
| 56 |
+
})
|
| 57 |
+
else:
|
| 58 |
+
# Fiber 3: Aesthetics / Raw Binary
|
| 59 |
+
fiber = 3
|
| 60 |
+
# Chunking binary into 64-byte atoms
|
| 61 |
+
chunk_size = 64
|
| 62 |
+
for i in range(0, len(content), chunk_size):
|
| 63 |
+
chunk = content[i:i+chunk_size]
|
| 64 |
+
atoms.append({
|
| 65 |
+
"data": chunk,
|
| 66 |
+
"fiber": fiber,
|
| 67 |
+
"coord": self._get_coord(chunk, fiber),
|
| 68 |
+
"type": "aesthetic_chunk"
|
| 69 |
+
})
|
| 70 |
+
|
| 71 |
+
return atoms
|
| 72 |
+
|
| 73 |
+
if __name__ == "__main__":
|
| 74 |
+
shatterer = ContentShatterer()
|
| 75 |
+
test_text = "TGI is the future of intelligence. It maps raw data into geometric tori. Determinism is the key."
|
| 76 |
+
atoms = shatterer.shatter("test.txt", test_text.encode())
|
| 77 |
+
print(f"Shattered test.txt into {len(atoms)} atoms.")
|
| 78 |
+
for a in atoms:
|
| 79 |
+
print(f" Atom: '{a['data'][:30]}...' -> Coord: {a['coord']}")
|