feat: add schetch of function that ables to detect language simply

This commit is contained in:
Namu
2026-05-22 12:30:46 +02:00
parent b5b47feb54
commit a794e12636

View File

@@ -1,8 +1,84 @@
""" """
Ce module contient des fonctions utilisataires Ce module contient des fonctions utilisataires
""" """
from HMM import HMM
def normalize_probabilities(prob_fr: float, prob_en: float, prob_it: float, searched: float) -> float: def normalize_probabilities(prob_fr: float, prob_en: float, prob_it: float, searched: float) -> float:
sum = prob_fr + prob_en + prob_it sum = prob_fr + prob_en + prob_it
return searched / sum return searched / sum
def forward_detection(hmm_fr: HMM, hmm_en: HMM, hmm_it: HMM, O: str):
"""
Prints the language detected with forward method
:param hmm_fr: lambda_fr
:param hmm_en: lambda_en
:param hmm_it: lambda_it
:param O: mot à détecter
:return:
"""
res_fr = hmm_fr.forward(O)
res_en = hmm_en.forward(O)
res_it = hmm_it.forward(O)
proba_fr = normalize_probabilities(res_fr, res_en, res_it, res_fr)
proba_en = normalize_probabilities(res_fr, res_en, res_it, res_en)
proba_it = normalize_probabilities(res_fr, res_en, res_it, res_it)
print(f'FR={proba_fr}, EN={proba_en}, IT={proba_it}')
probas = [proba_fr, proba_en, proba_it]
max_prob = proba_fr
language_index = 0
for index in 1..len(probas):
if max_prob < probas[index]:
max_prob = probas[index]
language_index = index
if language_index == 0:
print('Français')
elif language_index == 1:
print('Anglais')
else:
print('Italien')
def backward_detection(hmm_fr: HMM, hmm_en: HMM, hmm_it: HMM, O: str):
"""
Prints the language detected with backward method
:param hmm_fr: lambda_fr
:param hmm_en: lambda_en
:param hmm_it: lambda_it
:param O: mot à détecter
:return:
"""
res_fr = hmm_fr.backward(O)
res_en = hmm_en.backward(O)
res_it = hmm_it.backward(O)
proba_fr = normalize_probabilities(res_fr, res_en, res_it, res_fr)
proba_en = normalize_probabilities(res_fr, res_en, res_it, res_en)
proba_it = normalize_probabilities(res_fr, res_en, res_it, res_it)
print(f'FR={proba_fr}, EN={proba_en}, IT={proba_it}')
probas = [proba_fr, proba_en, proba_it]
max_prob = proba_fr
language_index = 0
for index in 1..len(probas):
if max_prob < probas[index]:
max_prob = probas[index]
language_index = index
if language_index == 0:
print('Français')
elif language_index == 1:
print('Anglais')
else:
print('Italien')