Feat: Adds the HMM and detection for one word
This commit is contained in:
89
data_preparation/__init__.py
Normal file
89
data_preparation/__init__.py
Normal file
@@ -0,0 +1,89 @@
|
||||
"""
|
||||
Ce module contient les fonctions pour préparer les données.
|
||||
|
||||
Cela consiste à:
|
||||
- Lire un fichier
|
||||
- Nettoyer les données
|
||||
- Tout transformer en dataframe d'index de l'alphabet.
|
||||
"""
|
||||
import re
|
||||
|
||||
|
||||
def read_file(file_name: str) -> str:
|
||||
"""
|
||||
Lis le fichier sans rien touché. Retourne le texte brut
|
||||
:param file_name:
|
||||
:return:
|
||||
"""
|
||||
with open(file_name) as file:
|
||||
return file.read()
|
||||
|
||||
|
||||
def parse_data(raw_data: str) -> str:
|
||||
"""
|
||||
Cette fonction retire les caractères spéciaux et passe toutes les lettres
|
||||
en minuscule
|
||||
:param raw_data:
|
||||
:return:
|
||||
"""
|
||||
lower_raw_data = raw_data.lower()
|
||||
without_special_chars = re.sub(r'[^a-z]', ' ', lower_raw_data)
|
||||
return without_special_chars
|
||||
|
||||
|
||||
def prepare_file(file_name) -> str:
|
||||
"""
|
||||
Prépare le fichier en le lisant et en le parsant
|
||||
:param file_name:
|
||||
:return:
|
||||
"""
|
||||
raw_data = read_file(file_name)
|
||||
return parse_data(raw_data)
|
||||
|
||||
|
||||
def get_alphabet_index_of(letter: str) -> int:
|
||||
"""
|
||||
Retourne l'index dans l'alphabet d'une lettre en imaginant que
|
||||
l'alphabet est un tableau.
|
||||
|
||||
(ex: a -> 0)
|
||||
:param letter:
|
||||
:return:
|
||||
"""
|
||||
# l'alphabet et l'ensemble des états
|
||||
return 'abcdefghijklmnopqrstuvwxyz'.find(letter)
|
||||
|
||||
|
||||
def get_alphabet_index_form_word(word: str) -> list[int]:
|
||||
"""
|
||||
Retourne un mot sous forme d'ensemble d'index dans l'alphabet
|
||||
:param word:
|
||||
:return:
|
||||
"""
|
||||
return [get_alphabet_index_of(letter) for letter in word]
|
||||
|
||||
|
||||
def get_text_in_alphabet_index_form(text: str) -> list[list[int]]:
|
||||
"""
|
||||
Prends un texte et le transforme un matrice contenant tout les mots sous forme de tableau d'entier.
|
||||
Chaque entier correspond à l'index du caractère dans l'alphabet
|
||||
:param text:
|
||||
:return:
|
||||
"""
|
||||
words = text.split(' ')
|
||||
numeric_text = []
|
||||
for word in words:
|
||||
if word: # On ignore les espaces multiples
|
||||
numeric_text.append(get_alphabet_index_form_word(word))
|
||||
return numeric_text
|
||||
|
||||
|
||||
def prepare_data(file_name: str) -> list[list[int]]:
|
||||
"""
|
||||
Cette fonction lis le fichier, nettoie les données puis convertie tout en index alphabétique.
|
||||
:param file_name:
|
||||
:return:
|
||||
"""
|
||||
content = prepare_file(file_name)
|
||||
return get_text_in_alphabet_index_form(content)
|
||||
|
||||
BIN
data_preparation/__pycache__/__init__.cpython-314.pyc
Normal file
BIN
data_preparation/__pycache__/__init__.cpython-314.pyc
Normal file
Binary file not shown.
Reference in New Issue
Block a user