SHARE
TWEET

g

a guest Apr 21st, 2009 120 Never
  1. from string import letters
  2.  
  3. def frequent_words(filename, n):
  4.     wordlist = filter(len, [filter(lambda chr: chr in letters, word.lower()) for word in open(filename).read().split()])
  5.     counts = dict((word, wordlist.count(word)) for word in wordlist)
  6.     return [(word, counts[word]) for word in sorted(counts, key=lambda word: counts[word], reverse=True)][:n]
RAW Paste Data
Top