Advertisement
Clem585

Untitled

Jul 16th, 2021
1,219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.35 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. #####
  4. # Clément Hamel-Doyon - 18099246
  5. ###
  6.  
  7. from pdb import set_trace as dbg  # Utiliser dbg() pour faire un break dans votre code.
  8. import numpy as np
  9. import math
  10.  
  11. class Correcteur:
  12.     def __init__(self, p_init, p_transition, p_observation, int2letters, letters2int):
  13.         '''Correcteur de frappes dans un mot.
  14.  
  15.        Modèle de Markov caché (HMM) permettant de corriger les erreurs de frappes
  16.        dans un mot. La correction est dictée par l'inférence de l'explication
  17.        la plus pausible du modèle.
  18.  
  19.        Parameters
  20.        ------------
  21.        p_init : array-like shape (N,)
  22.                 Probabilités initiales de l'état caché à la première position.
  23.  
  24.        p_transition : array-like shape (X,Y)
  25.                       Modèle de transition.
  26.  
  27.        p_observation : array-like shape (X,Y)
  28.                        Modèle d'observation.
  29.  
  30.        int2letters : list
  31.                      Associe un entier (l'indice) à une lettre.
  32.  
  33.        letters2int : dict
  34.                      Associe une lettre (clé) à un entier (valeur).
  35.        '''
  36.         self.p_init = p_init
  37.         self.p_transition = p_transition
  38.         self.p_observation = p_observation
  39.         self.int2letters = int2letters
  40.         self.letters2int = letters2int
  41.  
  42.     def corrige(self, mot):
  43.         '''Corrige les frappes dans un mot.
  44.  
  45.        Retourne la correction du mot donné et la probabilité p(mot, mot corrigé).
  46.  
  47.        Parameters
  48.        ------------
  49.        mot : string
  50.              Mot à corriger.
  51.  
  52.        Returns
  53.        -----------
  54.        mot_corrige : string
  55.                      Le mot corrigé.
  56.  
  57.        prob : float
  58.               Probabilité dans le HMM du mot observé et du mot corrigé.
  59.               C'est-à-dire 'p(mot, mot_corrige)'.
  60.        '''
  61.  
  62.         a = np.empty([26, len(mot)])
  63.         for t in range(len(mot)):
  64.             for i in range(26):
  65.                 if t == 0:
  66.                     a[i, t] = self.p_init[i] * self.p_observation[self.letters2int[mot[t]], i]
  67.                 else:
  68.                     a[i, t] = self.p_observation[self.letters2int[mot[t]], i] * self.sumPrecedence(a, t, i)
  69.                 #print("a[", i, ",", t, "]=", a[i, t])
  70.  
  71.         mot_corrige = ""
  72.         total_prob = 1.0
  73.         for t in range(len(mot)):
  74.             current_prob = 0
  75.             current_letter = None
  76.             for i in range(26):
  77.                 prob = a[i, t]# / self.sumProb(a, t)
  78.                 if prob > current_prob:
  79.                     #if current_letter is not None:
  80.                     #    print("#", t, ": ", self.int2letters[i], "[", prob, "] VS ", self.int2letters[current_letter], "[", current_prob,"]")
  81.                     current_prob = prob
  82.                     current_letter = i
  83.             mot_corrige += self.int2letters[current_letter]
  84.             total_prob *= a[current_letter, t]
  85.  
  86.         # Retourne le mot sans correction avec une probabilité de 0.0 (.~= À MODIFIER =~.)
  87.         return mot_corrige, total_prob
  88.  
  89.     def sumProb(self, a, t_index):
  90.         sum = 0.0
  91.         for i in range(26):
  92.             sum += a[i, t_index]
  93.         return sum
  94.  
  95.     def sumPrecedence(self, a, t_index, i_index):
  96.         sum = 0.0
  97.         for i in range(26):
  98.             sum += (self.p_transition[i_index, i] * a[i, (t_index - 1)])
  99.         return sum
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement