File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed
Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change 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 ()
You can’t perform that action at this time.
0 commit comments