From 3258373bb4862f6dc7588d261c8d3fd767618640 Mon Sep 17 00:00:00 2001 From: E214194U <alexandreclenet@free.fr> Date: Thu, 28 Apr 2022 23:08:21 +0200 Subject: [PATCH] maj --- Stat/TP3/Exercice1_corpus/c.py | 38 ++++----- .../freq_lettre_fr_approx.json | 2 +- Stat/TP3/Exercice2_Caesar/c.py | 78 ++++++++++++++++--- .../Exercice2_Caesar/crypted_with_caesar.txt | 1 + Stat/TP3/Exercice2_Caesar/freq1.json | 1 + Stat/TP3/Exercice2_Caesar/freq2.json | 1 + 6 files changed, 91 insertions(+), 30 deletions(-) create mode 100644 Stat/TP3/Exercice2_Caesar/crypted_with_caesar.txt create mode 100644 Stat/TP3/Exercice2_Caesar/freq1.json create mode 100644 Stat/TP3/Exercice2_Caesar/freq2.json diff --git a/Stat/TP3/Exercice1_corpus/c.py b/Stat/TP3/Exercice1_corpus/c.py index 4e30fba..202c8f8 100644 --- a/Stat/TP3/Exercice1_corpus/c.py +++ b/Stat/TP3/Exercice1_corpus/c.py @@ -1,24 +1,26 @@ -# On importe la librairie json. + import json import unidecode -new_file = open("freq_lettre_fr_approx.json","w") - -Alphabet=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'] - -dictionnary ={} - -with open("Corpus/amourSwann.txt", "r") as txt: - lignes = unidecode.unidecode(txt.read().lower()) - print(lignes) - for i in range(0,26): - freqlettre=lignes.count(Alphabet[i])/len(lignes)*100 - dictionnary[Alphabet[i]]=freqlettre - - -json.dump(dictionnary,new_file) -# On ferme le fichier. -new_file.close() +def filetofreq(txt,fichier): + new_file = open(fichier,"w") + dictionnary ={} + Alphabet=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',' '] + with open(txt, "r",encoding="UTF-8") as txt: + lignes = unidecode.unidecode(txt.read().lower()) + print(lignes) + for i in range(0,26): + freqlettre=lignes.count(Alphabet[i])/(len(lignes)-lignes.count(Alphabet[26]))*100 + dictionnary[Alphabet[i]]=freqlettre + + json.dump(dictionnary,new_file) + new_file.close() + + +def main(): + filetofreq("Corpus/amourSwann.txt","freq_lettre_fr_approx.json") + +main() diff --git a/Stat/TP3/Exercice1_corpus/freq_lettre_fr_approx.json b/Stat/TP3/Exercice1_corpus/freq_lettre_fr_approx.json index 5d58a0a..03f8ebb 100644 --- a/Stat/TP3/Exercice1_corpus/freq_lettre_fr_approx.json +++ b/Stat/TP3/Exercice1_corpus/freq_lettre_fr_approx.json @@ -1 +1 @@ -{"a": 6.913184696814113, "b": 0.5162908151797873, "c": 2.3090957969564623, "d": 2.861304411058275, "e": 13.402847862780787, "f": 0.7337808000634621, "g": 0.5248846443697197, "h": 0.5934149232945657, "i": 6.320651191898002, "j": 0.321938062730546, "k": 0.0013221275676819136, "l": 4.548118832825783, "m": 2.1094545342364937, "n": 5.420723027495846, "o": 3.7729113689749543, "p": 2.1751202034313617, "q": 1.2328839568633847, "r": 4.926247317182811, "s": 5.735389388604141, "t": 5.943183771324816, "u": 5.32861480694734, "v": 1.4468482682332409, "w": 0.11370297082064458, "x": 0.26376444975254176, "y": 0.2042687092068557, "z": 0.1322127567681914} \ No newline at end of file +{"a": 8.210791637974717, "b": 0.6381955903179285, "c": 2.8461548455831718, "d": 3.4108124261046946, "e": 15.91149453661959, "f": 0.9032442475346576, "g": 0.635077370821261, "h": 0.706536567619889, "i": 7.528421271454, "j": 0.4157625995556537, "k": 0.0015591097483337016, "l": 5.410370678342667, "m": 2.6728338118934087, "n": 6.4087206205256795, "o": 4.592097912092195, "p": 2.6216430418231194, "q": 1.4808944092922942, "r": 5.821196097028597, "s": 6.9432354125794165, "t": 7.032104668234439, "u": 6.293086647524264, "v": 1.8083074564423713, "w": 0.13564254810503204, "x": 0.31182194966674026, "y": 0.24088245611755688, "z": 0.15695038133225928} \ No newline at end of file diff --git a/Stat/TP3/Exercice2_Caesar/c.py b/Stat/TP3/Exercice2_Caesar/c.py index 3fca9e0..7867d93 100644 --- a/Stat/TP3/Exercice2_Caesar/c.py +++ b/Stat/TP3/Exercice2_Caesar/c.py @@ -1,6 +1,10 @@ import string -from sympy import false, true - +from turtle import width +import pandas as pd +import matplotlib.pyplot as plt +import numpy as np +import json +import unidecode alphabet=string.ascii_lowercase ALPHABET=string.ascii_uppercase @@ -8,8 +12,8 @@ ALPHABET=string.ascii_uppercase def is_upper(x): for i in ALPHABET: if x==i: - return true - return false + return True + return False def caesar_crypt(msg,decal): @@ -27,16 +31,68 @@ def caesar_crypt(msg,decal): msg_crypt+=new_alphabet[y] return msg_crypt -with open("encrypted_with_caesar.txt","r") as txt: - str=txt.read() - print(str,"\n") - print(caesar_crypt(str, 3)) +def txtcryptjson(fichier,decal): + with open(fichier,"r",encoding="UTF-8") as txt: + new_file = open("crypted_with_caesar.txt","w") + str=txt.read() + strcry=caesar_crypt(str, decal) + new_file.write(strcry) + new_file.close() + filetofreq("crypted_with_caesar.txt","freq2.json") + +def filetofreq(text,fichier): + new_file = open(fichier,"w") + dictionnary ={} + Alphabet=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',' '] + with open(text, "r",encoding="UTF-8") as txt: + lignes = unidecode.unidecode(txt.read().lower()) + for i in range(0,26): + freqlettre=lignes.count(Alphabet[i])/(len(lignes)-lignes.count(Alphabet[26]))*100 + dictionnary[Alphabet[i]]=freqlettre + + json.dump(dictionnary,new_file) + new_file.close() def bar_freq(fichier): - return* + dictionary = json.load(open(fichier, 'r')) + xAxis = [key for key, value in dictionary.items()] + yAxis = [value for key, value in dictionary.items()] + + fig, ax = plt.subplots() + plt.xticks(rotation=35,ha="right") + ax.bar(xAxis,yAxis) + plt.tight_layout() + ax.set_title("Fréquence d'apparition d'une lettre") + plt.show() + def bar_freq1_freq2(fichier1,fichier2): - return + dictionary = json.load(open(fichier1, 'r')) + xAxis = [key for key, value in dictionary.items()] + yAxis = [value for key, value in dictionary.items()] + dictionary2 = json.load(open(fichier2, 'r')) + xAxis2 = [key for key,value in dictionary2.items()] + yAxis2 = [value for key, value in dictionary2.items()] + + fig, ax = plt.subplots() + plt.xticks(rotation=35,ha="right") + ax.bar(np.arange(len(xAxis))+0.2,yAxis,0.4) + ax.bar(np.arange(len(xAxis2))-0.2,yAxis2,0.4) + plt.tight_layout() + ax.set_title("Fréquence d'apparition d'une lettre") + plt.show() def bar_compare_freq(fichier1,fichier2,decal): - return \ No newline at end of file + txtcryptjson(fichier2,decal) + bar_freq1_freq2(fichier1,fichier2) + +def main(): + filetofreq("encrypted_with_caesar.txt","freq1.json") + txtcryptjson("encrypted_with_caesar.txt",3) + + #bar_freq("freq1.json") + #bar_freq1_freq2("freq1.json","freq2.json") + bar_compare_freq("freq1.json","freq2.json",0) + + +main() \ No newline at end of file diff --git a/Stat/TP3/Exercice2_Caesar/crypted_with_caesar.txt b/Stat/TP3/Exercice2_Caesar/crypted_with_caesar.txt new file mode 100644 index 0000000..e85d5b4 --- /dev/null +++ b/Stat/TP3/Exercice2_Caesar/crypted_with_caesar.txt @@ -0,0 +1 @@ +abcdefghijklmnopqrstuvwxyz \ No newline at end of file diff --git a/Stat/TP3/Exercice2_Caesar/freq1.json b/Stat/TP3/Exercice2_Caesar/freq1.json new file mode 100644 index 0000000..afc377c --- /dev/null +++ b/Stat/TP3/Exercice2_Caesar/freq1.json @@ -0,0 +1 @@ +{"a": 0.06353240152477764, "b": 0.0, "c": 5.082592121982211, "d": 3.1130876747141043, "e": 7.306226175349428, "f": 4.193138500635324, "g": 2.6048284625158833, "h": 1.1435832274459974, "i": 5.336721728081321, "j": 8.259212198221093, "k": 7.814485387547649, "l": 6.289707750952986, "m": 0.3811944091486658, "n": 0.0, "o": 0.3176620076238882, "p": 0.44472681067344344, "q": 0.06353240152477764, "r": 7.496823379923762, "s": 1.7153748411689964, "t": 4.701397712833545, "u": 3.6213468869123253, "v": 16.96315120711563, "w": 2.096569250317662, "x": 1.2706480304955527, "y": 2.096569250317662, "z": 7.6238881829733165} \ No newline at end of file diff --git a/Stat/TP3/Exercice2_Caesar/freq2.json b/Stat/TP3/Exercice2_Caesar/freq2.json new file mode 100644 index 0000000..08cc651 --- /dev/null +++ b/Stat/TP3/Exercice2_Caesar/freq2.json @@ -0,0 +1 @@ +{"a": 3.8461538461538463, "b": 3.8461538461538463, "c": 3.8461538461538463, "d": 3.8461538461538463, "e": 3.8461538461538463, "f": 3.8461538461538463, "g": 3.8461538461538463, "h": 3.8461538461538463, "i": 3.8461538461538463, "j": 3.8461538461538463, "k": 3.8461538461538463, "l": 3.8461538461538463, "m": 3.8461538461538463, "n": 3.8461538461538463, "o": 3.8461538461538463, "p": 3.8461538461538463, "q": 3.8461538461538463, "r": 3.8461538461538463, "s": 3.8461538461538463, "t": 3.8461538461538463, "u": 3.8461538461538463, "v": 3.8461538461538463, "w": 3.8461538461538463, "x": 3.8461538461538463, "y": 3.8461538461538463, "z": 3.8461538461538463} \ No newline at end of file -- GitLab