
g
By: a guest on
Apr 21st, 2009 | syntax:
Python | size: 0.36 KB | hits: 113 | expires: Never
from string import letters
def frequent_words(filename, n):
wordlist = filter(len, [filter(lambda chr: chr in letters, word.lower()) for word in open(filename).read().split()])
counts = dict((word, wordlist.count(word)) for word in wordlist)
return [(word, counts[word]) for word in sorted(counts, key=lambda word: counts[word], reverse=True)][:n]