49 lines
1.5 KiB
Python
49 lines
1.5 KiB
Python
# -*- coding : utf8 -*-
|
|
import random
|
|
from scipy.stats import norm
|
|
import numpy as np
|
|
from colors import bold
|
|
|
|
# Preferences
|
|
noise_duration = 50
|
|
silence_duration = 300
|
|
|
|
# prob_list contiendra la problaité qu'il y ai une altération sur Chaque
|
|
# lettre durant le bruit.
|
|
prob_list_range = np.arange(-2, 2, 4/noise_duration)
|
|
prob_list = [y*2 for y in norm().pdf(prob_list_range)]
|
|
|
|
# fonction principale
|
|
def main():
|
|
# Lire le fichier input.txt et le ranger dans text_in
|
|
with open("input.txt") as f:
|
|
text_in = f.read()
|
|
|
|
# Parcourir le fichier et effectuer le procédé sur chaque carractere
|
|
# en fonction de sa place dans le texte. Écrire le résultat dans text_out
|
|
text_out = ""
|
|
for i, c in enumerate(text_in):
|
|
text_out += process(i, c)
|
|
|
|
# Écrire le contenu de text_out dans le fichier output.txt
|
|
with open("output.txt", "w") as f:
|
|
f.write(text_out)
|
|
|
|
# Le procédé a appliquer sur chaque caractere en fonction de sa position.
|
|
def process(index, char):
|
|
# Je m'assure qu'il y ait un silence au début du texte
|
|
index += silence_duration
|
|
|
|
# Si je suis dans la période de bruit
|
|
if index%(noise_duration+silence_duration) < noise_duration:
|
|
# je tire au sort pour savoir si j'altère le caractere
|
|
if random.random() < prob_list[index%(noise_duration+silence_duration)]:
|
|
# j'altère le caractere avec un "v" ou un "r"
|
|
char = bold(random.choice(["v", "r"]))
|
|
# je retourne le caractere
|
|
return char
|
|
|
|
|
|
# je lance le programme
|
|
main()
|