-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordcount.py
More file actions
38 lines (31 loc) · 1.09 KB
/
Copy pathwordcount.py
File metadata and controls
38 lines (31 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import sys
import os
def main():
if len(sys.argv) < 2:
filepath = input("Enter file path here:")
else:
filepath = sys.argv[1]
if not os.path.isfile(filepath):
print("File path {} does not exist. Exiting ...".format(filepath))
sys.exit()
bag_of_words = {}
with open(filepath) as fp:
cnt = 0
for line in fp:
print("line {} contents {}".format(cnt, line))
record_word_cnt(line.strip().split(' '), bag_of_words)
cnt += 1
sorted_words = order_bag_of_words(bag_of_words, desc=True)
print("Most frequent 10 words {}".format(sorted_words[:10]))
def order_bag_of_words(bag_of_words, desc=False):
words = [(word, cnt) for word, cnt in bag_of_words.items()]
return sorted(words, key=lambda x: x[1], reverse=desc)
def record_word_cnt(words, bag_of_words):
for word in words:
if word != '':
if word.lower() in bag_of_words:
bag_of_words[word.lower()] += 1
else:
bag_of_words[word.lower()] = 0
if __name__ == '__main__':
main()