quellelangue.py

Created by schraf

Created on December 31, 2020

3.14 KB

Explications en vidéo


from math import sqrt

LANGUES = ['FR','ANG','ALL','ESP','POR','ITA']
EFF = [[763,90,326,366,1471,106,86,73,752,54,4,545,296,709,537,302,136,655,794,724,631,162,11,38,30,13],[816,149,278,425,1270,222,201,609,696,15,77,402,240,674,750,192,9,598,632,905,275,97,236,15,197,7],[651,189,306,508,1740,166,301,476,755,27,121,344,253,978,251,79,2,700,727,615,435,67,189,3,4,113],[1253,142,468,586,1368,69,101,70,625,44,1,497,315,671,868,251,88,687,798,463,393,90,2,22,90,52],[1463,104,388,499,1257,102,130,128,618,40,2,278,474,505,1073,252,120,653,781,474,463,167,1,21,1,47],[1174,92,450,373,1179,95,164,154,1128,0,0,651,251,688,983,305,51,637,498,562,301,210,0,0,0,49]]

def analyse(extrait):
 txt = extrait.upper()
 ob = [0] * 26
 for c in txt:
  ascii = ord(c)
  if 65 <= ascii <= 90:
   ob[ascii - 65] += 1
 return ob

def distance(e1,e2):
 s1 = sum(e1)
 s2 = sum(e2)
 d = 0
 for i in range(26):
  d += (e1[i]/s1 - e2[i]/s2) ** 2
 return sqrt(d)

def fluctu(p,n):
 e = 1.96 * sqrt(p * (1 - p) / n)
 return [p - e, p + e]
 
def interfluctu(e1,e2):
 s1 = sum(e1)
 s2 = sum(e2)
 ok = 0
 for i in range(26):
  bornes = fluctu(e2[i]/s2,s1)
  if bornes[0] <= e1[i]/s1 <= bornes[1]:
      ok += 1
 return int(100 * ok / 26) 
 
def langue(extrait):
 e = analyse(extrait)
 for k,v in enumerate(EFF):
  print(LANGUES[k],distance(e,v))
  print(interfluctu(e,v), '% lettres OK')
 return "= FIN ANALYSE ="
 
extrait1 = "Si je vous ai raconte ces details sur l'asteroide B 612 et si je vous ai confie son numero, c'est a cause des grandes personnes. Les grandes personnes aiment les chiffres. Quand vous leur parlez d'un nouvel ami, elles ne vous questionnent jamais sur l'essentiel. Elles ne vous disent jamais: Quel est le son de sa voix ? Quels sont les jeux qu'il prefere ? Est-ce qu'il collectionne les papillons ? Elles vous demandent: Quel age a-t-il ? Combien a-t-il de freres ? Combien pese-t-il ? Combien gagne son pere ? Alors seulement elles croient le connaitre. Si vous dites aux grandes personnes : J'ai vu une belle maison en briques roses, avec des geraniums aux fenetres et des colombes sur le toit...Elles ne parviennent pas a s'imaginer cette maison. Il faut leur dire :J'ai vu une maison de cent mille francs. Alors elles s'ecrient : Comme c'est joli !"
extrait2 = "If I have told you these details about the asteroid, and made a note of its number for you, it is on account of the grown-ups and their ways. When you tell them that you have made a new friend, they never ask you any questions about essential matters. They never say to you, What does his voice sound like? What games does he love best? Does he collect butterflies? Instead, they demand: How old is he? How many brothers has he? How much does he weigh? How much money does his father make? Only from these figures do they think they have learned anything about him.If you were to say to the grown-ups: I saw a beautiful house made of rosy brick, with geraniums in the windows and doves on the roof, they would not be able to get any idea of that house at all. You would have to say to them: I saw a house that cost $20,000. Then they would exclaim: Oh, what a pretty house that is!"

print(langue(extrait1))
print(langue(extrait2))