import sys, re filename = sys.argv[1] text = file(filename).read() dict = {} text = re.sub('[^\w\s]', '', text) words = re.split('\s', text) for i in range(1, len(words)): for j in range(0, len(words) - i + 1): key = "" for k in range(0, i): key += ' ' + words[j + k] if(key != " "): if(dict.has_key(key)): dict[key] += 1 else: dict[key] = 1 for key in dict.keys(): print '\"' + key + "\"," + str(dict[key]