tzero-mcp 10.7.4 → 11.0.0

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.
Files changed (127) hide show
  1. package/CHANGELOG.md +56 -29
  2. package/CONTRIBUTING.md +71 -71
  3. package/README.md +64 -64
  4. package/bin/tzero-mcp.js +62 -66
  5. package/bin/tzero.js +61 -65
  6. package/index.d.ts +25 -25
  7. package/index.js +15 -15
  8. package/package.json +88 -88
  9. package/src/addmcp.py +14 -11
  10. package/src/app.py +1810 -0
  11. package/src/main.py +1 -1
  12. package/src/pc_fixer_mcp.py +25 -0
  13. package/src/tzero/__init__.py +54 -0
  14. package/src/tzero/__main__.py +6 -0
  15. package/src/tzero/siber_akademi.ico +0 -0
  16. package/src/tzero/tzero_arch_test.py +463 -0
  17. package/src/{tzero_benchmark.py → tzero/tzero_benchmark.py} +475 -475
  18. package/src/tzero/tzero_blackboard.py +257 -0
  19. package/src/{tzero_bridge.py → tzero/tzero_bridge.py} +546 -362
  20. package/src/tzero/tzero_budget.py +195 -0
  21. package/src/{tzero_ci.py → tzero/tzero_ci.py} +153 -153
  22. package/src/{tzero_commit.py → tzero/tzero_commit.py} +192 -192
  23. package/src/tzero/tzero_contracts.py +1093 -0
  24. package/src/{tzero_cost.py → tzero/tzero_cost.py} +166 -163
  25. package/src/{tzero_dashboard.py → tzero/tzero_dashboard.py} +531 -19
  26. package/src/tzero/tzero_debt.py +461 -0
  27. package/src/{tzero_deps.py → tzero/tzero_deps.py} +179 -4
  28. package/src/{tzero_drift.py → tzero/tzero_drift.py} +208 -208
  29. package/src/{tzero_explain.py → tzero/tzero_explain.py} +73 -7
  30. package/src/{tzero_features.py → tzero/tzero_features.py} +19 -17
  31. package/src/tzero/tzero_federation.py +299 -0
  32. package/src/{tzero_fix.py → tzero/tzero_fix.py} +228 -228
  33. package/src/{tzero_fixer → tzero/tzero_fixer}/__init__.py +63 -63
  34. package/src/{tzero_fixer → tzero/tzero_fixer}/ast_syntax_healer.py +464 -464
  35. package/src/{tzero_fixer → tzero/tzero_fixer}/ast_type_modernizer.py +288 -288
  36. package/src/{tzero_fixer → tzero/tzero_fixer}/async_io_modernizer.py +127 -127
  37. package/src/{tzero_fixer → tzero/tzero_fixer}/automated_regression_suite.py +600 -600
  38. package/src/{tzero_fixer → tzero/tzero_fixer}/benchmarks_and_diagnostics.py +174 -174
  39. package/src/{tzero_fixer → tzero/tzero_fixer}/ci_cd_self_healing_gate.py +246 -246
  40. package/src/{tzero_fixer → tzero/tzero_fixer}/code_duplicity_and_refactorer.py +151 -151
  41. package/src/{tzero_fixer → tzero/tzero_fixer}/code_smells_healer.py +260 -260
  42. package/src/{tzero_fixer → tzero/tzero_fixer}/cwe_remediator.py +171 -171
  43. package/src/{tzero_fixer → tzero/tzero_fixer}/dead_code_eliminator.py +150 -150
  44. package/src/{tzero_fixer → tzero/tzero_fixer}/dependency_vulnerability_auditor.py +309 -309
  45. package/src/{tzero_fixer → tzero/tzero_fixer}/git_advanced_surgeon.py +1 -1
  46. package/src/{tzero_fixer → tzero/tzero_fixer}/hardware_and_disk_doctor.py +1 -1
  47. package/src/{tzero_fixer → tzero/tzero_fixer}/import_graph_healer.py +412 -412
  48. package/src/{tzero_fixer → tzero/tzero_fixer}/interactive_tui.py +91 -91
  49. package/src/{tzero_fixer → tzero/tzero_fixer}/knowledge_base.py +295 -295
  50. package/src/{tzero_fixer → tzero/tzero_fixer}/knowledge_base_extended.py +940 -940
  51. package/src/{tzero_fixer → tzero/tzero_fixer}/linters_and_static_analyzer.py +307 -307
  52. package/src/{tzero_fixer → tzero/tzero_fixer}/mcp_server.py +49 -7
  53. package/src/{tzero_fixer → tzero/tzero_fixer}/os_doctor.py +502 -502
  54. package/src/{tzero_fixer → tzero/tzero_fixer}/packaging_abi_surgeon.py +1 -1
  55. package/src/{tzero_fixer → tzero/tzero_fixer}/pc_fixer_doctor.py +600 -599
  56. package/src/{tzero_fixer → tzero/tzero_fixer}/performance_and_memory_profiler.py +274 -274
  57. package/src/{tzero_fixer → tzero/tzero_fixer}/polyglot_ast_parsers.py +313 -313
  58. package/src/{tzero_fixer → tzero/tzero_fixer}/polyglot_build_healer.py +1 -1
  59. package/src/{tzero_fixer → tzero/tzero_fixer}/polyglot_config_healer.py +318 -318
  60. package/src/{tzero_fixer → tzero/tzero_fixer}/polyglot_doctor.py +372 -372
  61. package/src/{tzero_fixer → tzero/tzero_fixer}/polyglot_linters.py +1 -1
  62. package/src/{tzero_fixer → tzero/tzero_fixer}/polyglot_schema_validator.py +305 -305
  63. package/src/{tzero_fixer → tzero/tzero_fixer}/security_cwe_database.py +444 -444
  64. package/src/{tzero_fixer → tzero/tzero_fixer}/security_deep_rules.py +114 -114
  65. package/src/{tzero_fixer → tzero/tzero_fixer}/security_rules_catalog.py +110 -110
  66. package/src/{tzero_fixer → tzero/tzero_fixer}/security_zero_leak.py +326 -326
  67. package/src/{tzero_fixer → tzero/tzero_fixer}/smart_patch_recommender.py +223 -223
  68. package/src/{tzero_fixer → tzero/tzero_fixer}/synthetic_workspace_generator.py +220 -220
  69. package/src/{tzero_fixer → tzero/tzero_fixer}/system_diagnostics_extended.py +1 -1
  70. package/src/{tzero_fixer → tzero/tzero_fixer}/system_repair_recipes.py +447 -447
  71. package/src/{tzero_fixer → tzero/tzero_fixer}/venv_doctor.py +309 -309
  72. package/src/tzero/tzero_golden.py +319 -0
  73. package/src/tzero/tzero_graph_hud.html +338 -0
  74. package/src/tzero/tzero_graph_rag.py +263 -0
  75. package/src/{tzero_guard.py → tzero/tzero_guard.py} +243 -243
  76. package/src/tzero/tzero_hooks.py +565 -0
  77. package/src/{tzero_infra.py → tzero/tzero_infra.py} +274 -274
  78. package/src/{tzero_jarvis → tzero/tzero_jarvis}/__init__.py +124 -124
  79. package/src/{tzero_jarvis → tzero/tzero_jarvis}/causal_graph.py +310 -310
  80. package/src/{tzero_jarvis → tzero/tzero_jarvis}/graph_visualizer.py +395 -395
  81. package/src/{tzero_jarvis → tzero/tzero_jarvis}/hybrid_search.py +214 -214
  82. package/src/{tzero_jarvis → tzero/tzero_jarvis}/latent_press.py +137 -137
  83. package/src/{tzero_jarvis → tzero/tzero_jarvis}/memory_consolidation.py +212 -212
  84. package/src/{tzero_jarvis → tzero/tzero_jarvis}/minhash_lsh.py +184 -184
  85. package/src/{tzero_jarvis → tzero/tzero_jarvis}/orchestrator.py +335 -335
  86. package/src/{tzero_jarvis → tzero/tzero_jarvis}/self_healing.py +308 -308
  87. package/src/{tzero_jarvis → tzero/tzero_jarvis}/semantic_compressor.py +887 -887
  88. package/src/{tzero_jarvis → tzero/tzero_jarvis}/swarm.py +261 -261
  89. package/src/{tzero_jarvis → tzero/tzero_jarvis}/swarm_bft.py +144 -144
  90. package/src/{tzero_jarvis → tzero/tzero_jarvis}/workspace_orchestrator.py +1167 -1167
  91. package/src/{tzero_jarvis → tzero/tzero_jarvis}/zero_key_router.py +173 -173
  92. package/src/tzero/tzero_lsp.py +634 -0
  93. package/src/{tzero_mcp.py → tzero/tzero_mcp.py} +479 -100
  94. package/src/tzero/tzero_mutation.py +358 -0
  95. package/src/tzero/tzero_packer.py +577 -0
  96. package/src/{tzero_patch.py → tzero/tzero_patch.py} +624 -624
  97. package/src/{tzero_pc_fixer.py → tzero/tzero_pc_fixer.py} +12699 -12657
  98. package/src/{tzero_pipe.py → tzero/tzero_pipe.py} +95 -95
  99. package/src/tzero/tzero_polyglot.py +799 -0
  100. package/src/tzero/tzero_proxy.py +127 -0
  101. package/src/{tzero_reachability.py → tzero/tzero_reachability.py} +272 -272
  102. package/src/{tzero_replay.py → tzero/tzero_replay.py} +141 -141
  103. package/src/tzero/tzero_rust_bridge.py +198 -0
  104. package/src/tzero/tzero_scip.py +368 -0
  105. package/src/{tzero_security.py → tzero/tzero_security.py} +5 -2
  106. package/src/tzero/tzero_semantic_merge.py +189 -0
  107. package/src/tzero/tzero_snapshot.py +471 -0
  108. package/src/{tzero_stubs.py → tzero/tzero_stubs.py} +313 -313
  109. package/src/tzero/tzero_subagent_engine.py +459 -0
  110. package/src/{tzero_superdesign.py → tzero/tzero_superdesign.py} +774 -774
  111. package/src/{tzero_sync.py → tzero/tzero_sync.py} +241 -241
  112. package/src/{tzero_task_benchmark.py → tzero/tzero_task_benchmark.py} +458 -363
  113. package/src/{tzero_tui.py → tzero/tzero_tui.py} +91 -22
  114. package/src/tzero/tzero_turbo.py +264 -0
  115. package/src/{tzero_v3.py → tzero/tzero_v3.py} +628 -163
  116. package/src/tzero.py +2 -54
  117. package/test/package.test.js +55 -43
  118. package/src/tzero/jarvis/semantic_compressor.py +0 -373
  119. package/src/tzero_gateway.py +0 -885
  120. package/src/tzero_packer.py +0 -370
  121. package/src/tzero_polyglot.py +0 -422
  122. package/src/tzero_snapshot.py +0 -258
  123. /package/src/{siber_akademi_icon.py → tzero/siber_akademi_icon.py} +0 -0
  124. /package/src/{tzero_fixer → tzero/tzero_fixer}/amalgamation.py +0 -0
  125. /package/src/{tzero_fixer → tzero/tzero_fixer}/models.py +0 -0
  126. /package/src/{tzero_fixer → tzero/tzero_fixer}/orchestrator.py +0 -0
  127. /package/src/{tzero_neural_cortex.py → tzero/tzero_neural_cortex.py} +0 -0
@@ -1,184 +1,184 @@
1
- # -*- coding: utf-8 -*-
2
- """
3
- T-ZERO SUB-MICROSECOND MINHASH LSH TRIPLET SIMILARITY ENGINE
4
- Developer: Toprak Ahmet Aydoğmuş (Siber Akademi)
5
- Version: 4.5.0
6
-
7
- Bu modül, en son 2024-2025 LSH (Locality-Sensitive Hashing) ve MinHash
8
- literatüründen esinlenilmiş; harici hiçbir nöral kütüphaneye ihtiyaç duymadan,
9
- anlamsal üçlüler (Semantic Triplets) arasındaki Jaccard benzerliğini
10
- mikrosaniyenin altında (<1 µs / nanosecond-tier) hesaplayan ve yakın-kopyaları
11
- (near-duplicates) anında kümeleyen deterministik hash motorudur.
12
- """
13
-
14
- from __future__ import annotations
15
- import hashlib
16
- from typing import Dict, List, Optional, Set, Tuple, Any
17
- from dataclasses import dataclass, field
18
- from tzero_jarvis.semantic_compressor import SemanticTriplet, TypedEntity, EntityType, SemanticExtractor
19
-
20
-
21
- # Büyük Mersenne Asalı: 2^31 - 1
22
- MERSENNE_PRIME = 2147483647
23
- NUM_PERMUTATIONS = 64
24
- BANDS = 16
25
- ROWS_PER_BAND = 4 # 16 * 4 = 64
26
-
27
-
28
- # Deterministik Hash Katsayıları (a * x + b) % p
29
- def _generate_hash_seeds(k: int = NUM_PERMUTATIONS) -> List[Tuple[int, int]]:
30
- seeds = []
31
- for i in range(k):
32
- # SHA-256 ile tekrarlanabilir deterministik katsayılar
33
- h = hashlib.sha256(f"tzero_seed_{i}".encode("utf-8")).hexdigest()
34
- a = (int(h[:8], 16) % (MERSENNE_PRIME - 1)) + 1
35
- b = int(h[8:16], 16) % MERSENNE_PRIME
36
- seeds.append((a, b))
37
- return seeds
38
-
39
- HASH_SEEDS = _generate_hash_seeds(NUM_PERMUTATIONS)
40
-
41
-
42
- @dataclass
43
- class MinHashSignature:
44
- triplet_code: int
45
- signature: List[int] # 64 integer hashes
46
- label: str
47
- tokens: Set[str] = field(default_factory=set)
48
-
49
- def jaccard_similarity(self, other: MinHashSignature) -> float:
50
- """İki imza arasındaki Jaccard benzerliğini hesaplar (Bitwise/Integer çarpışma oranı)."""
51
- if len(self.signature) != len(other.signature):
52
- return 0.0
53
- matches = sum(1 for a, b in zip(self.signature, other.signature) if a == b)
54
- return matches / float(len(self.signature))
55
-
56
-
57
- class MinHashLSHEngine:
58
- """
59
- Sub-Mikrosaniye MinHash LSH Çizge Benzerlik ve Kümeleme Motoru.
60
- Banding tekniği ile O(1) yakın-komşu araması yapar.
61
- """
62
-
63
- def __init__(self, bands: int = BANDS, rows_per_band: int = ROWS_PER_BAND):
64
- self.bands = bands
65
- self.rows_per_band = rows_per_band
66
- self.num_hashes = bands * rows_per_band
67
- self.signatures: Dict[int, MinHashSignature] = {}
68
- # Band -> BucketHash -> Set of triplet_codes
69
- self.buckets: List[Dict[int, Set[int]]] = [{} for _ in range(self.bands)]
70
-
71
- @staticmethod
72
- def _shingle_triplet(triplet: SemanticTriplet) -> Set[str]:
73
- """Üçlünün bileşenlerinden 1-gram ve 2-gram anlamsal shingle'lar üretir."""
74
- tokens = set()
75
- tokens.add(f"subj:{triplet.subject.lower()}")
76
- tokens.add(f"pred:{triplet.predicate.lower()}")
77
-
78
- for o in triplet.objects:
79
- tokens.add(f"obj:{o.name.lower()}")
80
- tokens.add(f"tag:{o.entity_type.lower()}")
81
- tokens.add(f"so:{triplet.subject.lower()}_{o.name.lower()}")
82
-
83
- for c in triplet.contexts:
84
- tokens.add(f"ctx:{c.name.lower()}")
85
- tokens.add(f"sc:{triplet.subject.lower()}_{c.name.lower()}")
86
-
87
- return tokens
88
-
89
- def compute_signature(self, triplet: SemanticTriplet, code: int) -> MinHashSignature:
90
- """Üçlü için 64 boyutlu kompakt MinHash imzasını hesaplar."""
91
- shingles = self._shingle_triplet(triplet)
92
- sig = [MERSENNE_PRIME] * self.num_hashes
93
-
94
- for token in shingles:
95
- # Token'ın 32-bit integer hash'i
96
- tok_h = int(hashlib.md5(token.encode("utf-8")).hexdigest()[:8], 16)
97
- for i, (a, b) in enumerate(HASH_SEEDS):
98
- perm_val = (a * tok_h + b) % MERSENNE_PRIME
99
- if perm_val < sig[i]:
100
- sig[i] = perm_val
101
-
102
- return MinHashSignature(
103
- triplet_code=code,
104
- signature=sig,
105
- label=triplet.to_graph_string(),
106
- tokens=shingles
107
- )
108
-
109
- def insert(self, triplet: SemanticTriplet, code: int) -> MinHashSignature:
110
- """Üçlüyü LSH tablosuna ekler ve bant kovalarına yerleştirir."""
111
- sig = self.compute_signature(triplet, code)
112
- self.signatures[code] = sig
113
-
114
- # Banding tekniği ile kovalara yaz
115
- for b_idx in range(self.bands):
116
- start = b_idx * self.rows_per_band
117
- end = start + self.rows_per_band
118
- band_slice = tuple(sig.signature[start:end])
119
- bucket_key = hash(band_slice)
120
-
121
- if bucket_key not in self.buckets[b_idx]:
122
- self.buckets[b_idx][bucket_key] = set()
123
- self.buckets[b_idx][bucket_key].add(code)
124
-
125
- return sig
126
-
127
- def query_near_duplicates(self, triplet: SemanticTriplet, threshold: float = 0.5) -> List[Dict[str, Any]]:
128
- """
129
- LSH kovaları üzerinden O(1) hızında adayları çeker ve
130
- Jaccard benzerliği threshold üzerinde olanları döndürür.
131
- """
132
- temp_sig = self.compute_signature(triplet, 0)
133
- candidate_codes: Set[int] = set()
134
-
135
- for b_idx in range(self.bands):
136
- start = b_idx * self.rows_per_band
137
- end = start + self.rows_per_band
138
- band_slice = tuple(temp_sig.signature[start:end])
139
- bucket_key = hash(band_slice)
140
-
141
- if bucket_key in self.buckets[b_idx]:
142
- candidate_codes.update(self.buckets[b_idx][bucket_key])
143
-
144
- results = []
145
- for cand_code in candidate_codes:
146
- cand_sig = self.signatures.get(cand_code)
147
- if cand_sig:
148
- sim = temp_sig.jaccard_similarity(cand_sig)
149
- if sim >= threshold:
150
- results.append({
151
- "code": cand_code,
152
- "similarity": round(sim, 4),
153
- "label": cand_sig.label
154
- })
155
-
156
- results.sort(key=lambda x: x["similarity"], reverse=True)
157
- return results
158
-
159
- def find_clusters(self, min_similarity: float = 0.7) -> List[List[int]]:
160
- """Tüm kayıtlı kodlar arasındaki benzerlik kümelerini (Clusters) çıkarır."""
161
- visited: Set[int] = set()
162
- clusters: List[List[int]] = []
163
-
164
- all_codes = list(self.signatures.keys())
165
- for code in all_codes:
166
- if code in visited:
167
- continue
168
-
169
- sig = self.signatures[code]
170
- cluster = [code]
171
- visited.add(code)
172
-
173
- for other_code in all_codes:
174
- if other_code in visited:
175
- continue
176
- other_sig = self.signatures[other_code]
177
- if sig.jaccard_similarity(other_sig) >= min_similarity:
178
- cluster.append(other_code)
179
- visited.add(other_code)
180
-
181
- if len(cluster) > 1:
182
- clusters.append(cluster)
183
-
184
- return clusters
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ T-ZERO SUB-MICROSECOND MINHASH LSH TRIPLET SIMILARITY ENGINE
4
+ Developer: Toprak Ahmet Aydoğmuş (Siber Akademi)
5
+ Version: 4.5.0
6
+
7
+ Bu modül, en son 2024-2025 LSH (Locality-Sensitive Hashing) ve MinHash
8
+ literatüründen esinlenilmiş; harici hiçbir nöral kütüphaneye ihtiyaç duymadan,
9
+ anlamsal üçlüler (Semantic Triplets) arasındaki Jaccard benzerliğini
10
+ mikrosaniyenin altında (<1 µs / nanosecond-tier) hesaplayan ve yakın-kopyaları
11
+ (near-duplicates) anında kümeleyen deterministik hash motorudur.
12
+ """
13
+
14
+ from __future__ import annotations
15
+ import hashlib
16
+ from typing import Dict, List, Optional, Set, Tuple, Any
17
+ from dataclasses import dataclass, field
18
+ from tzero.tzero_jarvis.semantic_compressor import SemanticTriplet, TypedEntity, EntityType, SemanticExtractor
19
+
20
+
21
+ # Büyük Mersenne Asalı: 2^31 - 1
22
+ MERSENNE_PRIME = 2147483647
23
+ NUM_PERMUTATIONS = 64
24
+ BANDS = 16
25
+ ROWS_PER_BAND = 4 # 16 * 4 = 64
26
+
27
+
28
+ # Deterministik Hash Katsayıları (a * x + b) % p
29
+ def _generate_hash_seeds(k: int = NUM_PERMUTATIONS) -> List[Tuple[int, int]]:
30
+ seeds = []
31
+ for i in range(k):
32
+ # SHA-256 ile tekrarlanabilir deterministik katsayılar
33
+ h = hashlib.sha256(f"tzero_seed_{i}".encode("utf-8")).hexdigest()
34
+ a = (int(h[:8], 16) % (MERSENNE_PRIME - 1)) + 1
35
+ b = int(h[8:16], 16) % MERSENNE_PRIME
36
+ seeds.append((a, b))
37
+ return seeds
38
+
39
+ HASH_SEEDS = _generate_hash_seeds(NUM_PERMUTATIONS)
40
+
41
+
42
+ @dataclass
43
+ class MinHashSignature:
44
+ triplet_code: int
45
+ signature: List[int] # 64 integer hashes
46
+ label: str
47
+ tokens: Set[str] = field(default_factory=set)
48
+
49
+ def jaccard_similarity(self, other: MinHashSignature) -> float:
50
+ """İki imza arasındaki Jaccard benzerliğini hesaplar (Bitwise/Integer çarpışma oranı)."""
51
+ if len(self.signature) != len(other.signature):
52
+ return 0.0
53
+ matches = sum(1 for a, b in zip(self.signature, other.signature) if a == b)
54
+ return matches / float(len(self.signature))
55
+
56
+
57
+ class MinHashLSHEngine:
58
+ """
59
+ Sub-Mikrosaniye MinHash LSH Çizge Benzerlik ve Kümeleme Motoru.
60
+ Banding tekniği ile O(1) yakın-komşu araması yapar.
61
+ """
62
+
63
+ def __init__(self, bands: int = BANDS, rows_per_band: int = ROWS_PER_BAND):
64
+ self.bands = bands
65
+ self.rows_per_band = rows_per_band
66
+ self.num_hashes = bands * rows_per_band
67
+ self.signatures: Dict[int, MinHashSignature] = {}
68
+ # Band -> BucketHash -> Set of triplet_codes
69
+ self.buckets: List[Dict[int, Set[int]]] = [{} for _ in range(self.bands)]
70
+
71
+ @staticmethod
72
+ def _shingle_triplet(triplet: SemanticTriplet) -> Set[str]:
73
+ """Üçlünün bileşenlerinden 1-gram ve 2-gram anlamsal shingle'lar üretir."""
74
+ tokens = set()
75
+ tokens.add(f"subj:{triplet.subject.lower()}")
76
+ tokens.add(f"pred:{triplet.predicate.lower()}")
77
+
78
+ for o in triplet.objects:
79
+ tokens.add(f"obj:{o.name.lower()}")
80
+ tokens.add(f"tag:{o.entity_type.lower()}")
81
+ tokens.add(f"so:{triplet.subject.lower()}_{o.name.lower()}")
82
+
83
+ for c in triplet.contexts:
84
+ tokens.add(f"ctx:{c.name.lower()}")
85
+ tokens.add(f"sc:{triplet.subject.lower()}_{c.name.lower()}")
86
+
87
+ return tokens
88
+
89
+ def compute_signature(self, triplet: SemanticTriplet, code: int) -> MinHashSignature:
90
+ """Üçlü için 64 boyutlu kompakt MinHash imzasını hesaplar."""
91
+ shingles = self._shingle_triplet(triplet)
92
+ sig = [MERSENNE_PRIME] * self.num_hashes
93
+
94
+ for token in shingles:
95
+ # Token'ın 32-bit integer hash'i
96
+ tok_h = int(hashlib.md5(token.encode("utf-8")).hexdigest()[:8], 16)
97
+ for i, (a, b) in enumerate(HASH_SEEDS):
98
+ perm_val = (a * tok_h + b) % MERSENNE_PRIME
99
+ if perm_val < sig[i]:
100
+ sig[i] = perm_val
101
+
102
+ return MinHashSignature(
103
+ triplet_code=code,
104
+ signature=sig,
105
+ label=triplet.to_graph_string(),
106
+ tokens=shingles
107
+ )
108
+
109
+ def insert(self, triplet: SemanticTriplet, code: int) -> MinHashSignature:
110
+ """Üçlüyü LSH tablosuna ekler ve bant kovalarına yerleştirir."""
111
+ sig = self.compute_signature(triplet, code)
112
+ self.signatures[code] = sig
113
+
114
+ # Banding tekniği ile kovalara yaz
115
+ for b_idx in range(self.bands):
116
+ start = b_idx * self.rows_per_band
117
+ end = start + self.rows_per_band
118
+ band_slice = tuple(sig.signature[start:end])
119
+ bucket_key = hash(band_slice)
120
+
121
+ if bucket_key not in self.buckets[b_idx]:
122
+ self.buckets[b_idx][bucket_key] = set()
123
+ self.buckets[b_idx][bucket_key].add(code)
124
+
125
+ return sig
126
+
127
+ def query_near_duplicates(self, triplet: SemanticTriplet, threshold: float = 0.5) -> List[Dict[str, Any]]:
128
+ """
129
+ LSH kovaları üzerinden O(1) hızında adayları çeker ve
130
+ Jaccard benzerliği threshold üzerinde olanları döndürür.
131
+ """
132
+ temp_sig = self.compute_signature(triplet, 0)
133
+ candidate_codes: Set[int] = set()
134
+
135
+ for b_idx in range(self.bands):
136
+ start = b_idx * self.rows_per_band
137
+ end = start + self.rows_per_band
138
+ band_slice = tuple(temp_sig.signature[start:end])
139
+ bucket_key = hash(band_slice)
140
+
141
+ if bucket_key in self.buckets[b_idx]:
142
+ candidate_codes.update(self.buckets[b_idx][bucket_key])
143
+
144
+ results = []
145
+ for cand_code in candidate_codes:
146
+ cand_sig = self.signatures.get(cand_code)
147
+ if cand_sig:
148
+ sim = temp_sig.jaccard_similarity(cand_sig)
149
+ if sim >= threshold:
150
+ results.append({
151
+ "code": cand_code,
152
+ "similarity": round(sim, 4),
153
+ "label": cand_sig.label
154
+ })
155
+
156
+ results.sort(key=lambda x: x["similarity"], reverse=True)
157
+ return results
158
+
159
+ def find_clusters(self, min_similarity: float = 0.7) -> List[List[int]]:
160
+ """Tüm kayıtlı kodlar arasındaki benzerlik kümelerini (Clusters) çıkarır."""
161
+ visited: Set[int] = set()
162
+ clusters: List[List[int]] = []
163
+
164
+ all_codes = list(self.signatures.keys())
165
+ for code in all_codes:
166
+ if code in visited:
167
+ continue
168
+
169
+ sig = self.signatures[code]
170
+ cluster = [code]
171
+ visited.add(code)
172
+
173
+ for other_code in all_codes:
174
+ if other_code in visited:
175
+ continue
176
+ other_sig = self.signatures[other_code]
177
+ if sig.jaccard_similarity(other_sig) >= min_similarity:
178
+ cluster.append(other_code)
179
+ visited.add(other_code)
180
+
181
+ if len(cluster) > 1:
182
+ clusters.append(cluster)
183
+
184
+ return clusters