85 lines
2.2 KiB
Python
85 lines
2.2 KiB
Python
"""
|
|
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:
|
|
sum = prob_fr + prob_en + prob_it
|
|
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')
|
|
|
|
|
|
|