Ver a proveniência

top-words solution

We'll try rewriting that during next session
master
Dimitri Merejkowsky há 5 anos
ascendente
cometimento
0c0cea4351
1 ficheiros alterados com 14 adições e 5 eliminações
  1. +14
    -5
      sources/06-top-words.py

+ 14
- 5
sources/06-top-words.py Ver ficheiro

@@ -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()