Solved: Spoiler Solution to Word Frequency Assignment

Sharing my word count code for the assignment in Session 3:

print("Word Count program executed. Enter the filename of a text file you wish to read:")
try:
    file = open(input(''), 'r')
    linecount = 0
    wordcount = 0
    charcount = 0
    for line in file:
        linecount = linecount + 1
        charcount = charcount + len(line)
    print("The specified file contains: ")
    print("Lines: " + str(linecount))
    print("Characters: " + str(charcount))
    file.close()
    file = open('test.txt', 'r')
    data = file.read()
    data = data.split(" ")
    data = str(data).replace("\\n", " ")
    data = data.split(" ")
    wordcount = len(data)
    print("Words: " + str(wordcount))
    file.close()
except:
    print("File does not exist")

Hi Gopi,

Your program is good for counting total words and characters.
We are supposed to find the frequency of each word instead of just counting total words.

So, if my file contains: cat is a cat is a cat

The output should be:
a 2
cat 3
is 2

1 Like

Is this better:

print("Word frequency program executed. Enter the filename of a text file you wish to read:")
try:
    file = open(input(''), 'r')
except:
    print("File does not exist")
wordfreq = {}
words = file.read().split()
print("Generating word list:... ")
print("Counting " + str(len(words)) + " words...")
for word in words:
    wordfreq[word] = wordfreq.get(word,0) + 1
index = sorted(wordfreq.items())
for word_key, wordcount in index:
    print(word_key, wordcount)
file.close()
1 Like

Did you try to run it?
Is it working?

To me it looks okay.

Yes working now thank you

1 Like