Files
ola5doc/content/leon_lenclos/sources/ligne13/ligne13.py
T

55 lines
1.6 KiB
Python
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding : utf8 -*-
# Ligne 13
# Léon Lenclos 2017 (leon.lenclos(à)gmail.com)
# Workshop OLA#5 à Synesthésie
# organisé par Bachir Soussi Chiadmi avec An Mertens
import random
from scipy.stats import norm
import numpy as np
# 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 = random.choice(["v", "r"])
# je retourne le caractere
return char
# je lance le programme
main()