All pastes #634915 Raw Edit

Someone

public text v1 · immutable
#634915 ·published 2007-07-26 19:04 UTC
rendered paste body
def nMostCommonWords(n, file):
        """ returns a list of the n most common words """
        contents        = open(file).read()
        words           = re.findall('\w+', contents, re.M)
        words           = [x.lower() for x in words]
        wordsDict       = dict()
        for word in words:
                if word not in wordsDict:
                        wordsDict[word] = 1
                else:
                        wordsDict[word] += 1
        wordCount = wordsDict.items()
        wordCount.sort(cmp = lambda x,y: y[1] - x[1])
        return wordCount[0:n]