ligne13~20171217-114348.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # -*- coding : utf8 -*-
  2. import random
  3. from scipy.stats import norm
  4. import numpy as np
  5. # Preferences
  6. noise_duration = 200
  7. silence_duration = 300
  8. # prob_list contiendra la problaité qu'il y ai une altération sur Chaque
  9. # lettre durant le bruit.
  10. prob_list_range = np.arange(-2, 2, 4/noise_duration)
  11. prob_list = [y*2 for y in norm().pdf(prob_list_range)]
  12. # fonction principale
  13. def main():
  14. # Lire le fichier input.txt et le ranger dans text_in
  15. with open("input.txt") as f:
  16. text_in = f.read()
  17. # Parcourir le fichier et effectuer le procédé sur chaque carractere
  18. # en fonction de sa place dans le texte. Écrire le résultat dans text_out
  19. text_out = ""
  20. for i, c in enumerate(text_in):
  21. text_out += process(i, c)
  22. # Écrire le contenu de text_out dans le fichier output.txt
  23. with open("output.txt", "w") as f:
  24. f.write(text_out)
  25. # Le procédé a appliquer sur chaque caractere en fonction de sa position.
  26. def process(index, char):
  27. # Je m'assure qu'il y ait un silence au début du texte
  28. index += silence_duration
  29. # Si je suis dans la période de bruit
  30. if index%(noise_duration+silence_duration) < noise_duration:
  31. # je tire au sort pour savoir si j'altère le caractere
  32. if random.random() < prob_list[index%(noise_duration+silence_duration)]:
  33. # j'altère le caractere avec un "v" ou un "r"
  34. char = random.choice(["v", "r"])
  35. # je retourne le caractere
  36. return char
  37. # je lance le programme
  38. main()