Skip to content

Commit 1082186

Browse files
committed
Added the script for word frequrncy counter issue
1 parent de808b4 commit 1082186

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Word_frequency_counter/main.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import re
2+
from collections import Counter
3+
4+
def find_words_frequency(file_path):
5+
'''
6+
This script takes the path of the text file to be processed, as input (argument)
7+
and prints the top ten words and also prints their counts in given text file.
8+
'''
9+
with open(file_path, 'r', encoding='utf-8') as file:
10+
text = file.read().lower()
11+
12+
all_words = re.findall(r'\b\w+\b', text)
13+
word_frequency = Counter(all_words)
14+
most_common_words = word_frequency.most_common(10)
15+
16+
# Print in tabular format
17+
print(f"{'Word':<15} {'Count':<5}")
18+
print("-" * 20)
19+
for word, count in most_common_words:
20+
print(f"{word:<15} {count:<5}")
21+
22+
def main():
23+
file_path = input("Enter the path of file : ")
24+
find_words_frequency(file_path)
25+
26+
if __name__ == "__main__":
27+
main()

0 commit comments

Comments
 (0)