File size: 3,179 Bytes
824c85b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pandas as pd
from datetime import datetime
import hashlib
import uuid

# Load core memory layers
memory_path = "SegmentMemory.csv"
conflict_path = "SegmentConflictMap.csv"

memory_df = pd.read_csv(memory_path)
conflict_df = pd.read_csv(conflict_path)

def generate_segment_id():
    # Simulate infinite room logic by adding new numeric ID
    existing_ids = memory_df['SegmentID'].astype(int)
    return str(existing_ids.max() + 1 if not existing_ids.empty else 1).zfill(4)

def hash_segment(segment):
    return hashlib.sha256(segment.encode()).hexdigest()[:12]

def insert_segment(raw_text, concepts, terms, structure, datapoints, comparisons, applications):
    new_hash = hash_segment(raw_text)

    # Check for duplicates by hash
    if new_hash in memory_df['HashChecksum'].values:
        print("Duplicate segment detected. Skipping insertion.")
        return

    # Basic simulated trust/conflict logic
    trust_score = 0.75 if "error" not in raw_text.lower() else -0.4
    relevance_score = 0.6
    entropy = 0.5

    # Check conflicts by comparing against concepts or raw text
    conflicting_rows = memory_df[memory_df['RawText'].str.contains(raw_text.split(" ")[0], case=False, na=False)]
    if not conflicting_rows.empty:
        for _, row in conflicting_rows.iterrows():
            conflict_entry = {
                "SegmentA": row["SegmentID"],
                "SegmentB": generate_segment_id(),
                "ConflictType": "Concept Overlap",
                "ConflictScore": 0.8,
                "ResolutionStrategy": "Manual Review",
                "WinningSegment": "",
                "LastChecked": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
                "ResolutionStatus": "Pending Review",
                "AgentTrigger": "System",
                "Notes": "Auto-detected via insertion engine"
            }
            conflict_df.loc[len(conflict_df)] = conflict_entry
            conflict_df.to_csv(conflict_path, index=False)
        print("Conflict detected. Segment routed to ConflictMap.")
        return

    # Accept segment
    new_row = {
        "SegmentID": generate_segment_id(),
        "RawText": raw_text,
        "Concepts": concepts,
        "Terms": terms,
        "Structure": structure,
        "DataPoints": datapoints,
        "Comparisons": comparisons,
        "Applications": applications,
        "Links": "",
        "Entropy": entropy,
        "TrustScore": trust_score,
        "RelevanceScore": relevance_score,
        "RecallCount": 0,
        "LastUsed": "",
        "ConflictsWith": "",
        "Verified": trust_score > 0.5,
        "HashChecksum": new_hash
    }

    memory_df.loc[len(memory_df)] = new_row
    memory_df.to_csv(memory_path, index=False)
    print(f"Segment added successfully with ID {new_row['SegmentID']}")

# Example Usage
if __name__ == "__main__":
    insert_segment(
        "Hydrogen is the lightest element in the periodic table.",
        "lightest element, hydrogen",
        "hydrogen, element, periodic",
        "Chemistry > Elements",
        "Atomic number: 1, Mass: 1.008",
        "Compared to helium and lithium",
        "Used in hydrogen fuel cells"
    )