Mathématiques en première générale – Algorithmes

Fréquence des lettres d'un texte

Fréquence d'apparition des lettres d'un texte donné, en français, en anglais
def frequences(alphabet, fichier) :
    dict = {l: 0 for l in alphabet}
    with open(fichier, "r") as f :
        while True :
            c = f.read(1)
            if not c :
                break
            m = c.lower()
            if m in dict :
                dict[m] += 1
        total = sum(dict.values())
        return dict, total

def run(alphabet, fichier) :
    dict, total = frequences(alphabet, fichier)
    for l in sorted(dict) :
        print("{} {:7} {:6.2%}".format(l, dict[l], dict[l] / total))
    print(total)

alphabet_francais = "abcdefghijklmnopqrstuvwxyzàâæçèéêëîïôùûüœ"
alphabet_anglais  = "abcdefghijklmnopqrstuvwxyz"

run(alphabet_francais, "du_cote_de_chez_swann.txt")
run(alphabet_anglais, "1984.txt")
a   64466  8.20%
b    6213  0.79%
c   23212  2.95%
d   28775  3.66%
e  116548 14.82%
f    7741  0.98%
g    6145  0.78%
h    6022  0.77%
i   60242  7.66%
j    4508  0.57%
k      24  0.00%
l   44385  5.64%
m   24525  3.12%
n   54800  6.97%
o   38734  4.92%
p   22061  2.80%
q   12145  1.54%
r   49461  6.29%
s   60771  7.73%
t   56481  7.18%
u   50996  6.48%
v   14206  1.81%
w     711  0.09%
x    2645  0.34%
y    2131  0.27%
z     998  0.13%
à    4441  0.56%
â     550  0.07%
æ       0  0.00%
ç     720  0.09%
è    2284  0.29%
é   14855  1.89%
ê    2118  0.27%
ë       8  0.00%
î     578  0.07%
ï      57  0.01%
ô     428  0.05%
ù     759  0.10%
û     629  0.08%
ü       1  0.00%
œ     262  0.03%
786636
a   36457  7.88%
b    7631  1.65%
c   11601  2.51%
d   18999  4.10%
e   59515 12.86%
f   10173  2.20%
g    9250  2.00%
h   29137  6.29%
i   31877  6.89%
j     457  0.10%
k    3602  0.78%
l   18620  4.02%
m   10814  2.34%
n   31931  6.90%
o   34979  7.56%
p    8591  1.86%
q     409  0.09%
r   26062  5.63%
s   28937  6.25%
t   43800  9.46%
u   12999  2.81%
v    4306  0.93%
w   12231  2.64%
x     791  0.17%
y    9406  2.03%
z     306  0.07%
462881
Lionel Avon