Q. Take a sample text file and find the most commonly occurring word. Also, list the frequencies of words in the text file.
Answer :-
with open ("nanthem.txt", "r") as fh: contents = fh.read() wordlist = contents.split() wordfreq = [] high = 0 word = '' existing = [] for w in wordlist: wcount = wordlist.count(w) if w not in existing: wordfreq.append([w,wcount]) existing.append(w) if wcount > high: high = wcount word = w print("The word "+ word +" occurs maximum number of times,", high, "times. ") print("\nother words have these frequencies :") print (wordfreq)
For the text file nanthem.txt shown below, the above program produced the following output:-
Jan gan man adhinayaka jaya he
Bharat bhagya vidhata
Panjab Sindh Gujarat Maratha
Dravid utkal Banga
Vindhye Himachal Yamuna Ganga
Uchchal jaladhi tarang
Tava shubh name jage,
Tava shubh ashish mange,
Gahe tava jaya gatha.
Jan gan mangal dayak jaya he
Output:-
The word jaya occurs maximum number of times, 3 times.
other words have these frequencies :
[['Jan', 2], ['gan', 2], ['man', 1], ['adhinayaka', 1], ['jaya', 3], ['he', 2], ['Bharat', 1], ['bhagya', 1], ['vidhata', 1], ['Panjab', 1], ['Sindh', 1], ['Gujarat', 1], ['Maratha', 1], ['Dravid', 1], ['utkal', 1], ['Banga', 1], ['Vindhye', 1], ['Himachal', 1], ['Yamuna', 1], ['Ganga', 1], ['Uchchal', 1], ['jaladhi', 1], ['tarang', 1], ['Tava', 2], ['shubh', 2], ['name', 1], ['jage,', 1], ['ashish', 1], ['mange,', 1], ['Gahe', 1], ['tava', 1], ['gatha.', 1], ['mangal', 1], ['dayak', 1]]
>>>
Post a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )