Browse Source

top-words solution

We'll try rewriting that during next session
master
Dimitri Merejkowsky 5 years ago
parent
commit
0c0cea4351
1 changed files with 14 additions and 5 deletions
  1. +14
    -5
      sources/06-top-words.py

+ 14
- 5
sources/06-top-words.py View File

@@ -7,10 +7,10 @@ def get_value(pair):




def get_word(chunk): 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: if len(chunk) < 4:
return None return None
return chunk.lower()
return chunk.lower() # lower()
else: else:
return None return None


@@ -18,19 +18,28 @@ def get_word(chunk):
def main(): def main():
filename = sys.argv[1] filename = sys.argv[1]
file = open(filename, "r") file = open(filename, "r")
lines = file.readlines()
file.close()


scores = {} scores = {}


for line in file.readlines():
for line in lines:
for chunk in line.split(): for chunk in line.split():
word = get_word(chunk) word = get_word(chunk)
if word: if word:
if not word in scores: if not word in scores:
scores[word] = 0
scores[word] = 1
else: else:
scores[word] += 1 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() main()