Blogger Tips and TricksLatest Tips And TricksBlogger Tricks

Program to count the frequency of occurrence of a word in a given file in python


import re
import sys
import string
frequency = {}
fopen=open(sys.argv[1],'r')
document_text = fopen
text_string = document_text.read().lower()
match_pattern = re.findall(r'\b[a-z]{3,15}\b', text_string)

for word in match_pattern:
    count = frequency.get(word,0)
    frequency[word] = count + 1
   
frequency_list = frequency.keys()
print ("The word and its frequency")
for words in frequency_list:
    print (words, frequency[words])
fopen.close()

No comments:

Post a Comment

Flag Counter