Skip to content

Commit 2218028

Browse files
committed
Complete 0006
1 parent 724248b commit 2218028

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

DIYgod/0006/important_word.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import re
4+
import os
5+
6+
# Get all files in designated path
7+
def get_files(path):
8+
filepath = os.listdir(path)
9+
files = []
10+
for fp in filepath:
11+
fppath = path + '/' + fp
12+
if(os.path.isfile(fppath)):
13+
files.append(fppath)
14+
elif(os.path.isdir(fppath)):
15+
files += get_files(fppath)
16+
return files
17+
18+
# Get the most popular word in designated files
19+
def get_important_word(files):
20+
worddict = {}
21+
for filename in files:
22+
f = open(filename, 'rb')
23+
s = f.read()
24+
words = re.findall(r'[a-zA-Z0-9]+', s)
25+
for word in words:
26+
worddict[word] = worddict[word] + 1 if word in worddict else 1
27+
wordsort = sorted(worddict.items(), key=lambda e:e[1], reverse=True)
28+
return wordsort
29+
30+
if __name__ == '__main__':
31+
files = get_files('.')
32+
print files
33+
print get_important_word(files)

DIYgod/0006/test/test.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
important important important important important important important important important important important important important important important important important important important
2+

0 commit comments

Comments
 (0)