File size: 8,900 Bytes
28f510f 3671161 f89d38d 28f510f 5eac832 28f510f 3671161 28f510f 5eac832 28f510f 3671161 28f510f | 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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | import re
import unicodedata
def normalize(text: str) -> str:
text = unicodedata.normalize('NFKD', text)
text = ''.join(c for c in text if not unicodedata.combining(c))
text = text.replace('ɔ', 'o').replace('ɛ', 'e').replace('ɖ', 'd')
return text.lower().strip()
def normalize_masked(text: str) -> str:
"""normalize + masque les nombres (11 chiffres = compteur, 8 = police/téléphone, 3-7 = montant) : le classificateur apprend la forme, pas la valeur."""
t = normalize(text)
t = re.sub(r'\b\d{11}\b', ' numcpt ', t)
t = re.sub(r'\b\d{8}\b', ' numtel ', t)
return re.sub(r'\b\d{3,7}\b', ' nummont ', t)
_ELEC = re.compile(r'sbee|compteur|kompitee|komtee|kontu|comtu|\b\d{11}\b|courant|electricit|hlonhlon|letek')
def mentions_electricity(text: str) -> bool:
"""Règle de l'auteur : un achat d'électricité doit citer SBEE, le compteur (français ou fon), un numéro de compteur,
ou le mot « courant » / « électricité » / « hlɔnhlɔn » (électricité en fon). Sinon, « acheter du crédit » = crédit de communication."""
return bool(_ELEC.search(normalize(text)))
# Multiplicateurs Fongbé
FON_MULT = {
"dokpo": 1, "de": 1,
"we": 2, "aton": 3, "ene": 4, "atoon": 5,
"ayizen": 6, "tenwe": 7, "tanton": 8, "tenne": 9,
"wo": 10,
"afoton": 15, # afɔtɔ́n = 15 en Fongbé
"afaton": 15, # tolérance orthographique
"ko": 20,
"gban": 30,
"kande": 40,
"kandewo": 50,
"kanweko": 100,
"afode": 200
}
# Unités et dizaines en Français
FRENCH_UNITS = {
"un": 1, "une": 1, "deux": 2, "trois": 3, "quatre": 4, "cinq": 5,
"six": 6, "sept": 7, "huit": 8, "neuf": 9, "dix": 10,
"onze": 11, "douze": 12, "treize": 13, "quatorze": 14, "quinze": 15,
"seize": 16, "dix-sept": 17, "dix-huit": 18, "dix-neuf": 19,
"vingt": 20, "trente": 30, "quarante": 40, "cinquante": 50,
"soixante": 60, "soixante-dix": 70, "quatre-vingt": 80, "quatre-vingts": 80,
"quatre-vingt-dix": 90
}
FRENCH_NUMBER_WORDS = {w for k in FRENCH_UNITS for w in k.split('-')} | {"cent", "cents", "vingts", "et"}
def parse_french_mille(clean: str) -> int:
"""Analyse le nombre qui précède 'mille' (cinquante mille, deux cent mille, quatre vingt mille...).
Seuls les mots-nombres CONSÉCUTIFS juste avant « mille » comptent : dans « un jlo na se cinq mille », le « un » du fon
(= « je ») ne doit pas s'ajouter au cinq (bug corrigé : 5000 était lu 6000)."""
m = re.search(r'([a-z\s\-]+?)\s+mille\b', clean)
if not m:
return 1000
tokens = [w for w in re.split(r'[\s\-]+', m.group(0).rsplit('mille', 1)[0].strip()) if w]
words = []
for w in reversed(tokens):
if w in FRENCH_NUMBER_WORDS:
words.append(w)
else:
break
words.reverse()
current, i = 0, 0
while i < len(words):
w = words[i]
if w == "quatre" and i + 1 < len(words) and words[i + 1] in ("vingt", "vingts"):
current += 80; i += 2; continue
if w in ("cent", "cents"):
current = 100 if current == 0 else current * 100
elif w in FRENCH_UNITS:
current += FRENCH_UNITS[w]
i += 1
return (current * 1000) if current > 0 else 1000
def parse_fon_amount(text: str) -> int | None:
"""
Extrait un montant en FCFA depuis une chaîne en Fongbé, en Français ou en chiffres.
Supporte les montants courants et les montants colossaux (jusqu'à plusieurs millions FCFA) :
- Fongbé : 'caki wè' (2000), 'caki ko' (20000), 'caki kande-wǒ' (50000), 'caki kanweko' (100000)
- Français : 'quinze mille' (15000), 'cinquante mille' (50000), 'deux cent mille' (200000)
- Chiffres : '50000', '15000', '20 000', '100.000'
"""
# 0. Normalisation des séparateurs de milliers (ex: 50 000 ou 50.000 -> 50000)
# « wɛ » (avec ɛ) est un démonstratif, pas le chiffre « wè » (2) : sans ce masque, la normalisation (ɛ -> e, accents retirés) les confond
text = re.sub(r'(?<!\w)w\u025b[\u0300-\u036f]*(?!\w)', ' DEMONSTRATIF ', text)
text_clean_num = re.sub(r'(\d+)[ .](\d{3})\b', r'\1\2', text)
clean = normalize(text_clean_num)
# 1. Base Millions
if "million" in clean:
if "deux" in clean: return 2000000
if "trois" in clean: return 3000000
if "cinq" in clean: return 5000000
return 1000000
# 2. Base Caki / Kotokun / Franc Afɔtɔ́n (1 000 FCFA)
if "caki" in clean or "kotokun" in clean or "afoton" in clean or "afaton" in clean:
# Cas où afɔtɔ́n (15) est le multiplicateur de caki (ex: caki afɔtɔ́n = 15 000 FCFA)
if "caki" in clean and ("afoton" in clean or "afaton" in clean):
return 15000
# la ponctuation collée au mot (« caki wo, compteur 142… ») empêchait de reconnaître le multiplicateur,
# et le chiffre qui suivait (numéro de compteur) était alors multiplié par 1000
parts = re.sub(r"[,;:.!?()\"]", " ", clean).replace("franc", "").replace("akwe", "").replace("-", "").split()
base_idx = -1
for i, p in enumerate(parts):
if p in ["caki", "kotokun", "afoton", "afaton"]:
base_idx = i
break
if base_idx != -1:
sub = parts[base_idx + 1:base_idx + 4]
sub_str = ''.join(sub)
if "kandewo" in sub_str or ("kande" in sub and "wo" in sub):
return 50 * 1000
if "konukunatoon" in sub_str or ("ko" in sub and "atoon" in sub):
return 25 * 1000
if "wonukunatoon" in sub_str or ("wo" in sub and "atoon" in sub):
return 15 * 1000
# dizaine + unité (additif, comme « ko atɔ́ɔ́n » = 25) : « caki wǒ atɔn » = 13 000, « caki ko atɔn » = 23 000 (validé par l'auteur).
# « wè » et « ɖokpó » sont exclus des unités.
UNITS = {"aton", "ene", "atoon", "ayizen", "tenwe", "tanton", "tenne"}
TENS = {"wo": 10, "ko": 20, "gban": 30, "kande": 40}
if len(sub) >= 2 and sub[0] in TENS and sub[1] in UNITS:
return (TENS[sub[0]] + FON_MULT[sub[1]]) * 1000
for p in sub:
if p in FON_MULT and p != "de":
return FON_MULT[p] * 1000
# Si chiffres arabes après caki (ex: caki 50)
for p in sub:
if p.isdigit() and len(p) <= 3: # « caki 50 » ; jamais un numéro de téléphone, compteur ou police
return int(p) * 1000
return 1000
# 3. Base Kpowun (25 FCFA) - Système traditionnel de comptage
if "kpowun" in clean:
if "ko nukun ene" in clean: return 24 * 25 # 600 FCFA
if "ko nukun atoon" in clean: return 25 * 25 # 625 FCFA
if "ko atoon" in clean: return 25 * 25 # 625 FCFA (forme validée par le locuteur : kpowun ko atɔ́ɔ́n)
if "ko" in clean: return 20 * 25 # 500 FCFA
parts = clean.replace("franc", "").split()
for p in parts:
if p in FON_MULT and p != "kpowun":
return FON_MULT[p] * 25
return 25
# 4. Base Kantaan (200 FCFA)
if "kantaan" in clean:
parts = clean.replace("franc", "").split()
for p in parts:
if p in FON_MULT and p != "kantaan":
return FON_MULT[p] * 200
return 200
# 5. Expressions françaises avec 'mille'
if "mille" in clean:
return parse_french_mille(clean)
# 6. Chiffres arabes directs (ex: 50000, 15000, 20000, 100000)
# On ignore les numéros de téléphone (8 chiffres) et compteurs (11 chiffres)
for m in re.finditer(r'\b(\d+)\b', text_clean_num):
num_str = m.group(1)
val = int(num_str)
# Montants valides : de 100 FCFA à 5 000 000 FCFA (et pas 8 ou 11 chiffres)
if 100 <= val <= 5000000 and len(num_str) not in [8, 11]:
return val
return None
if __name__ == "__main__":
tests = [
("Xɔ crédit sbee 50000 dó 14253647589 jí", 50000),
("Recharge mon compteur de cinquante mille francs", 50000),
("Xɔ crédit sbee caki ko dó 14253647589 jí", 20000),
("Payer quinze mille pour le courant", 15000),
("Sú facture SONEB cent mille", 100000),
("Sɛ́ deux cent mille à Papa", 200000),
("Acheter crédit SBEE de 15000 sur le 14253647589", 15000),
("Xɔ crédit sbee caki kande-wǒ dó 14253647589 jí", 50000),
("Xɔ crédit sbee caki kanweko dó 14253647589 jí", 100000),
("Paiement facture d'eau 20 000", 20000)
]
for text, expected in tests:
res = parse_fon_amount(text)
print(f"« {text} » -> {res} FCFA (Attendu: {expected})")
assert res == expected, f"ERREUR: {res} != {expected}"
print("\nTOUS LES TESTS DE MONTANTS COLOSSAUX ET COURANTS ONT RÉUSSI !")
|