ENSAE Ecole Nationale de la Statistique et de l ... - xavierdupre.fr
ENSAE Ecole Nationale de la Statistique et de l ... - xavierdupre.fr
ENSAE Ecole Nationale de la Statistique et de l ... - xavierdupre.fr
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
2. Séances dirigées 46C<strong>et</strong>te fonction prend en entrée une chaîne <strong>de</strong> caractères <strong>et</strong> r<strong>et</strong>ourne un dictionnaire. Chaque élémentcontiendra le nombre <strong>de</strong> fois qu’un caractère apparaît.3) Afin d’obtenir <strong>de</strong>s probabilités, on divise chaque nombre par le nombre total <strong>de</strong> l<strong>et</strong>tres (attention auxdivisions entières).4) Ecrire une fonction qui r<strong>et</strong>ourne <strong>la</strong> probabilité <strong>de</strong> chaque l<strong>et</strong>tre dans un fichier texte.5) En observant les résultats obtenus sur un texte <strong>fr</strong>ançais <strong>et</strong> un texte ang<strong>la</strong>is, pensez-vous qu’il soitpossible d’écrire une fonction qui détermine automatiquement <strong>la</strong> <strong>la</strong>ngue d’un texte à condition que celle-cisoit l’ang<strong>la</strong>is ou le <strong>fr</strong>ançais.2.4.2 Correction1) La fonction répondant à <strong>la</strong> première question revient régulièrement dans beaucoup <strong>de</strong> programmes.<strong>de</strong>f lit_fichier (nom) :f = open (nom, "r")l = f.readlines ()f.close ()r<strong>et</strong>urn "".join (l)# ouverture du fichier# on récupère toutes les lignes# on ferme le fichier# on r<strong>et</strong>ourne <strong>la</strong> somme <strong>de</strong>s lignesOn pourrait également souhaiter récupérer un texte directement <strong>de</strong>puis Intern<strong>et</strong> à condition d’en connaîtrel’adresse, ce que fait <strong>la</strong> fonction suivante. La première fonction doit recevoir un nom <strong>de</strong> fichier, <strong>la</strong> secon<strong>de</strong>une adresse Intern<strong>et</strong>.import urllib<strong>de</strong>f lit_url (nom) :f = urllib.urlopen (nom)res = f.read ()f.close ()r<strong>et</strong>urn res# import du module urllib# on ouvre l’url# on lit son contenu# on termine <strong>la</strong> lecture# on r<strong>et</strong>ourne le résultatOn peut regrouper ces <strong>de</strong>ux fonctions <strong>et</strong> appeler soit l’une soit l’autre selon que le nom du texte est unfichier texte ou une adresse Intern<strong>et</strong> :<strong>de</strong>f lit (texte) :if "http" in texte : s = lit_url (texte)else : s = lit_fichier (texte)r<strong>et</strong>urn s# Intern<strong>et</strong># fichier textes = lit ("hugo.txt")Une autre option consiste à utiliser les exceptions : on essaye d’abord d’ouvrir un fichier avec <strong>la</strong> fonctionopen. Si ce<strong>la</strong> ne fonctionne pas, on peut supposer que le nom du fichier fait référence à une adresse Intern<strong>et</strong>.<strong>de</strong>f lit (texte) :try :s = lit_fichier (texte)r<strong>et</strong>urn sexcept :s = lit_url (texte)r<strong>et</strong>urn s# fichier texte# si ce<strong>la</strong> ne marche pas,# on suppose que texte# est une adresse Intern<strong>et</strong>s = lit ("hugo.txt")2) La fonction suivante compte les occurrences <strong>de</strong> chaque l<strong>et</strong>tre dans <strong>la</strong> chaîne <strong>de</strong> caractères texte. Unepremière solution consiste à appeler 26 fois <strong>la</strong> métho<strong>de</strong> count <strong>de</strong>s chaînes <strong>de</strong> caractères.