tzero-mcp 10.7.0 → 10.7.2
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.
- package/CHANGELOG.md +589 -0
- package/CONTRIBUTING.md +71 -0
- package/README.md +25 -17
- package/bin/postinstall.js +34 -0
- package/bin/tzero-mcp.js +16 -19
- package/bin/tzero.js +14 -19
- package/index.d.ts +24 -0
- package/index.js +7 -3
- package/package.json +24 -2
- package/src/addmcp.py +1194 -0
- package/src/main.py +60 -0
- package/src/siber_akademi_icon.py +77 -0
- package/src/tzero/jarvis/semantic_compressor.py +373 -0
- package/src/tzero.py +58 -0
- package/src/tzero_benchmark.py +475 -0
- package/src/tzero_bridge.py +362 -0
- package/src/tzero_ci.py +153 -0
- package/src/tzero_commit.py +192 -0
- package/src/tzero_cost.py +163 -0
- package/src/tzero_dashboard.py +1448 -0
- package/src/tzero_deps.py +287 -0
- package/src/tzero_drift.py +208 -0
- package/src/tzero_explain.py +240 -0
- package/src/tzero_features.py +3022 -0
- package/src/tzero_fix.py +228 -0
- package/src/tzero_fixer/__init__.py +63 -0
- package/src/tzero_fixer/amalgamation.py +334 -0
- package/src/tzero_fixer/ast_syntax_healer.py +464 -0
- package/src/tzero_fixer/ast_type_modernizer.py +288 -0
- package/src/tzero_fixer/async_io_modernizer.py +127 -0
- package/src/tzero_fixer/automated_regression_suite.py +600 -0
- package/src/tzero_fixer/benchmarks_and_diagnostics.py +174 -0
- package/src/tzero_fixer/ci_cd_self_healing_gate.py +246 -0
- package/src/tzero_fixer/code_duplicity_and_refactorer.py +151 -0
- package/src/tzero_fixer/code_smells_healer.py +260 -0
- package/src/tzero_fixer/cwe_remediator.py +171 -0
- package/src/tzero_fixer/dead_code_eliminator.py +150 -0
- package/src/tzero_fixer/dependency_vulnerability_auditor.py +309 -0
- package/src/tzero_fixer/git_advanced_surgeon.py +280 -0
- package/src/tzero_fixer/hardware_and_disk_doctor.py +325 -0
- package/src/tzero_fixer/import_graph_healer.py +412 -0
- package/src/tzero_fixer/interactive_tui.py +91 -0
- package/src/tzero_fixer/knowledge_base.py +295 -0
- package/src/tzero_fixer/knowledge_base_extended.py +940 -0
- package/src/tzero_fixer/linters_and_static_analyzer.py +307 -0
- package/src/tzero_fixer/mcp_server.py +447 -0
- package/src/tzero_fixer/models.py +385 -0
- package/src/tzero_fixer/orchestrator.py +375 -0
- package/src/tzero_fixer/os_doctor.py +502 -0
- package/src/tzero_fixer/packaging_abi_surgeon.py +323 -0
- package/src/tzero_fixer/pc_fixer_doctor.py +599 -0
- package/src/tzero_fixer/performance_and_memory_profiler.py +274 -0
- package/src/tzero_fixer/polyglot_ast_parsers.py +313 -0
- package/src/tzero_fixer/polyglot_build_healer.py +364 -0
- package/src/tzero_fixer/polyglot_config_healer.py +318 -0
- package/src/tzero_fixer/polyglot_doctor.py +372 -0
- package/src/tzero_fixer/polyglot_linters.py +619 -0
- package/src/tzero_fixer/polyglot_schema_validator.py +305 -0
- package/src/tzero_fixer/security_cwe_database.py +444 -0
- package/src/tzero_fixer/security_deep_rules.py +114 -0
- package/src/tzero_fixer/security_rules_catalog.py +110 -0
- package/src/tzero_fixer/security_zero_leak.py +326 -0
- package/src/tzero_fixer/smart_patch_recommender.py +223 -0
- package/src/tzero_fixer/synthetic_workspace_generator.py +220 -0
- package/src/tzero_fixer/system_diagnostics_extended.py +493 -0
- package/src/tzero_fixer/system_repair_recipes.py +447 -0
- package/src/tzero_fixer/venv_doctor.py +309 -0
- package/src/tzero_gateway.py +885 -0
- package/src/tzero_guard.py +243 -0
- package/src/tzero_infra.py +274 -0
- package/src/tzero_jarvis/__init__.py +124 -0
- package/src/tzero_jarvis/causal_graph.py +310 -0
- package/src/tzero_jarvis/graph_visualizer.py +395 -0
- package/src/tzero_jarvis/hybrid_search.py +214 -0
- package/src/tzero_jarvis/latent_press.py +137 -0
- package/src/tzero_jarvis/memory_consolidation.py +212 -0
- package/src/tzero_jarvis/minhash_lsh.py +184 -0
- package/src/tzero_jarvis/orchestrator.py +335 -0
- package/src/tzero_jarvis/self_healing.py +308 -0
- package/src/tzero_jarvis/semantic_compressor.py +887 -0
- package/src/tzero_jarvis/swarm.py +261 -0
- package/src/tzero_jarvis/swarm_bft.py +144 -0
- package/src/tzero_jarvis/workspace_orchestrator.py +1167 -0
- package/src/tzero_jarvis/zero_key_router.py +173 -0
- package/src/tzero_mcp.py +4858 -0
- package/src/tzero_neural_cortex.py +1425 -0
- package/src/tzero_packer.py +370 -0
- package/src/tzero_patch.py +624 -0
- package/src/tzero_pc_fixer.py +12657 -0
- package/src/tzero_pipe.py +95 -0
- package/src/tzero_polyglot.py +422 -0
- package/src/tzero_reachability.py +272 -0
- package/src/tzero_replay.py +141 -0
- package/src/tzero_security.py +570 -0
- package/src/tzero_snapshot.py +258 -0
- package/src/tzero_stubs.py +313 -0
- package/src/tzero_superdesign.py +774 -0
- package/src/tzero_sync.py +241 -0
- package/src/tzero_task_benchmark.py +363 -0
- package/src/tzero_tui.py +244 -0
- package/src/tzero_v3.py +9435 -0
|
@@ -0,0 +1,887 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
T-ZERO JARVIS UNIVERSAL 3-LAYER SEMANTIC MEMORY & COMPRESSION ENGINE
|
|
4
|
+
---------------------------------------------------------------------
|
|
5
|
+
Developer: Toprak Ahmet Aydoğmuş (Siber Akademi)
|
|
6
|
+
Version: 4.3.0
|
|
7
|
+
|
|
8
|
+
Evrensel 3-Katmanlı Anlamsal Çekirdek ve Vektör Bellek Mimarisi:
|
|
9
|
+
Katman A: Çok Dilli ve Çok Alanlı Anlamsal Graf Çıkarımı (TR + EN + 12 Alanlı NER)
|
|
10
|
+
Katman B: Deterministik Vektör Kuantizasyon Sözlüğü (VQ Codebook + Graf Topolojisi)
|
|
11
|
+
Katman C: Dinamik Soft-Prompt Sanal Bellek Enjeksiyonu (<|tzero:mem:ID|>) + Bellek Arama
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
import re
|
|
15
|
+
import os
|
|
16
|
+
import json
|
|
17
|
+
import hashlib
|
|
18
|
+
import ipaddress
|
|
19
|
+
from typing import List, Dict, Optional, Tuple, Any, Union, Set
|
|
20
|
+
from dataclasses import dataclass, asdict
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class EntityType:
|
|
24
|
+
HARDWARE = "HARDWARE"
|
|
25
|
+
PROTOCOL = "PROTOCOL"
|
|
26
|
+
IP = "IP"
|
|
27
|
+
CRYPTO = "CRYPTO"
|
|
28
|
+
VULN = "VULN"
|
|
29
|
+
SOFTWARE = "SOFTWARE"
|
|
30
|
+
QUANTUM = "QUANTUM"
|
|
31
|
+
BIO = "BIO"
|
|
32
|
+
AEROSPACE = "AEROSPACE"
|
|
33
|
+
AUTOMOTIVE = "AUTOMOTIVE"
|
|
34
|
+
FINANCE = "FINANCE"
|
|
35
|
+
ROBOTICS = "ROBOTICS"
|
|
36
|
+
GENERIC = "GENERIC"
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@dataclass
|
|
40
|
+
class TypedEntity:
|
|
41
|
+
name: str
|
|
42
|
+
entity_type: str = EntityType.GENERIC
|
|
43
|
+
|
|
44
|
+
def to_tagged_string(self) -> str:
|
|
45
|
+
if self.entity_type == EntityType.GENERIC:
|
|
46
|
+
return self.name
|
|
47
|
+
return f"{self.name}:{self.entity_type}"
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
@dataclass
|
|
51
|
+
class SemanticTriplet:
|
|
52
|
+
"""Saf anlamsal çizge üçlüsü (Graph Triplet) with NER."""
|
|
53
|
+
subject: str
|
|
54
|
+
predicate: str
|
|
55
|
+
objects: List[TypedEntity]
|
|
56
|
+
contexts: List[TypedEntity]
|
|
57
|
+
|
|
58
|
+
def to_graph_string(self) -> str:
|
|
59
|
+
"""Kompakt çizge gösterimi üretir: (Özne)->[Yüklem]->(Nesne:TIP + Nesne:TIP | Bağlam:TIP)"""
|
|
60
|
+
obj_str = " + ".join(o.to_tagged_string() for o in self.objects) if self.objects else "Hedef"
|
|
61
|
+
ctx_str = " | ".join(c.to_tagged_string() for c in self.contexts) if self.contexts else ""
|
|
62
|
+
if ctx_str:
|
|
63
|
+
return f"({self.subject})->[{self.predicate}]->({obj_str} | {ctx_str})"
|
|
64
|
+
return f"({self.subject})->[{self.predicate}]->({obj_str})"
|
|
65
|
+
|
|
66
|
+
def to_compact_code(self) -> str:
|
|
67
|
+
"""Token tasarrufu sağlayan ultra sıkışık format: &S:Özne#P:Eylem#O:Nesneler#C:Bağlamlar"""
|
|
68
|
+
obj_str = "+".join(o.to_tagged_string() for o in self.objects)
|
|
69
|
+
ctx_str = "|".join(c.to_tagged_string() for c in self.contexts)
|
|
70
|
+
return f"&S:{self.subject}#P:{self.predicate}#O:{obj_str}#C:{ctx_str}"
|
|
71
|
+
|
|
72
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
73
|
+
return {
|
|
74
|
+
"subject": self.subject,
|
|
75
|
+
"predicate": self.predicate,
|
|
76
|
+
"objects": [asdict(o) for o in self.objects],
|
|
77
|
+
"contexts": [asdict(c) for c in self.contexts],
|
|
78
|
+
"graph_representation": self.to_graph_string(),
|
|
79
|
+
"compact_code": self.to_compact_code()
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
class SemanticTripletExtractor:
|
|
84
|
+
"""Katman A: Dolgu kelimeleri temizleyip mantıksal çizgeye dönüştüren evrensel çıkarıcı."""
|
|
85
|
+
|
|
86
|
+
STOPWORDS = frozenset({
|
|
87
|
+
# Türkçe Dolgular
|
|
88
|
+
"ve", "veya", "ile", "için", "icin", "dolayısıyla", "dolayisiyla", "bu", "şu", "su", "o",
|
|
89
|
+
"bir", "birkaç", "birkac", "dün", "dun", "akşam", "aksam", "sabah", "öğle", "ogle", "gece",
|
|
90
|
+
"bugün", "bugun", "yarın", "yarin", "şimdi", "simdi", "üzerinde", "uzerinde", "üzerine", "uzerine",
|
|
91
|
+
"altında", "altinda", "yanında", "yaninda", "tarafından", "tarafindan", "aracılığıyla", "araciligiyla",
|
|
92
|
+
"vasıtasıyla", "vasitasiyla", "bağlayarak", "baglayarak", "kullanarak", "yoluyla", "şekilde", "sekilde",
|
|
93
|
+
"falan", "filan", "şey", "sey", "dair", "göre", "gore", "kadar", "beri", "sonra", "önce", "once",
|
|
94
|
+
"dolayı", "dolayi", "ötürü", "oturu", "adeta", "neredeyse",
|
|
95
|
+
"kart", "karti", "kartı", "kartını", "kartini", "modül", "modul", "modülü", "modulu", "modülün", "modulun", "modülünü", "modulunu",
|
|
96
|
+
"port", "portu", "portuna", "üzeri", "uzeri", "üzerin", "uzerin", "üzerinde", "uzerinde", "üzerine", "uzerine",
|
|
97
|
+
"cihaz", "cihazı", "cihazi", "cihazını", "sunucu", "sunucusu", "sunucusunu", "küme", "kume", "kümesi", "kumesi",
|
|
98
|
+
"kümesini", "tablo", "tablosu", "tablosunu", "ekip", "ekibi", "kütüphane", "kutuphane", "kütüphanesi",
|
|
99
|
+
# İngilizce Dolgular
|
|
100
|
+
"and", "or", "with", "for", "therefore", "thus", "hence", "the", "a", "an",
|
|
101
|
+
"yesterday", "evening", "today", "morning", "night", "now", "on", "in", "at",
|
|
102
|
+
"by", "through", "using", "via", "connecting", "connected", "over", "under", "into",
|
|
103
|
+
"from", "to", "after", "before", "while", "during", "towards", "as", "well",
|
|
104
|
+
"module", "card", "board", "chip", "port", "bus", "system", "environment", "device"
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
HARDWARE_KEYWORDS = frozenset({
|
|
108
|
+
"ESP32", "ESP8266", "NRF24L01", "NRF24", "STM32", "ARDUINO", "RASPBERRY", "RASPBERRYPI",
|
|
109
|
+
"PIC", "AVR", "FPGA", "CPLD", "RP2040", "TEENSY", "SENSOR", "OLED", "LCD", "SERVO",
|
|
110
|
+
"MOTOR", "RELAY", "TRANSCEIVER", "ACCELEROMETER", "GYROSCOPE", "BAROMETER", "RFID",
|
|
111
|
+
"MICROCONTROLLER", "MCU", "SOC", "RISCV", "RISC-V", "ARM", "CORTEX", "PCB", "ASIC"
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
PROTOCOL_KEYWORDS = frozenset({
|
|
115
|
+
"SPI", "I2C", "UART", "CAN", "GPIO", "USB", "TCP", "UDP", "HTTP", "HTTPS",
|
|
116
|
+
"REST", "GRPC", "WEBSOCKET", "MQTT", "COAP", "BLE", "WIFI", "LORA", "ZIGBEE",
|
|
117
|
+
"TLS", "SSL", "SSH", "IPSEC", "MODBUS", "PROFIBUS", "ETHERNET", "PCIE", "DMA", "JTAG", "SWD"
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
CRYPTO_KEYWORDS = frozenset({
|
|
121
|
+
"AES-256", "AES-128", "AES", "RSA-4096", "RSA-2048", "RSA", "ECC", "ECDSA",
|
|
122
|
+
"SHA-256", "SHA-512", "SHA-3", "MD5", "HMAC", "PBKDF2", "CHACHA20", "POLY1305",
|
|
123
|
+
"JWT", "OAUTH2", "ARGON2", "BCRYPT", "PGP", "GPG", "ZERO-KNOWLEDGE", "ZK-SNARK"
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
SOFTWARE_KEYWORDS = frozenset({
|
|
127
|
+
"KUBERNETES", "K8S", "DOCKER", "POSTGRESQL", "POSTGRES", "MYSQL", "MONGODB",
|
|
128
|
+
"REDIS", "RABBITMQ", "KAFKA", "NGINX", "APACHE", "LINUX", "WINDOWS", "PYTORCH",
|
|
129
|
+
"TENSORFLOW", "FASTAPI", "NODEJS", "REACT", "VUE", "TAILWIND", "RUST", "PYTHON",
|
|
130
|
+
"ELK", "ELASTICSEARCH", "PROMETHEUS", "GRAFANA", "TERRAFORM", "ANSIBLE", "GIT"
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
QUANTUM_KEYWORDS = frozenset({
|
|
134
|
+
"QUBIT", "QUBITS", "QISKIT", "CIRQ", "SHOR", "GROVER", "HAMILTONIAN", "TELEPORTATION",
|
|
135
|
+
"SUPERCONDUCTING", "IONTRAP", "QUANTUM_GATE", "QUANTUM", "BELL_STATE", "ENTANGLEMENT"
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
BIO_KEYWORDS = frozenset({
|
|
139
|
+
"CRISPR", "CAS9", "ALPHAFOLD", "MRNA", "DNA", "RNA", "EXON", "INTRON", "CODON",
|
|
140
|
+
"RIBOSOME", "GENOME", "GENOMICS", "PROTEIN", "BLAST", "TRANSCRIPTION", "TRANSLATION"
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
AEROSPACE_KEYWORDS = frozenset({
|
|
144
|
+
"AVIONICS", "TELEMETRY", "IMU", "CANAEROSPACE", "ARINC429", "ARINC", "MIL-STD-1553",
|
|
145
|
+
"GYROSCOPE", "THRUST", "RCS", "SATELLITE", "STARTRACKER", "ORBIT", "PROPULSION"
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
AUTOMOTIVE_KEYWORDS = frozenset({
|
|
149
|
+
"AUTOSAR", "CAN_FD", "CANFD", "LIN", "FLEXRAY", "ECU", "BMS", "OBD2", "OBD",
|
|
150
|
+
"OBD-II", "LIDAR", "ADAS", "RADAR", "DRIVEBYWIRE"
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
FINANCE_KEYWORDS = frozenset({
|
|
154
|
+
"ORDERBOOK", "FIX", "VWAP", "ARBITRAGE", "MARKETMAKER", "HFT", "DERIVATIVE",
|
|
155
|
+
"OPTION", "HEDGING", "ALGOTRADING", "EQUITY", "LIQUIDITY"
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
ROBOTICS_KEYWORDS = frozenset({
|
|
159
|
+
"ROS", "ROS2", "MICROROS", "MICRO-ROS", "DDS", "URDF", "SLAM", "KINEMATICS",
|
|
160
|
+
"GAZEBO", "ACTUATOR", "SERVO", "ODOMETRY"
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
ACTION_MAP = {
|
|
164
|
+
# Türkçe Eylemler
|
|
165
|
+
"test etti": "test_etti",
|
|
166
|
+
"test_etti": "test_etti",
|
|
167
|
+
"denedi": "test_etti",
|
|
168
|
+
"devreye aldı": "devreye_aldi",
|
|
169
|
+
"devreye aldi": "devreye_aldi",
|
|
170
|
+
"bağladı": "bagladi",
|
|
171
|
+
"bagladi": "bagladi",
|
|
172
|
+
"çalıştırdı": "calistirdi",
|
|
173
|
+
"calistirdi": "calistirdi",
|
|
174
|
+
"derledi": "derledi",
|
|
175
|
+
"yazdı": "yazdi",
|
|
176
|
+
"analiz etti": "analiz_etti",
|
|
177
|
+
"onardı": "onardi",
|
|
178
|
+
"onardi": "onardi",
|
|
179
|
+
"düzeltti": "onardi",
|
|
180
|
+
"kurdu": "kurdu",
|
|
181
|
+
"eğitti": "egitti",
|
|
182
|
+
"egitti": "egitti",
|
|
183
|
+
"şifreledi": "sifreledi",
|
|
184
|
+
"sifreledi": "sifreledi",
|
|
185
|
+
"doğruladı": "dogruladi",
|
|
186
|
+
"dogruladi": "dogruladi",
|
|
187
|
+
"izledi": "izledi",
|
|
188
|
+
"kalibre etti": "kalibre_etti",
|
|
189
|
+
"entegre etti": "entegre_etti",
|
|
190
|
+
"sentezledi": "sentezledi",
|
|
191
|
+
"simüle etti": "simule_etti",
|
|
192
|
+
"yamaladı": "yamaladi",
|
|
193
|
+
"yapılandırdı": "yapilandirdi",
|
|
194
|
+
# İngilizce Eylemler
|
|
195
|
+
"tested": "test_etti",
|
|
196
|
+
"ran": "calistirdi",
|
|
197
|
+
"compiled": "derledi",
|
|
198
|
+
"analyzed": "analiz_etti",
|
|
199
|
+
"fixed": "onardi",
|
|
200
|
+
"repaired": "onardi",
|
|
201
|
+
"deployed": "devreye_aldi",
|
|
202
|
+
"trained": "egitti",
|
|
203
|
+
"encrypted": "sifreledi",
|
|
204
|
+
"verified": "dogruladi",
|
|
205
|
+
"connected": "bagladi",
|
|
206
|
+
"configured": "yapilandirdi",
|
|
207
|
+
"patched": "yamaladi",
|
|
208
|
+
"calibrated": "kalibre_etti",
|
|
209
|
+
"integrated": "entegre_etti",
|
|
210
|
+
"monitored": "izledi",
|
|
211
|
+
"simulated": "simule_etti",
|
|
212
|
+
"synthesized": "sentezledi",
|
|
213
|
+
"reverse engineered": "tersine_muhendislik_yapti"
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
# 5. TÜRKÇE İSİM ÇEKİM EKLERİ TEMİZLEYİCİ (Stemmer / Lemmatizer)
|
|
217
|
+
@classmethod
|
|
218
|
+
def strip_turkish_suffixes(cls, word: str) -> str:
|
|
219
|
+
"""Kesme işaretini ve yönelme/ayrılma eklerini atar (ESP32'yi -> ESP32)."""
|
|
220
|
+
base = word.split("'")[0].split("’")[0]
|
|
221
|
+
suffixes = (
|
|
222
|
+
"sinde", "sında", "sinden", "sından", "leriyle", "larıyla", "lerin", "ların",
|
|
223
|
+
"lerinin", "larının", "lerine", "larına", "den", "dan", "ten", "tan", "nin",
|
|
224
|
+
"nın", "nun", "nün", "de", "da", "te", "ta", "ye", "ya", "ne", "na",
|
|
225
|
+
"in", "ın", "un", "ün", "e", "a", "i", "ı", "u", "ü"
|
|
226
|
+
)
|
|
227
|
+
clean = base
|
|
228
|
+
clean_lower = clean.lower()
|
|
229
|
+
# Meslek / fail / icracı eklerini koru (-ici, -ıcı, -ucu, -ücü, -maci, -macı)
|
|
230
|
+
agent_suffixes = ("ici", "ıcı", "ucu", "ücü", "maci", "macı", "men", "man", "ist", "tor", "tör", "er", "or")
|
|
231
|
+
if any(clean_lower.endswith(asuf) for asuf in agent_suffixes):
|
|
232
|
+
return clean
|
|
233
|
+
|
|
234
|
+
for suf in suffixes:
|
|
235
|
+
if clean_lower.endswith(suf) and len(clean) > len(suf) + 3:
|
|
236
|
+
clean = clean[:-len(suf)]
|
|
237
|
+
break
|
|
238
|
+
return clean
|
|
239
|
+
|
|
240
|
+
# 3. VARLIK TİPİ TESPİTİ (12-Alanlı Kapsamlı NER Tagging)
|
|
241
|
+
@classmethod
|
|
242
|
+
def classify_entity(cls, token: str) -> str:
|
|
243
|
+
upper = token.upper()
|
|
244
|
+
|
|
245
|
+
# 1. IP Adresi
|
|
246
|
+
try:
|
|
247
|
+
ipaddress.IPv4Address(upper)
|
|
248
|
+
return EntityType.IP
|
|
249
|
+
except ValueError:
|
|
250
|
+
pass
|
|
251
|
+
|
|
252
|
+
# 2. Güvenlik Zafiyeti (CVE-YYYY-NNNNN vb.)
|
|
253
|
+
if upper.startswith("CVE-") or "VULN" in upper or "EXPLOIT" in upper:
|
|
254
|
+
return EntityType.VULN
|
|
255
|
+
|
|
256
|
+
# 3. Kriptografi
|
|
257
|
+
if upper in cls.CRYPTO_KEYWORDS or upper.startswith("AES") or upper.startswith("RSA"):
|
|
258
|
+
return EntityType.CRYPTO
|
|
259
|
+
|
|
260
|
+
# 4. Kuantum Bilişim
|
|
261
|
+
if upper in cls.QUANTUM_KEYWORDS or upper.startswith("QUBIT"):
|
|
262
|
+
return EntityType.QUANTUM
|
|
263
|
+
|
|
264
|
+
# 5. Biyoinformatik & Genetik
|
|
265
|
+
if upper in cls.BIO_KEYWORDS or upper.startswith("CRISPR") or upper.startswith("CAS9"):
|
|
266
|
+
return EntityType.BIO
|
|
267
|
+
|
|
268
|
+
# 6. Havacılık & Aviyonik
|
|
269
|
+
if upper in cls.AEROSPACE_KEYWORDS:
|
|
270
|
+
return EntityType.AEROSPACE
|
|
271
|
+
|
|
272
|
+
# 7. Otomotiv & Otonom Sürüş
|
|
273
|
+
if upper in cls.AUTOMOTIVE_KEYWORDS:
|
|
274
|
+
return EntityType.AUTOMOTIVE
|
|
275
|
+
|
|
276
|
+
# 8. Finansal Mühendislik & HFT
|
|
277
|
+
if upper in cls.FINANCE_KEYWORDS:
|
|
278
|
+
return EntityType.FINANCE
|
|
279
|
+
|
|
280
|
+
# 9. Robotik & Kontrol
|
|
281
|
+
if upper in cls.ROBOTICS_KEYWORDS or upper.startswith("ROS"):
|
|
282
|
+
return EntityType.ROBOTICS
|
|
283
|
+
|
|
284
|
+
# 10. Protokol
|
|
285
|
+
if upper in cls.PROTOCOL_KEYWORDS:
|
|
286
|
+
return EntityType.PROTOCOL
|
|
287
|
+
|
|
288
|
+
# 11. Donanım
|
|
289
|
+
if upper in cls.HARDWARE_KEYWORDS or upper.startswith("NRF") or upper.startswith("ESP") or upper.startswith("STM"):
|
|
290
|
+
return EntityType.HARDWARE
|
|
291
|
+
|
|
292
|
+
# 12. Yazılım
|
|
293
|
+
if upper in cls.SOFTWARE_KEYWORDS:
|
|
294
|
+
return EntityType.SOFTWARE
|
|
295
|
+
|
|
296
|
+
return EntityType.GENERIC
|
|
297
|
+
|
|
298
|
+
# 2. TÜRKÇE & İNGİLİZCE BİLEŞİK FİİLLER (Light Verbs) n-gram DEDEKTÖRÜ
|
|
299
|
+
@classmethod
|
|
300
|
+
def detect_compound_predicate(cls, tokens: List[str]) -> Optional[Tuple[str, int, int]]:
|
|
301
|
+
auxiliaries = {"etti", "yaptı", "yapti", "aldı", "aldi", "kıldı", "kildi", "oldu", "verdi", "geçti", "gecti", "buldu"}
|
|
302
|
+
for i in range(len(tokens) - 1):
|
|
303
|
+
first = re.sub(r"[^\w]", "", tokens[i]).lower()
|
|
304
|
+
second = re.sub(r"[^\w]", "", tokens[i + 1]).lower()
|
|
305
|
+
combined = f"{first} {second}"
|
|
306
|
+
if combined in cls.ACTION_MAP:
|
|
307
|
+
return (cls.ACTION_MAP[combined], i, i + 1)
|
|
308
|
+
if any(aux in second for aux in auxiliaries):
|
|
309
|
+
return (f"{first}_{second}", i, i + 1)
|
|
310
|
+
return None
|
|
311
|
+
|
|
312
|
+
@classmethod
|
|
313
|
+
def _normalize_predicate(cls, raw_verb: str) -> str:
|
|
314
|
+
v = raw_verb.lower().strip()
|
|
315
|
+
for k, val in cls.ACTION_MAP.items():
|
|
316
|
+
if k in v:
|
|
317
|
+
return val
|
|
318
|
+
if "test" in v or "dene" in v:
|
|
319
|
+
return "test_etti"
|
|
320
|
+
if "bağla" in v or "connect" in v:
|
|
321
|
+
return "bagladi"
|
|
322
|
+
if "çalıştır" in v or "run" in v:
|
|
323
|
+
return "calistirdi"
|
|
324
|
+
if "derle" in v or "compile" in v:
|
|
325
|
+
return "derledi"
|
|
326
|
+
if "şifrele" in v or "encrypt" in v:
|
|
327
|
+
return "sifreledi"
|
|
328
|
+
if "dağıt" in v or "deploy" in v:
|
|
329
|
+
return "devreye_aldi"
|
|
330
|
+
if "kur" in v or "install" in v:
|
|
331
|
+
return "kurdu"
|
|
332
|
+
if "analiz" in v or "analyze" in v:
|
|
333
|
+
return "analiz_etti"
|
|
334
|
+
if "onar" in v or "fix" in v or "repair" in v:
|
|
335
|
+
return "onardi"
|
|
336
|
+
if "eğit" in v or "train" in v:
|
|
337
|
+
return "egitti"
|
|
338
|
+
if "doğrula" in v or "verify" in v:
|
|
339
|
+
return "dogruladi"
|
|
340
|
+
if "kalibre" in v or "calibrate" in v:
|
|
341
|
+
return "kalibre_etti"
|
|
342
|
+
if "entegre" in v or "integrate" in v:
|
|
343
|
+
return "entegre_etti"
|
|
344
|
+
return "islem_yapti"
|
|
345
|
+
|
|
346
|
+
@classmethod
|
|
347
|
+
def extract_triplets(cls, text: str) -> List[SemanticTriplet]:
|
|
348
|
+
"""Metinden anlamsal graf üçlülerini eksiksiz ve deterministik çıkarır."""
|
|
349
|
+
triplets: List[SemanticTriplet] = []
|
|
350
|
+
if not text or not text.strip():
|
|
351
|
+
return triplets
|
|
352
|
+
|
|
353
|
+
# Cümle bazlı ayrıştırma
|
|
354
|
+
sentences = re.split(r"[\.\!\?;\n]+", text)
|
|
355
|
+
for sent in sentences:
|
|
356
|
+
sent_str = sent.strip()
|
|
357
|
+
if not sent_str:
|
|
358
|
+
continue
|
|
359
|
+
|
|
360
|
+
raw_tokens = sent_str.split()
|
|
361
|
+
if not raw_tokens:
|
|
362
|
+
continue
|
|
363
|
+
|
|
364
|
+
# 1. Bileşik Eylemleri n-gram ile yakala
|
|
365
|
+
comp = cls.detect_compound_predicate(raw_tokens)
|
|
366
|
+
if comp:
|
|
367
|
+
predicate = comp[0]
|
|
368
|
+
verb_start, verb_end = comp[1], comp[2]
|
|
369
|
+
else:
|
|
370
|
+
# SVO (İngilizce/Doğrudan) veya bilinen fiilleri ara (Örn: "Developer deployed Kubernetes")
|
|
371
|
+
found_verb = None
|
|
372
|
+
for idx, tok in enumerate(raw_tokens):
|
|
373
|
+
clean_tok = re.sub(r"[^\w]", "", tok).lower()
|
|
374
|
+
if clean_tok in cls.ACTION_MAP:
|
|
375
|
+
found_verb = (cls.ACTION_MAP[clean_tok], idx, idx)
|
|
376
|
+
break
|
|
377
|
+
|
|
378
|
+
if found_verb:
|
|
379
|
+
predicate, verb_start, verb_end = found_verb
|
|
380
|
+
else:
|
|
381
|
+
# SOV (Türkçe cümle sonu) fiilini kontrol et
|
|
382
|
+
verb_candidate = raw_tokens[-1]
|
|
383
|
+
predicate = cls._normalize_predicate(verb_candidate)
|
|
384
|
+
verb_start, verb_end = len(raw_tokens) - 1, len(raw_tokens) - 1
|
|
385
|
+
|
|
386
|
+
# 2. Özne Tespiti
|
|
387
|
+
subject = ""
|
|
388
|
+
subject_idx = None
|
|
389
|
+
for idx, tok in enumerate(raw_tokens):
|
|
390
|
+
clean = cls.strip_turkish_suffixes(re.sub(r"[^\w\-\.]", "", tok))
|
|
391
|
+
if clean and clean.lower() not in cls.STOPWORDS:
|
|
392
|
+
subject = clean
|
|
393
|
+
subject_idx = idx
|
|
394
|
+
break
|
|
395
|
+
|
|
396
|
+
if not subject:
|
|
397
|
+
subject = "Sistem"
|
|
398
|
+
|
|
399
|
+
# 3. Nesneler & Bağlamlar
|
|
400
|
+
objects: List[TypedEntity] = []
|
|
401
|
+
contexts: List[TypedEntity] = []
|
|
402
|
+
|
|
403
|
+
for idx, tok in enumerate(raw_tokens):
|
|
404
|
+
if idx == subject_idx:
|
|
405
|
+
continue
|
|
406
|
+
if verb_start is not None and verb_end is not None and verb_start <= idx <= verb_end:
|
|
407
|
+
continue
|
|
408
|
+
|
|
409
|
+
stemmed = cls.strip_turkish_suffixes(re.sub(r"[^\w\-\.]", "", tok))
|
|
410
|
+
if not stemmed or stemmed.lower() in cls.STOPWORDS:
|
|
411
|
+
continue
|
|
412
|
+
|
|
413
|
+
ent_type = cls.classify_entity(stemmed)
|
|
414
|
+
typed_ent = TypedEntity(stemmed, ent_type)
|
|
415
|
+
|
|
416
|
+
# Evrensel Bağlam Tespiti (Morfolojik veya Anahtar Kelime)
|
|
417
|
+
raw_low = tok.lower()
|
|
418
|
+
clean_low = stemmed.lower()
|
|
419
|
+
is_context = (
|
|
420
|
+
ent_type == EntityType.PROTOCOL or
|
|
421
|
+
any(raw_low.endswith(sfx) for sfx in (
|
|
422
|
+
"da", "de", "ta", "te", "dan", "den", "tan", "ten",
|
|
423
|
+
"ında", "inde", "sında", "sinde", "unda", "ünde", "sunda", "sünde"
|
|
424
|
+
)) or
|
|
425
|
+
any(env in clean_low or env in raw_low for env in (
|
|
426
|
+
"laboratuvar", "lab", "atölye", "atolye", "oda", "saha", "kart", "board", "pin", "port",
|
|
427
|
+
"sunucu", "server", "küme", "kume", "cluster", "cloud", "bulut", "production", "prod", "staging", "dev",
|
|
428
|
+
"ağ", "network", "subnet", "dmz", "vpn", "intranet", "internet", "firewall", "honeypot", "sandbox",
|
|
429
|
+
"kriyostat", "cryostat", "vakum", "kavite", "cavity", "lattice",
|
|
430
|
+
"hücre", "hucre", "cell", "doku", "tissue", "petri", "in_vitro", "in_vivo", "reaktör",
|
|
431
|
+
"yörünge", "yorunge", "orbit", "uzay", "space", "atmosfer", "istasyon", "hangar", "uydu",
|
|
432
|
+
"araç", "arac", "vehicle", "şasi", "sasi", "chassis", "motor", "otoyol", "highway", "kabin",
|
|
433
|
+
"borsa", "market", "piyasa", "exchange", "kasa", "cüzdan", "wallet", "banka", "bank",
|
|
434
|
+
"fabrika", "factory", "konveyör", "konveyor", "arena", "simülasyon", "simulasyon", "sim",
|
|
435
|
+
"ev", "ofis", "şehir", "sehir", "arşiv", "arsiv", "kütüphane", "kutuphane", "masada", "ekranda", "ortam", "environment"
|
|
436
|
+
))
|
|
437
|
+
)
|
|
438
|
+
|
|
439
|
+
if is_context:
|
|
440
|
+
contexts.append(typed_ent)
|
|
441
|
+
elif len(objects) < 8:
|
|
442
|
+
if not any(o.name == typed_ent.name for o in objects):
|
|
443
|
+
objects.append(typed_ent)
|
|
444
|
+
|
|
445
|
+
triplets.append(SemanticTriplet(subject, predicate, objects, contexts))
|
|
446
|
+
|
|
447
|
+
return triplets
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
class VectorQuantizedCodebook:
|
|
451
|
+
"""Katman B: VQ Codebook, Graf Topolojisi ve Kalıcı Disk Depolama."""
|
|
452
|
+
|
|
453
|
+
def __init__(self, seed_code: int = 8421):
|
|
454
|
+
self._codebook: Dict[int, SemanticTriplet] = {}
|
|
455
|
+
self._reverse_index: Dict[str, int] = {}
|
|
456
|
+
self._seed_reference_codes()
|
|
457
|
+
|
|
458
|
+
def _seed_reference_codes(self):
|
|
459
|
+
"""12 Temel Evrensel Bilim ve Mühendislik Alanı için Çekirdek Referans Senaryolar."""
|
|
460
|
+
omni_scenarios = [
|
|
461
|
+
# 1. Hardware / Embedded IoT
|
|
462
|
+
(8421, SemanticTriplet(
|
|
463
|
+
subject="Toprak",
|
|
464
|
+
predicate="test_etti",
|
|
465
|
+
objects=[TypedEntity("ESP32", EntityType.HARDWARE), TypedEntity("nRF24L01", EntityType.HARDWARE)],
|
|
466
|
+
contexts=[TypedEntity("SPI", EntityType.PROTOCOL), TypedEntity("laboratuvar", EntityType.GENERIC)]
|
|
467
|
+
)),
|
|
468
|
+
# 2. Software & Cloud DevOps
|
|
469
|
+
(8422, SemanticTriplet(
|
|
470
|
+
subject="Geliştirici",
|
|
471
|
+
predicate="deploy_etti",
|
|
472
|
+
objects=[TypedEntity("Kubernetes", EntityType.SOFTWARE), TypedEntity("PostgreSQL", EntityType.SOFTWARE)],
|
|
473
|
+
contexts=[TypedEntity("Docker", EntityType.SOFTWARE), TypedEntity("production", EntityType.GENERIC)]
|
|
474
|
+
)),
|
|
475
|
+
# 3. Cybersecurity & Red Teaming
|
|
476
|
+
(8423, SemanticTriplet(
|
|
477
|
+
subject="Analist",
|
|
478
|
+
predicate="tespit_etti",
|
|
479
|
+
objects=[TypedEntity("CVE-2024-1234", EntityType.VULN), TypedEntity("BufferOverflow", EntityType.VULN)],
|
|
480
|
+
contexts=[TypedEntity("DMZ", EntityType.GENERIC), TypedEntity("Exploit", EntityType.SOFTWARE)]
|
|
481
|
+
)),
|
|
482
|
+
# 4. Quantum Computing
|
|
483
|
+
(8424, SemanticTriplet(
|
|
484
|
+
subject="Fizikçi",
|
|
485
|
+
predicate="dolaşıklaştırdı",
|
|
486
|
+
objects=[TypedEntity("Qubit", EntityType.QUANTUM), TypedEntity("BellState", EntityType.QUANTUM)],
|
|
487
|
+
contexts=[TypedEntity("Kriyostat", EntityType.QUANTUM), TypedEntity("Qiskit", EntityType.QUANTUM)]
|
|
488
|
+
)),
|
|
489
|
+
# 5. Bio-Informatics & Genomics
|
|
490
|
+
(8425, SemanticTriplet(
|
|
491
|
+
subject="Genetikçi",
|
|
492
|
+
predicate="modifiye_etti",
|
|
493
|
+
objects=[TypedEntity("CRISPR", EntityType.BIO), TypedEntity("Exon", EntityType.BIO)],
|
|
494
|
+
contexts=[TypedEntity("AlphaFold", EntityType.BIO), TypedEntity("hücre", EntityType.BIO)]
|
|
495
|
+
)),
|
|
496
|
+
# 6. Aerospace & Avionics
|
|
497
|
+
(8426, SemanticTriplet(
|
|
498
|
+
subject="Mühendis",
|
|
499
|
+
predicate="kalibre_etti",
|
|
500
|
+
objects=[TypedEntity("Telemetry", EntityType.AEROSPACE), TypedEntity("Gyroscope", EntityType.AEROSPACE)],
|
|
501
|
+
contexts=[TypedEntity("yörünge", EntityType.AEROSPACE), TypedEntity("Uydu", EntityType.AEROSPACE)]
|
|
502
|
+
)),
|
|
503
|
+
# 7. Automotive & Autonomous Driving
|
|
504
|
+
(8427, SemanticTriplet(
|
|
505
|
+
subject="Sürücü_Yazılımı",
|
|
506
|
+
predicate="denetledi",
|
|
507
|
+
objects=[TypedEntity("ECU", EntityType.AUTOMOTIVE), TypedEntity("LiDAR", EntityType.AUTOMOTIVE)],
|
|
508
|
+
contexts=[TypedEntity("CAN", EntityType.PROTOCOL), TypedEntity("otoyol", EntityType.AUTOMOTIVE)]
|
|
509
|
+
)),
|
|
510
|
+
# 8. Financial Algorithmic Trading
|
|
511
|
+
(8428, SemanticTriplet(
|
|
512
|
+
subject="AlgoTrader",
|
|
513
|
+
predicate="arbitraj_yaptı",
|
|
514
|
+
objects=[TypedEntity("OrderBook", EntityType.FINANCE), TypedEntity("Derivative", EntityType.FINANCE)],
|
|
515
|
+
contexts=[TypedEntity("Borsa", EntityType.FINANCE), TypedEntity("NASDAQ", EntityType.FINANCE)]
|
|
516
|
+
)),
|
|
517
|
+
# 9. Robotics & Automation
|
|
518
|
+
(8429, SemanticTriplet(
|
|
519
|
+
subject="Kontrolcü",
|
|
520
|
+
predicate="harekete_geçirdi",
|
|
521
|
+
objects=[TypedEntity("Actuator", EntityType.ROBOTICS), TypedEntity("Manipulator", EntityType.ROBOTICS)],
|
|
522
|
+
contexts=[TypedEntity("ROS", EntityType.SOFTWARE), TypedEntity("fabrika", EntityType.ROBOTICS)]
|
|
523
|
+
)),
|
|
524
|
+
# 10. Cryptography & Zero-Knowledge
|
|
525
|
+
(8430, SemanticTriplet(
|
|
526
|
+
subject="Kriptograf",
|
|
527
|
+
predicate="doğruladı",
|
|
528
|
+
objects=[TypedEntity("ZK-SNARK", EntityType.CRYPTO), TypedEntity("SHA-256", EntityType.CRYPTO)],
|
|
529
|
+
contexts=[TypedEntity("TLS", EntityType.PROTOCOL), TypedEntity("Blockchain", EntityType.CRYPTO)]
|
|
530
|
+
)),
|
|
531
|
+
# 11. Artificial Intelligence & LLMs
|
|
532
|
+
(8431, SemanticTriplet(
|
|
533
|
+
subject="Araştırmacı",
|
|
534
|
+
predicate="fine_tune_etti",
|
|
535
|
+
objects=[TypedEntity("Transformer", EntityType.SOFTWARE), TypedEntity("Weights", EntityType.SOFTWARE)],
|
|
536
|
+
contexts=[TypedEntity("LoRA", EntityType.SOFTWARE), TypedEntity("GPU_Cluster", EntityType.HARDWARE)]
|
|
537
|
+
)),
|
|
538
|
+
# 12. Universal Science & Human Cognition
|
|
539
|
+
(8432, SemanticTriplet(
|
|
540
|
+
subject="İnsan",
|
|
541
|
+
predicate="araştırdı",
|
|
542
|
+
objects=[TypedEntity("Bilgi", EntityType.GENERIC), TypedEntity("Hipotez", EntityType.GENERIC)],
|
|
543
|
+
contexts=[TypedEntity("Evren", EntityType.GENERIC), TypedEntity("Kütüphane", EntityType.GENERIC)]
|
|
544
|
+
)),
|
|
545
|
+
]
|
|
546
|
+
for code_id, triplet in omni_scenarios:
|
|
547
|
+
self.register_triplet_with_code(code_id, triplet)
|
|
548
|
+
|
|
549
|
+
def _compute_signature(self, triplet: SemanticTriplet) -> str:
|
|
550
|
+
sorted_objs = sorted(o.to_tagged_string().lower() for o in triplet.objects)
|
|
551
|
+
sorted_ctxs = sorted(c.to_tagged_string().lower() for c in triplet.contexts)
|
|
552
|
+
obj_str = "+".join(sorted_objs)
|
|
553
|
+
ctx_str = "|".join(sorted_ctxs)
|
|
554
|
+
return f"{triplet.subject.lower()}::{triplet.predicate.lower()}::{obj_str}::{ctx_str}"
|
|
555
|
+
|
|
556
|
+
def register_triplet(self, triplet: SemanticTriplet) -> int:
|
|
557
|
+
sig = self._compute_signature(triplet)
|
|
558
|
+
if sig in self._reverse_index:
|
|
559
|
+
return self._reverse_index[sig]
|
|
560
|
+
|
|
561
|
+
h = hashlib.sha256(sig.encode("utf-8")).hexdigest()
|
|
562
|
+
code_id = (int(h[:6], 16) % 80000) + 1000
|
|
563
|
+
|
|
564
|
+
while code_id in self._codebook and self._compute_signature(self._codebook[code_id]) != sig:
|
|
565
|
+
code_id += 1
|
|
566
|
+
|
|
567
|
+
self._codebook[code_id] = triplet
|
|
568
|
+
self._reverse_index[sig] = code_id
|
|
569
|
+
return code_id
|
|
570
|
+
|
|
571
|
+
def register_triplet_with_code(self, code_id: int, triplet: SemanticTriplet):
|
|
572
|
+
sig = self._compute_signature(triplet)
|
|
573
|
+
self._codebook[code_id] = triplet
|
|
574
|
+
self._reverse_index[sig] = code_id
|
|
575
|
+
|
|
576
|
+
def lookup(self, code_id: int) -> Optional[SemanticTriplet]:
|
|
577
|
+
return self._codebook.get(code_id)
|
|
578
|
+
|
|
579
|
+
def size(self) -> int:
|
|
580
|
+
return len(self._codebook)
|
|
581
|
+
|
|
582
|
+
@property
|
|
583
|
+
def codes(self) -> Dict[int, Dict[str, Any]]:
|
|
584
|
+
"""Sözlükteki tüm kodları metadata sözlüğü olarak döndürür."""
|
|
585
|
+
result = {}
|
|
586
|
+
for cid, trip in self._codebook.items():
|
|
587
|
+
primary_dom = "GENERIC"
|
|
588
|
+
if trip.objects:
|
|
589
|
+
primary_dom = trip.objects[0].entity_type
|
|
590
|
+
elif trip.contexts:
|
|
591
|
+
primary_dom = trip.contexts[0].entity_type
|
|
592
|
+
result[cid] = {
|
|
593
|
+
"graph": trip.to_graph_string(),
|
|
594
|
+
"compact_code": trip.to_compact_code(),
|
|
595
|
+
"primary_domain": primary_dom,
|
|
596
|
+
"triplet": trip
|
|
597
|
+
}
|
|
598
|
+
return result
|
|
599
|
+
|
|
600
|
+
# -------------------------------------------------------------------------
|
|
601
|
+
# ANLAMSAL GRAF ARAMA (Semantic Search across Knowledge Graph)
|
|
602
|
+
# -------------------------------------------------------------------------
|
|
603
|
+
def query_graph(
|
|
604
|
+
self,
|
|
605
|
+
entity: Optional[str] = None,
|
|
606
|
+
predicate: Optional[str] = None,
|
|
607
|
+
context: Optional[str] = None,
|
|
608
|
+
text_query: Optional[str] = None
|
|
609
|
+
) -> List[Dict[str, Any]]:
|
|
610
|
+
"""Hafıza sözlüğünde kayıtlı çizgeleri varlık, eylem veya bağlam bazında filtreler."""
|
|
611
|
+
results = []
|
|
612
|
+
term = text_query.lower().strip() if text_query else None
|
|
613
|
+
|
|
614
|
+
for code_id, trip in self._codebook.items():
|
|
615
|
+
matched = True
|
|
616
|
+
|
|
617
|
+
if entity:
|
|
618
|
+
ent_low = entity.lower()
|
|
619
|
+
subj_match = ent_low in trip.subject.lower()
|
|
620
|
+
obj_match = any(ent_low in o.name.lower() for o in trip.objects)
|
|
621
|
+
if not (subj_match or obj_match):
|
|
622
|
+
matched = False
|
|
623
|
+
|
|
624
|
+
if predicate and matched:
|
|
625
|
+
if predicate.lower() not in trip.predicate.lower():
|
|
626
|
+
matched = False
|
|
627
|
+
|
|
628
|
+
if context and matched:
|
|
629
|
+
ctx_low = context.lower()
|
|
630
|
+
if not any(ctx_low in c.name.lower() for c in trip.contexts):
|
|
631
|
+
matched = False
|
|
632
|
+
|
|
633
|
+
if term and matched:
|
|
634
|
+
full_str = f"{trip.subject} {trip.predicate} {' '.join(o.name for o in trip.objects)} {' '.join(c.name for c in trip.contexts)}".lower()
|
|
635
|
+
if term not in full_str:
|
|
636
|
+
matched = False
|
|
637
|
+
|
|
638
|
+
if matched:
|
|
639
|
+
results.append({
|
|
640
|
+
"code_id": code_id,
|
|
641
|
+
"virtual_token": f"<|tzero:mem:{code_id}|>",
|
|
642
|
+
"triplet": trip.to_dict(),
|
|
643
|
+
"graph_string": trip.to_graph_string(),
|
|
644
|
+
"compact_code": trip.to_compact_code()
|
|
645
|
+
})
|
|
646
|
+
|
|
647
|
+
return results
|
|
648
|
+
|
|
649
|
+
# -------------------------------------------------------------------------
|
|
650
|
+
# GRAF TOPOLOJİSİ (Graph Topology for Visual HUD)
|
|
651
|
+
# -------------------------------------------------------------------------
|
|
652
|
+
def get_topology(self) -> Dict[str, Any]:
|
|
653
|
+
"""Tüm hafıza çizgelerinden düğüm (Node) ve kenar (Edge) listesi çıkarır."""
|
|
654
|
+
nodes: Dict[str, Dict[str, Any]] = {}
|
|
655
|
+
edges: List[Dict[str, Any]] = []
|
|
656
|
+
|
|
657
|
+
for code_id, trip in self._codebook.items():
|
|
658
|
+
# Subject Node
|
|
659
|
+
if trip.subject not in nodes:
|
|
660
|
+
nodes[trip.subject] = {"id": trip.subject, "label": trip.subject, "type": "SUBJECT"}
|
|
661
|
+
|
|
662
|
+
# Objects & Contexts
|
|
663
|
+
for o in trip.objects:
|
|
664
|
+
if o.name not in nodes:
|
|
665
|
+
nodes[o.name] = {"id": o.name, "label": o.name, "type": o.entity_type}
|
|
666
|
+
edges.append({
|
|
667
|
+
"from": trip.subject,
|
|
668
|
+
"to": o.name,
|
|
669
|
+
"predicate": trip.predicate,
|
|
670
|
+
"code_id": code_id
|
|
671
|
+
})
|
|
672
|
+
|
|
673
|
+
for c in trip.contexts:
|
|
674
|
+
if c.name not in nodes:
|
|
675
|
+
nodes[c.name] = {"id": c.name, "label": c.name, "type": c.entity_type}
|
|
676
|
+
edges.append({
|
|
677
|
+
"from": trip.subject,
|
|
678
|
+
"to": c.name,
|
|
679
|
+
"predicate": f"in_{c.name}",
|
|
680
|
+
"code_id": code_id
|
|
681
|
+
})
|
|
682
|
+
|
|
683
|
+
return {
|
|
684
|
+
"total_nodes": len(nodes),
|
|
685
|
+
"total_edges": len(edges),
|
|
686
|
+
"nodes": list(nodes.values()),
|
|
687
|
+
"edges": edges
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
# -------------------------------------------------------------------------
|
|
691
|
+
# KALICI DİSK DEPOLAMA (Persistent Memory Store)
|
|
692
|
+
# -------------------------------------------------------------------------
|
|
693
|
+
def save_to_disk(self, filepath: Optional[str] = None) -> str:
|
|
694
|
+
"""Kayıtlı hafızayı diske JSON olarak serialize eder."""
|
|
695
|
+
target = filepath or os.path.join(os.path.dirname(__file__), "tzero_memory_store.json")
|
|
696
|
+
data = {
|
|
697
|
+
"version": "4.3.0",
|
|
698
|
+
"codes": {
|
|
699
|
+
str(code_id): trip.to_dict()
|
|
700
|
+
for code_id, trip in self._codebook.items()
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
with open(target, "w", encoding="utf-8") as f:
|
|
704
|
+
json.dump(data, f, indent=2, ensure_ascii=False)
|
|
705
|
+
return target
|
|
706
|
+
|
|
707
|
+
def load_from_disk(self, filepath: Optional[str] = None) -> int:
|
|
708
|
+
"""Diskteki kayıtlı hafızayı bellek sözlüğüne yükler."""
|
|
709
|
+
target = filepath or os.path.join(os.path.dirname(__file__), "tzero_memory_store.json")
|
|
710
|
+
if not os.path.exists(target):
|
|
711
|
+
return 0
|
|
712
|
+
|
|
713
|
+
with open(target, "r", encoding="utf-8") as f:
|
|
714
|
+
data = json.load(f)
|
|
715
|
+
|
|
716
|
+
loaded_count = 0
|
|
717
|
+
for code_str, tdict in data.get("codes", {}).items():
|
|
718
|
+
code_id = int(code_str)
|
|
719
|
+
objs = [TypedEntity(o["name"], o["entity_type"]) for o in tdict.get("objects", [])]
|
|
720
|
+
ctxs = [TypedEntity(c["name"], c["entity_type"]) for c in tdict.get("contexts", [])]
|
|
721
|
+
trip = SemanticTriplet(tdict["subject"], tdict["predicate"], objs, ctxs)
|
|
722
|
+
self.register_triplet_with_code(code_id, trip)
|
|
723
|
+
loaded_count += 1
|
|
724
|
+
|
|
725
|
+
return loaded_count
|
|
726
|
+
|
|
727
|
+
|
|
728
|
+
class DynamicVirtualMemoryInjector:
|
|
729
|
+
"""Katman C: Dinamik Çözücü & Soft-Prompt / Virtual KV-Cache Enjeksiyonu."""
|
|
730
|
+
|
|
731
|
+
def __init__(self, codebook: Optional[VectorQuantizedCodebook] = None):
|
|
732
|
+
self.codebook = codebook or VectorQuantizedCodebook()
|
|
733
|
+
|
|
734
|
+
def compress_text_to_virtual_tokens(self, text: str) -> Dict[str, Any]:
|
|
735
|
+
"""Tekil veya kısa cümleleri 3 katmandan geçirir."""
|
|
736
|
+
triplets = SemanticTripletExtractor.extract_triplets(text)
|
|
737
|
+
raw_words = text.split()
|
|
738
|
+
original_token_count = max(1, int(len(raw_words) * 1.3))
|
|
739
|
+
|
|
740
|
+
code_ids = []
|
|
741
|
+
graph_strings = []
|
|
742
|
+
compact_codes = []
|
|
743
|
+
virtual_tokens = []
|
|
744
|
+
|
|
745
|
+
for trip in triplets:
|
|
746
|
+
code_id = self.codebook.register_triplet(trip)
|
|
747
|
+
code_ids.append(code_id)
|
|
748
|
+
graph_strings.append(trip.to_graph_string())
|
|
749
|
+
compact_codes.append(trip.to_compact_code())
|
|
750
|
+
virtual_tokens.append(f"<|tzero:mem:{code_id}|>")
|
|
751
|
+
|
|
752
|
+
compressed_token_count = max(1, len(virtual_tokens))
|
|
753
|
+
token_savings = max(0, original_token_count - compressed_token_count)
|
|
754
|
+
reduction_ratio_pct = round((token_savings / original_token_count) * 100.0, 2)
|
|
755
|
+
|
|
756
|
+
return {
|
|
757
|
+
"original_text": text,
|
|
758
|
+
"original_token_count": original_token_count,
|
|
759
|
+
"triplets": [t.to_dict() for t in triplets],
|
|
760
|
+
"graph_representations": graph_strings,
|
|
761
|
+
"compact_codes": compact_codes,
|
|
762
|
+
"codebook_ids": code_ids,
|
|
763
|
+
"virtual_tokens": virtual_tokens,
|
|
764
|
+
"virtual_token_string": " ".join(virtual_tokens),
|
|
765
|
+
"compressed_token_count": compressed_token_count,
|
|
766
|
+
"token_savings": token_savings,
|
|
767
|
+
"reduction_ratio_pct": reduction_ratio_pct,
|
|
768
|
+
"layer_breakdown": {
|
|
769
|
+
"layer_a_semantic_triplets": graph_strings,
|
|
770
|
+
"layer_b_vq_codes": [f"#{cid}" for cid in code_ids],
|
|
771
|
+
"layer_c_virtual_injection": " ".join(virtual_tokens)
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
# -------------------------------------------------------------------------
|
|
776
|
+
# ÇOK PARAGRAFLI / DÖKÜMAN SIKIŞTIRMA (Hierarchical Document Compressor)
|
|
777
|
+
# -------------------------------------------------------------------------
|
|
778
|
+
def compress_document(self, doc_text: str) -> Dict[str, Any]:
|
|
779
|
+
"""
|
|
780
|
+
Geniş teknik dokümanları, logları veya konuşma geçmişlerini (1000+ kelime)
|
|
781
|
+
tam bir bilgi grafiğine ve kompozit sanal token zincirine dönüştürür.
|
|
782
|
+
"""
|
|
783
|
+
paragraphs = [p.strip() for p in doc_text.split("\n\n") if p.strip()]
|
|
784
|
+
all_triplets: List[SemanticTriplet] = []
|
|
785
|
+
all_code_ids: List[int] = []
|
|
786
|
+
all_virtual_tokens: List[str] = []
|
|
787
|
+
all_graphs: List[str] = []
|
|
788
|
+
|
|
789
|
+
raw_word_count = len(doc_text.split())
|
|
790
|
+
original_token_count = max(1, int(raw_word_count * 1.3))
|
|
791
|
+
|
|
792
|
+
for p in paragraphs:
|
|
793
|
+
triplets = SemanticTripletExtractor.extract_triplets(p)
|
|
794
|
+
for t in triplets:
|
|
795
|
+
cid = self.codebook.register_triplet(t)
|
|
796
|
+
all_triplets.append(t)
|
|
797
|
+
all_code_ids.append(cid)
|
|
798
|
+
all_virtual_tokens.append(f"<|tzero:mem:{cid}|>")
|
|
799
|
+
all_graphs.append(t.to_graph_string())
|
|
800
|
+
|
|
801
|
+
compressed_token_count = max(1, len(all_virtual_tokens))
|
|
802
|
+
token_savings = max(0, original_token_count - compressed_token_count)
|
|
803
|
+
reduction_ratio_pct = round((token_savings / original_token_count) * 100.0, 2) if original_token_count > 0 else 0.0
|
|
804
|
+
|
|
805
|
+
return {
|
|
806
|
+
"total_paragraphs": len(paragraphs),
|
|
807
|
+
"total_triplets_extracted": len(all_triplets),
|
|
808
|
+
"original_word_count": raw_word_count,
|
|
809
|
+
"original_token_count": original_token_count,
|
|
810
|
+
"compressed_token_count": compressed_token_count,
|
|
811
|
+
"token_savings": token_savings,
|
|
812
|
+
"reduction_ratio_pct": reduction_ratio_pct,
|
|
813
|
+
"codebook_ids": all_code_ids,
|
|
814
|
+
"virtual_token_string": " ".join(all_virtual_tokens),
|
|
815
|
+
"graph_representations": all_graphs,
|
|
816
|
+
"topology": self.codebook.get_topology()
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
def decode_virtual_token(self, token_or_code: Union[str, int]) -> Optional[Dict[str, Any]]:
|
|
820
|
+
code_id = None
|
|
821
|
+
if isinstance(token_or_code, int):
|
|
822
|
+
code_id = token_or_code
|
|
823
|
+
elif isinstance(token_or_code, str):
|
|
824
|
+
m = re.search(r"(\d+)", token_or_code)
|
|
825
|
+
if m:
|
|
826
|
+
code_id = int(m.group(1))
|
|
827
|
+
|
|
828
|
+
if code_id is None:
|
|
829
|
+
return None
|
|
830
|
+
|
|
831
|
+
triplet = self.codebook.lookup(code_id)
|
|
832
|
+
if not triplet:
|
|
833
|
+
return None
|
|
834
|
+
|
|
835
|
+
obj_str = " + ".join(o.name for o in triplet.objects)
|
|
836
|
+
ctx_str = " | ".join(c.name for c in triplet.contexts)
|
|
837
|
+
reconstructed = f"{triplet.subject}, {obj_str} hedefini {ctx_str} ortamında {triplet.predicate}." if ctx_str else f"{triplet.subject}, {obj_str} hedefini {triplet.predicate}."
|
|
838
|
+
|
|
839
|
+
return {
|
|
840
|
+
"code_id": code_id,
|
|
841
|
+
"virtual_token": f"<|tzero:mem:{code_id}|>",
|
|
842
|
+
"graph_representation": triplet.to_graph_string(),
|
|
843
|
+
"compact_code": triplet.to_compact_code(),
|
|
844
|
+
"triplet": triplet.to_dict(),
|
|
845
|
+
"reconstructed_summary": reconstructed
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
def query_memory(self, query: str) -> List[Dict[str, Any]]:
|
|
849
|
+
"""Hafıza sözlüğünde doğal dil veya varlık araması yapar."""
|
|
850
|
+
return self.codebook.query_graph(text_query=query)
|
|
851
|
+
|
|
852
|
+
def build_soft_prompt_prefix(self, virtual_tokens: List[str]) -> str:
|
|
853
|
+
decoded_context = []
|
|
854
|
+
for vt in virtual_tokens:
|
|
855
|
+
info = self.decode_virtual_token(vt)
|
|
856
|
+
if info:
|
|
857
|
+
decoded_context.append(f"• {info['virtual_token']} => {info['compact_code']} ({info['graph_representation']})")
|
|
858
|
+
|
|
859
|
+
return (
|
|
860
|
+
"[T-ZERO HIGH-DENSITY VIRTUAL MEMORY STATE]\n"
|
|
861
|
+
+ "\n".join(decoded_context)
|
|
862
|
+
+ "\n[ATTENTION PRIMING COMPLETE - ZERO TOKEN OVERHEAD]\n"
|
|
863
|
+
)
|
|
864
|
+
|
|
865
|
+
# Kolaylık ve Uyumluluk Alias Metotları
|
|
866
|
+
compress_text = compress_text_to_virtual_tokens
|
|
867
|
+
decompress_code = decode_virtual_token
|
|
868
|
+
|
|
869
|
+
def compress_statement(self, text: str) -> Dict[str, Any]:
|
|
870
|
+
"""Tekil bir cümleyi hızlıca sanal bellek tokenına dönüştürür."""
|
|
871
|
+
res = self.compress_text_to_virtual_tokens(text)
|
|
872
|
+
vt = res["virtual_tokens"][0] if res["virtual_tokens"] else "<|tzero:mem:0|>"
|
|
873
|
+
return {
|
|
874
|
+
"triplets": res["triplets"],
|
|
875
|
+
"virtual_token": vt,
|
|
876
|
+
"virtual_tokens": res["virtual_tokens"],
|
|
877
|
+
"codebook_ids": res["codebook_ids"]
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
|
|
881
|
+
_global_compressor = DynamicVirtualMemoryInjector()
|
|
882
|
+
|
|
883
|
+
def get_global_compressor() -> DynamicVirtualMemoryInjector:
|
|
884
|
+
return _global_compressor
|
|
885
|
+
|
|
886
|
+
# Uyumluluk Aliası
|
|
887
|
+
SemanticExtractor = SemanticTripletExtractor
|