Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Licence CC BY-NC-ND Thierry Parmentelat & Arnaud Legout

classe WordCounts

on veut calculer la fréquence d’apparition des mots dans un texte
pour cela on vous demande d’écrire une classe qui s’utilise comme ceci

from wordcounts import WordCounts

wc = WordCounts("wordcounts-data.txt")

# on choisit arbitrairement d'afficher les 5 mots les + fréquents
print(wc)
wordcounts-data.txt: 1580 total words570 different words    the : 65
     he : 56
      a : 52
     to : 52
     it : 40
# ensuite on peut chercher le nombre d'occurences comme ceci

for word in ['arthur', 'people']:
    print(f"word {word} was found {wc.counter[word]} times")
word arthur was found 16 times
word people was found 9 times
# et voir si un mot apparait ou pas

for word in ['arthur', 'armageddon']:
    present = word in wc.vocabulary()
    print(f"is word '{word}' present ? : {present} ")
is word 'arthur' present ? : True 
is word 'armageddon' present ? : False 

Indices

variantes

for word in ['arthur', 'people']:
    # here we can index the WordCount instance directly
    print(f"word {word} was found {wc[word]} times")
word arthur was found 16 times
word people was found 9 times

solution