File size: 3,584 Bytes
f94712a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import binascii
import numpy as np
import sympy
from collections import Counter

class DissolutionCore:
    def __init__(self, chunk_size=1024):
        """
        The 'Stomach' of LOGOS. Digests raw files into Mathematical Atoms.
        chunk_size: How many bytes to process at once (The Bite Size).
        Protocol 45: Hex Dissolution.
        """
        self.chunk_size = chunk_size
        
        # Pre-compute Prime Weights for all Byte values (0-255)
        # This is a lookup table for "Semantic Gravity"
        self.atom_weights = self._generate_atomic_weights()

    def _generate_atomic_weights(self):
        """
        Maps every possible Byte (00-FF) to its Greatest Prime Factor (GPF).
        Ex: 'FF' (255) -> Factors: 3, 5, 17 -> GPF: 17
        Ex: '07' (7)   -> Factors: 7      -> GPF: 7
        """
        weights = {}
        for i in range(256):
            if i < 2:
                weights[i] = 0 # Null/Unity has no heat
            else:
                factors = sympy.primefactors(i)
                weights[i] = max(factors)
        return weights

    def ingest_file(self, file_path):
        """
        Reads a file and dissolves it into a Hex-Atom Stream.
        Returns: A stream of 'Physical Atoms' ready for the Tension Web.
        """
        if not os.path.exists(file_path):
            raise FileNotFoundError(f"Target not acquired: {file_path}")

        print(f">> [DISSOLVE] Initiating breakdown of: {os.path.basename(file_path)}")
        
        hex_stream = []
        heat_map = []
        
        with open(file_path, 'rb') as f:
            while True:
                # 1. Read Binary
                chunk = f.read(self.chunk_size)
                if not chunk:
                    break
                
                # 2. Convert to Integers (The Atoms)
                # We work with Ints (0-255) because they map directly to Primes
                atoms = list(chunk) 
                
                # 3. Calculate 'Heat' (GPF) for this chunk
                chunk_heat = [self.atom_weights[a] for a in atoms]
                
                # 4. Store (In production, this streams to the DB/Visualizer)
                hex_stream.extend(atoms)
                heat_map.extend(chunk_heat)

        return self._analyze_residue(hex_stream, heat_map)

    def _analyze_residue(self, atoms, heat):
        """
        Produces the 'Meta-Tensor' report from the dissolved matter.
        """
        total_atoms = len(atoms)
        avg_heat = np.mean(heat) if heat else 0
        entropy = self._calculate_shannon_entropy(atoms)
        
        # Find the 'Dominant Resonance' (Most frequent Prime Weight)
        if heat:
            heat_counts = Counter(heat)
            dominant_prime = heat_counts.most_common(1)[0][0]
        else:
            dominant_prime = 0

        return {
            "status": "DISSOLVED",
            "total_atoms": total_atoms,
            "system_entropy": f"{entropy:.4f}",
            "average_heat": f"{avg_heat:.2f}",
            "dominant_resonance": f"Prime {dominant_prime}",
            "hex_preview": binascii.hexlify(bytearray(atoms[:16])).decode('utf-8').upper()
        }

    def _calculate_shannon_entropy(self, data):
        """
        Standard entropy calculation to detect 'Noise' vs 'Signal'.
        """
        if not data: return 0
        counts = Counter(data)
        probs = [count / len(data) for count in counts.values()]
        return -sum(p * np.log2(p) for p in probs)

if __name__ == "__main__":
    print("Dissolution Core v1.0")