Write a Python program to count the frequency of words in a
Solution
1)
since the program only talks about the frequency of words, we would use python functions to first turn every word to lower case, so that we can operate on them easily.
then we use the python\'s regular expression to first, find all the words which are in the file, then counting the frequency of all those words one by one, then printing them.
code ----
import re
import string
\'\'\' open the file in read mode\'\'\'
f1 = open(\"myfile.txt\",\'r\')
\'\'\'convert to lower case\'\'\'
str = f1.read().lower()
\'\'\'finding all words\'\'\'
all_words = re.findall(r\'\\b[a-z]{2,17}\\b\',str)
\'\'\'counting all words\'\'\'
freq = {}
for word in all_words:
count = freq.get(word,0)
freq[word]=count + 1
freq-list = freq.keys()
for words in freq_list():
print word , freq[word]
--------------------
test run
if file has text as \"chegg india chegg india\"
output after reading file would be
chegg 2
india 2
----------------------------------------------------------------
2) sentences 3) words
this is simple just count the number of \'.\' and \'!\' and \'?\' in the complete text file for number of sentences, and spaces for number of words.
open the file just like above
----------.
import re
import string
\'\'\' open the file in read mode\'\'\'
f1 = open(\"myfile.txt\",\'r\')
\'\'\'read one line at a time \'\'\'
count = 0
sentences = 0
words = 0
for line in f1 :
sentences += line.count(\'.\') + line.count(\'?\') + line.count(\'!\')
\'\'\'for words just count the spaces\'\'\'
temp = line.split(None)
words = words + len(temp)
print sentences
print words
------------------------------------------

