diff --git a/sources/06-top-words.py b/sources/06-top-words.py index d89f4a2..8654a0b 100644 --- a/sources/06-top-words.py +++ b/sources/06-top-words.py @@ -7,10 +7,10 @@ def get_value(pair): def get_word(chunk): - if all(x.isalpha() for x in chunk): + if all(x.isalpha() for x in chunk): # is_alpha() if len(chunk) < 4: return None - return chunk.lower() + return chunk.lower() # lower() else: return None @@ -18,19 +18,28 @@ def get_word(chunk): def main(): filename = sys.argv[1] file = open(filename, "r") + lines = file.readlines() + file.close() scores = {} - for line in file.readlines(): + for line in lines: for chunk in line.split(): word = get_word(chunk) if word: if not word in scores: - scores[word] = 0 + scores[word] = 1 else: scores[word] += 1 - file.close() + to_sort = [] + for k in scores: # iterate on dicts + v = scores[k] + to_sort.append([v, k]) + to_sort.sort() + + + print(to_sort[-10:]) main()