Repository with sources and generator of https://larlet.fr/david/ https://larlet.fr/david/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. title: Nombre d'occurences d'un mot dans un texte en Python
  2. slug: nombre-doccurences-dun-mot-dans-un-texte-en-python
  3. date: 2008-12-06 15:19:20
  4. type: thought
  5. vignette:
  6. contextual_title1: La documentation Django en local (html et pdf)
  7. contextual_url1: 20081204-la-documentation-django-en-local-html-et-pdf
  8. contextual_title2: Interfaces et promotion du Web Sémantique
  9. contextual_url2: 20081203-interfaces-et-promotion-du-web-semantique
  10. contextual_title3: 24ways, le calendrier de l'Avent des geeks web
  11. contextual_url3: 20081201-24ways-le-calendrier-de-lavant-des-geeks-web
  12. Juste un petit snippet car j'en ai eu besoin récemment pour faire des statistiques sur des termes recherchés et je pense que ça peut être utile :
  13. from itertools import groupby
  14. def word_frequencies(content, blacklist):
  15. """
  16. Count the number of words in a content, excluding blacklisted terms.
  17. Return a generator of tuples (count, word) sorted by descending frequency.
  18. Example::
  19. >>> song = 'Ob la di ob la da "rla di da" da "da"'
  20. >>> for count, word in word_frequencies(song, ['di']):
  21. ... print "%s %s" % (count, word)
  22. ...
  23. 4 da
  24. 2 la
  25. 2 ob
  26. 1 rla
  27. """
  28. sorted_words = sorted(word \
  29. for word in content.lower().replace('"', '').split() \
  30. if word not in blacklist)
  31. return ((len(list(group)), word) for word, group in groupby(sorted_words))
  32. if __name__ == "__main__":
  33. import doctest
  34. doctest.testmod(verbose=True)
  35. À adapter selon votre convenance, si vous avez mieux je suis preneur, comme toujours.