In: Computer Science
In Python write a function with prototype “def wordfreq(filename = "somefile.txt"):” that will read a given file that contains words separated by spaces (perhaps multiple words on a line) and will create a dictionary whose keys are the words and the value is the number of times the word appears. Convert each word to lower case before processing.
#Program to get dictionary of words and its frequecy for input file
Feel free to ask any doubt. I will try to clear any doubt
import re
import string
#function to get dictionary of words and its frequecy for input file
def wordfreq(filename = "somefile.txt"):
#create a dictionary
word_freq_dict = {}
#Open file
document_text = open('Sample.txt', 'r')
#read file and convert everything in lower
text_string = document_text.read().lower()
#regular expression that would return all the words with the number of characters in the range [1-15]
match_pattern = re.findall(r'\b[a-z]{1,15}\b', text_string)
#get the count of all words
for word in match_pattern:
count = word_freq_dict.get(word,0)
word_freq_dict[word] = count + 1
return word_freq_dict
#Main program
if __name__ == "__main__":
word_freq_dict = wordfreq("ample.txt")
print(word_freq_dict)
Explanation:
1) Above program has a function wordfreq which returns dictionary of words and its frequecy for input file
2) It has a main program at the bottom, where this function is called with input file
3) Copy above code in python file and run,Make sure you have input file exist
4) Below is test output
{'department': 1, 'of': 1, 'computer': 1, 'science': 1, 'university': 1, 'major': 2, 'in': 2, 'information': 2, 'technology': 2, 'test': 2, 'my': 2, 'name': 2}