bokono 4.0.0__py3-none-any.whl
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.
- bokono/__init__.py +14 -0
- bokono/agents/__init__.py +4 -0
- bokono/core/__init__.py +4 -0
- bokono/core/fa_encoder.py +355 -0
- bokono/core/fa_knowledge_public.json +3842 -0
- bokono/core/fa_matrix.py +208 -0
- bokono/ml/__init__.py +4 -0
- bokono/ml/activations.py +676 -0
- bokono/ml/benchmark.py +428 -0
- bokono/ml/layers.py +505 -0
- bokono/ml/network.py +431 -0
- bokono/ml/torch_activations.py +178 -0
- bokono/ml/torch_layers.py +261 -0
- bokono/ml/torch_loss.py +91 -0
- bokono/ml/torch_network.py +692 -0
- bokono/nlp/__init__.py +10 -0
- bokono/nlp/fa_transformer.py +336 -0
- bokono-4.0.0.dist-info/METADATA +118 -0
- bokono-4.0.0.dist-info/RECORD +39 -0
- bokono-4.0.0.dist-info/WHEEL +5 -0
- bokono-4.0.0.dist-info/licenses/LICENSE +34 -0
- bokono-4.0.0.dist-info/top_level.txt +2 -0
- bokono_ml_legacy/__init__.py +4 -0
- bokono_ml_legacy/activations.py +676 -0
- bokono_ml_legacy/benchmark.py +428 -0
- bokono_ml_legacy/benchmark_activations.py +133 -0
- bokono_ml_legacy/dashboard.py +186 -0
- bokono_ml_legacy/demo_agri_fanet.py +105 -0
- bokono_ml_legacy/demo_health_fanet.py +93 -0
- bokono_ml_legacy/demo_interactive.py +210 -0
- bokono_ml_legacy/fa_encoder.py +343 -0
- bokono_ml_legacy/fa_matrix.py +208 -0
- bokono_ml_legacy/layers.py +505 -0
- bokono_ml_legacy/network.py +431 -0
- bokono_ml_legacy/parse_fa.py +213 -0
- bokono_ml_legacy/torch_activations.py +178 -0
- bokono_ml_legacy/torch_layers.py +261 -0
- bokono_ml_legacy/torch_loss.py +91 -0
- bokono_ml_legacy/torch_network.py +618 -0
bokono/__init__.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# Copyright (c) 2026 Cedoque BAGBONON — Projet Matrice-256.
|
|
2
|
+
# Tous droits réservés. Reproduction ou distribution interdite sans autorisation.
|
|
3
|
+
|
|
4
|
+
"""
|
|
5
|
+
bokono — Bibliothèque algorithmique fondée sur la logique binaire du Fâ
|
|
6
|
+
Projet Matrice-256 | Bénin, 2026
|
|
7
|
+
|
|
8
|
+
Le nom "bokono" désigne le praticien du Fâ en langue Fon.
|
|
9
|
+
Cette bibliothèque est le Bokonon numérique du Bénin.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
__version__ = "4.0.0"
|
|
13
|
+
__author__ = "Projet Matrice-256"
|
|
14
|
+
__license__ = "Proprietary Software (c) 2026 Cedoque BAGBONON - Matrice-256"
|
bokono/core/__init__.py
ADDED
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
# Copyright (c) 2026 Cedoque BAGBONON — Projet Matrice-256.
|
|
2
|
+
# Tous droits réservés. Reproduction ou distribution interdite sans autorisation.
|
|
3
|
+
|
|
4
|
+
"""
|
|
5
|
+
bokono.core.fa_encoder — Encodeur Fâ ↔ Binaire
|
|
6
|
+
================================================
|
|
7
|
+
Traducteur universel entre la logique du Fâ et la logique binaire moderne.
|
|
8
|
+
|
|
9
|
+
Le Fâ utilise un système de marques :
|
|
10
|
+
- Marque simple (impaire / ouvert) → 1 (actif)
|
|
11
|
+
- Marque double (paire / fermé) → 0 (passif)
|
|
12
|
+
|
|
13
|
+
Les 16 Kpoli s'encodent sur 4 bits (2⁴ = 16).
|
|
14
|
+
Les 256 du s'encodent sur 8 bits (2⁸ = 256) = 1 octet.
|
|
15
|
+
|
|
16
|
+
Auteur : Projet Matrice-256 / bokono
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
import json
|
|
21
|
+
import os
|
|
22
|
+
from typing import Optional, Union
|
|
23
|
+
try:
|
|
24
|
+
from typing import TypedDict
|
|
25
|
+
except ImportError: # Python < 3.8 fallback (non applicable ici)
|
|
26
|
+
from typing_extensions import TypedDict # type: ignore[no-redef]
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class KpoliEntry(TypedDict):
|
|
30
|
+
"""Structure d'une entrée du référentiel des 16 Kpoli."""
|
|
31
|
+
index: int
|
|
32
|
+
nom: str
|
|
33
|
+
code: str
|
|
34
|
+
alias: list[str]
|
|
35
|
+
|
|
36
|
+
# ─── Référentiel des 16 Kpoli (ordre canonique Fon) ──────────────────────────
|
|
37
|
+
KPOLI_16 = [
|
|
38
|
+
{"index": 0, "nom": "GBE", "code": "0000", "alias": ["GBÉ", "GBHE"]},
|
|
39
|
+
{"index": 1, "nom": "YEKU", "code": "0001", "alias": ["YEKOU", "YÉKU"]},
|
|
40
|
+
{"index": 2, "nom": "WOLI", "code": "0010", "alias": ["WOLI"]},
|
|
41
|
+
{"index": 3, "nom": "DI", "code": "0011", "alias": ["ODI"]},
|
|
42
|
+
{"index": 4, "nom": "ABLA", "code": "0100", "alias": ["ABLAMEDJI"]},
|
|
43
|
+
{"index": 5, "nom": "AKLAN", "code": "0101", "alias": ["AKLAN"]},
|
|
44
|
+
{"index": 6, "nom": "TRINMI", "code": "0110", "alias": ["TRINMI"]},
|
|
45
|
+
{"index": 7, "nom": "GUDA", "code": "0111", "alias": ["GUDA"]},
|
|
46
|
+
{"index": 8, "nom": "SA", "code": "1000", "alias": ["SA"]},
|
|
47
|
+
{"index": 9, "nom": "KA", "code": "1001", "alias": ["KA"]},
|
|
48
|
+
{"index": 10, "nom": "TULA", "code": "1010", "alias": ["TULA"]},
|
|
49
|
+
{"index": 11, "nom": "TURUKPE", "code": "1011", "alias": ["TURUKPE"]},
|
|
50
|
+
{"index": 12, "nom": "KANA", "code": "1100", "alias": ["KANA"]},
|
|
51
|
+
{"index": 13, "nom": "OTURA", "code": "1101", "alias": ["IRETE"]},
|
|
52
|
+
{"index": 14, "nom": "OKANRAN", "code": "1110", "alias": ["OSE"]},
|
|
53
|
+
{"index": 15, "nom": "IKA", "code": "1111", "alias": ["OFUN"]},
|
|
54
|
+
]
|
|
55
|
+
|
|
56
|
+
# Index de recherche rapide
|
|
57
|
+
_KPOLI_BY_NAME: dict[str, int] = {}
|
|
58
|
+
_k: KpoliEntry
|
|
59
|
+
for _k in KPOLI_16: # type: ignore[assignment]
|
|
60
|
+
_KPOLI_BY_NAME[_k["nom"].upper()] = _k["index"]
|
|
61
|
+
for _alias in _k["alias"]:
|
|
62
|
+
_KPOLI_BY_NAME[_alias.upper()] = _k["index"]
|
|
63
|
+
|
|
64
|
+
# Chemins de la base de connaissance (Fallback de sécurité)
|
|
65
|
+
_KB_PRIVATE_PATH = os.path.join(
|
|
66
|
+
os.path.dirname(__file__), "..", "..", "data", "knowledge_base", "fa_knowledge_private.json"
|
|
67
|
+
)
|
|
68
|
+
_KB_PUBLIC_PATH = os.path.join(
|
|
69
|
+
os.path.dirname(__file__), "..", "..", "data", "knowledge_base", "fa_knowledge_public.json"
|
|
70
|
+
)
|
|
71
|
+
_KB_PACKAGE_PATH = os.path.join(
|
|
72
|
+
os.path.dirname(__file__), "fa_knowledge_public.json"
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _load_knowledge_base() -> list[dict]:
|
|
77
|
+
"""Charge la base de connaissance Fâ (lazy loading avec Fallback privé/public/package)."""
|
|
78
|
+
path_priv = os.path.normpath(_KB_PRIVATE_PATH)
|
|
79
|
+
path_pub = os.path.normpath(_KB_PUBLIC_PATH)
|
|
80
|
+
path_pkg = os.path.normpath(_KB_PACKAGE_PATH)
|
|
81
|
+
|
|
82
|
+
# 1. Tente de charger la vraie intelligence (Usage Propriétaire Local)
|
|
83
|
+
if os.path.exists(path_priv):
|
|
84
|
+
try:
|
|
85
|
+
with open(path_priv, encoding="utf-8") as f:
|
|
86
|
+
return json.load(f)
|
|
87
|
+
except json.JSONDecodeError as e:
|
|
88
|
+
raise ValueError(f"Fichier de base de connaissance corrompu (Private) : {e}")
|
|
89
|
+
|
|
90
|
+
# 2. Fallback de sécurité (Usage GitHub / Tiers)
|
|
91
|
+
if os.path.exists(path_pub):
|
|
92
|
+
try:
|
|
93
|
+
with open(path_pub, encoding="utf-8") as f:
|
|
94
|
+
return json.load(f)
|
|
95
|
+
except json.JSONDecodeError as e:
|
|
96
|
+
raise ValueError(f"Fichier de base de connaissance corrompu (Public) : {e}")
|
|
97
|
+
|
|
98
|
+
# 3. Fallback de sécurité package (Usage après pip install)
|
|
99
|
+
if os.path.exists(path_pkg):
|
|
100
|
+
try:
|
|
101
|
+
with open(path_pkg, encoding="utf-8") as f:
|
|
102
|
+
return json.load(f)
|
|
103
|
+
except json.JSONDecodeError as e:
|
|
104
|
+
raise ValueError(f"Fichier de base de connaissance corrompu (Package) : {e}")
|
|
105
|
+
|
|
106
|
+
raise FileNotFoundError(
|
|
107
|
+
f"Base de connaissance introuvable dans data/knowledge_base/ ou dans le package bokono/core/\n"
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
_KB: Optional[list[dict]] = None # Cache en mémoire
|
|
111
|
+
_KB_HASH: Optional[str] = None
|
|
112
|
+
|
|
113
|
+
def _get_kb() -> list[dict]:
|
|
114
|
+
global _KB, _KB_HASH
|
|
115
|
+
if _KB is None:
|
|
116
|
+
import hashlib
|
|
117
|
+
_KB = _load_knowledge_base()
|
|
118
|
+
_KB_HASH = hashlib.sha256(json.dumps(_KB, sort_keys=True).encode("utf-8")).hexdigest()
|
|
119
|
+
return _KB
|
|
120
|
+
|
|
121
|
+
def get_kb_hash() -> str:
|
|
122
|
+
"""Retourne l'empreinte SHA-256 de la base de connaissance active."""
|
|
123
|
+
if _KB_HASH is None:
|
|
124
|
+
_get_kb()
|
|
125
|
+
assert _KB_HASH is not None
|
|
126
|
+
return _KB_HASH
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
# ─── Fonctions principales ────────────────────────────────────────────────────
|
|
130
|
+
|
|
131
|
+
def kpoli_index(nom: str) -> int:
|
|
132
|
+
"""
|
|
133
|
+
Retourne l'index (0-15) d'un Kpoli par son nom.
|
|
134
|
+
|
|
135
|
+
Args:
|
|
136
|
+
nom: Nom du Kpoli (ex: "GBE", "YEKU", "AKLAN")
|
|
137
|
+
|
|
138
|
+
Returns:
|
|
139
|
+
Index 0-15
|
|
140
|
+
|
|
141
|
+
Raises:
|
|
142
|
+
ValueError si le nom est inconnu.
|
|
143
|
+
|
|
144
|
+
Exemple:
|
|
145
|
+
>>> kpoli_index("GBE")
|
|
146
|
+
0
|
|
147
|
+
>>> kpoli_index("YEKU")
|
|
148
|
+
1
|
|
149
|
+
"""
|
|
150
|
+
key = nom.strip().upper()
|
|
151
|
+
if key in _KPOLI_BY_NAME:
|
|
152
|
+
return _KPOLI_BY_NAME[key]
|
|
153
|
+
raise ValueError(
|
|
154
|
+
f"Kpoli inconnu : '{nom}'. "
|
|
155
|
+
f"Valeurs valides : {[k['nom'] for k in KPOLI_16]}"
|
|
156
|
+
)
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def kpoli_name(index: int) -> str:
|
|
160
|
+
"""
|
|
161
|
+
Retourne le nom Fon d'un Kpoli par son index.
|
|
162
|
+
|
|
163
|
+
Args:
|
|
164
|
+
index: Entier 0-15
|
|
165
|
+
|
|
166
|
+
Returns:
|
|
167
|
+
Nom du Kpoli (ex: "GBE")
|
|
168
|
+
|
|
169
|
+
Exemple:
|
|
170
|
+
>>> kpoli_name(0)
|
|
171
|
+
'GBE'
|
|
172
|
+
"""
|
|
173
|
+
if not 0 <= index <= 15:
|
|
174
|
+
raise ValueError(f"Index Kpoli invalide : {index}. Attendu : 0-15.")
|
|
175
|
+
return str(KPOLI_16[index]["nom"]) # cast explicite : TypedDict garantit str
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def encode_du(left: Union[str, int], right: Union[str, int]) -> str:
|
|
179
|
+
"""
|
|
180
|
+
Encode deux Kpoli en un code binaire 8 bits représentant un du.
|
|
181
|
+
|
|
182
|
+
Args:
|
|
183
|
+
left: Kpoli de gauche (nom ou index 0-15)
|
|
184
|
+
right: Kpoli de droite (nom ou index 0-15)
|
|
185
|
+
|
|
186
|
+
Returns:
|
|
187
|
+
Code binaire 8 bits (ex: "00000000")
|
|
188
|
+
|
|
189
|
+
Exemples:
|
|
190
|
+
>>> encode_du("GBE", "GBE")
|
|
191
|
+
'00000000'
|
|
192
|
+
>>> encode_du("GBE", "YEKU")
|
|
193
|
+
'00000001'
|
|
194
|
+
>>> encode_du(1, 0)
|
|
195
|
+
'00010000'
|
|
196
|
+
"""
|
|
197
|
+
li = left if isinstance(left, int) else kpoli_index(left)
|
|
198
|
+
ri = right if isinstance(right, int) else kpoli_index(right)
|
|
199
|
+
if not (0 <= li <= 15 and 0 <= ri <= 15):
|
|
200
|
+
raise ValueError(f"Index hors limites : left={li}, right={ri}")
|
|
201
|
+
return f"{li:04b}{ri:04b}"
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def decode_du(code: Union[str, int]) -> dict:
|
|
205
|
+
"""
|
|
206
|
+
Décode un code binaire (ou entier 0-255) en entrée de la base de connaissance.
|
|
207
|
+
|
|
208
|
+
Args:
|
|
209
|
+
code: Code binaire 8 bits (ex: "00000001") ou entier 0-255
|
|
210
|
+
|
|
211
|
+
Returns:
|
|
212
|
+
Dictionnaire complet du du (depuis fa_knowledge.json)
|
|
213
|
+
|
|
214
|
+
Exemples:
|
|
215
|
+
>>> decode_du("00000000")["nom_fon"]
|
|
216
|
+
'GBE MEDJI'
|
|
217
|
+
>>> decode_du(1)["numero"]
|
|
218
|
+
2
|
|
219
|
+
"""
|
|
220
|
+
if isinstance(code, int):
|
|
221
|
+
idx = code
|
|
222
|
+
else:
|
|
223
|
+
idx = int(code, 2)
|
|
224
|
+
|
|
225
|
+
if not 0 <= idx <= 255:
|
|
226
|
+
raise ValueError(f"Code hors limites : {code}. Attendu : 0-255.")
|
|
227
|
+
|
|
228
|
+
kb = _get_kb()
|
|
229
|
+
return kb[idx]
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
def du_from_kpoli(left: Union[str, int], right: Union[str, int]) -> dict:
|
|
233
|
+
"""
|
|
234
|
+
Retourne le du complet à partir de deux Kpoli.
|
|
235
|
+
|
|
236
|
+
C'est la fonction principale du système : simule le résultat d'un tirage Fâ.
|
|
237
|
+
|
|
238
|
+
Args:
|
|
239
|
+
left: Kpoli de gauche
|
|
240
|
+
right: Kpoli de droite
|
|
241
|
+
|
|
242
|
+
Returns:
|
|
243
|
+
Dictionnaire complet du du
|
|
244
|
+
|
|
245
|
+
Exemple:
|
|
246
|
+
>>> d = du_from_kpoli("GBE", "YEKU")
|
|
247
|
+
>>> print(d["nom_fon"], d["polarite"])
|
|
248
|
+
"""
|
|
249
|
+
code = encode_du(left, right)
|
|
250
|
+
return decode_du(code)
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def value_to_du(value: float, domain: float = 1.0) -> dict:
|
|
254
|
+
"""
|
|
255
|
+
Convertit une valeur numérique (float) en du correspondant.
|
|
256
|
+
|
|
257
|
+
Utilisé par le moteur ML pour "lire" une sortie de réseau de neurones
|
|
258
|
+
à travers la grille de lecture du Fâ.
|
|
259
|
+
|
|
260
|
+
Args:
|
|
261
|
+
value: Valeur numérique (typiquement entre 0.0 et domain)
|
|
262
|
+
domain: Valeur maximale (défaut: 1.0)
|
|
263
|
+
|
|
264
|
+
Returns:
|
|
265
|
+
Dictionnaire du du le plus proche
|
|
266
|
+
|
|
267
|
+
Exemple:
|
|
268
|
+
>>> d = value_to_du(0.5)
|
|
269
|
+
>>> print(d["id"], d["nom_canonique"])
|
|
270
|
+
"""
|
|
271
|
+
normalized = max(0.0, min(1.0, value / domain))
|
|
272
|
+
idx = int(normalized * 255)
|
|
273
|
+
return decode_du(idx)
|
|
274
|
+
|
|
275
|
+
def value_to_algorithmic_state(value: float, domain: float = 1.0) -> dict:
|
|
276
|
+
"""
|
|
277
|
+
Interface ML Externe.
|
|
278
|
+
Convertit la valeur en un état mathématique pur (Ontologie Sécurisée).
|
|
279
|
+
Supprime tous les vocabulaires culturels (Mèdji, Gbé) pour l'API.
|
|
280
|
+
"""
|
|
281
|
+
raw_du = value_to_du(value, domain)
|
|
282
|
+
# Traduction en ontologie algorithmique universelle
|
|
283
|
+
# On garantit l'anonymisation culturelle
|
|
284
|
+
polarite_str = str(raw_du.get("polarite", "neutre")).upper()
|
|
285
|
+
return {
|
|
286
|
+
"state_id": int(raw_du.get("numero", 0)),
|
|
287
|
+
"binary_matrix": f"{raw_du.get('code_binaire', '00000000')[:4]} x {raw_du.get('code_binaire', '00000000')[4:]}",
|
|
288
|
+
"ontological_state": f"STATE_{raw_du.get('numero', 0):03d}",
|
|
289
|
+
"system_polarity": polarite_str,
|
|
290
|
+
"is_primary_node": bool(raw_du.get("est_meji", False))
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
def get_all_meji() -> list[dict]:
|
|
295
|
+
"""Retourne les 16 signes Meji (doubles — les plus puissants du Fâ)."""
|
|
296
|
+
kb = _get_kb()
|
|
297
|
+
return [d for d in kb if d.get("est_meji", False)]
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
def binary_to_marks(code: str) -> str:
|
|
301
|
+
"""
|
|
302
|
+
Convertit un code binaire en représentation visuelle Fâ.
|
|
303
|
+
1 → "|" (marque simple, ouvert)
|
|
304
|
+
0 → "||" (marque double, fermé)
|
|
305
|
+
|
|
306
|
+
Exemple:
|
|
307
|
+
>>> binary_to_marks("00000001")
|
|
308
|
+
'|| || || || || || || |'
|
|
309
|
+
"""
|
|
310
|
+
return " ".join("|" if b == "1" else "||" for b in code)
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
def marks_to_binary(marks: str) -> str:
|
|
314
|
+
"""
|
|
315
|
+
Convertit une représentation visuelle Fâ en code binaire.
|
|
316
|
+
"|" → 1
|
|
317
|
+
"||" → 0
|
|
318
|
+
|
|
319
|
+
Exemple:
|
|
320
|
+
>>> marks_to_binary("|| || || || || || || |")
|
|
321
|
+
'00000001'
|
|
322
|
+
"""
|
|
323
|
+
parts = marks.strip().split()
|
|
324
|
+
result = ""
|
|
325
|
+
i = 0
|
|
326
|
+
while i < len(parts):
|
|
327
|
+
if i + 1 < len(parts) and parts[i] == "|" and parts[i+1] == "|":
|
|
328
|
+
result += "0"
|
|
329
|
+
i += 2
|
|
330
|
+
elif parts[i] == "||":
|
|
331
|
+
result += "0"
|
|
332
|
+
i += 1
|
|
333
|
+
else:
|
|
334
|
+
result += "1"
|
|
335
|
+
i += 1
|
|
336
|
+
return result
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
# ─── Affichage ────────────────────────────────────────────────────────────────
|
|
340
|
+
|
|
341
|
+
def print_du(du: dict, verbose: bool = False) -> None:
|
|
342
|
+
"""Affiche un du de façon lisible."""
|
|
343
|
+
sep = "─" * 60
|
|
344
|
+
print(sep)
|
|
345
|
+
print(f" DU #{du['numero']:3d} | {du['nom_fon']}")
|
|
346
|
+
print(f" Binaire : {du['code_binaire']} → {binary_to_marks(du['code_binaire'])}")
|
|
347
|
+
print(f" Kpoli : {du['kpoli_gauche']} × {du['kpoli_droit']}")
|
|
348
|
+
print(f" Polarité : {du['polarite']}")
|
|
349
|
+
if verbose:
|
|
350
|
+
print(f" Interp : {du['interpretation'][:200]}")
|
|
351
|
+
if du["interdictions"]:
|
|
352
|
+
print(f" Interdit : {'; '.join(du['interdictions'][:3])}")
|
|
353
|
+
if du["prescriptions"]:
|
|
354
|
+
print(f" À faire : {'; '.join(du['prescriptions'][:3])}")
|
|
355
|
+
print(sep)
|