0

I am newbie in Python.I'm making a word count program. Till now I made my program count all the words in a file, now I would like to count the frequency of words in a file.

I saw on stackoverflow a few examples but I'm not quite sure how to implement them to my code.

This is part of my code:

def read_file(filename):
    with open(filename, 'r') as f:
        return f.read()

def word_count(filename):
    return len(read_file(filename).split())
The6thSense
  • 8,103
  • 8
  • 31
  • 65
John Charger
  • 416
  • 6
  • 21

2 Answers2

1

You'll want to create a set of words (i.e., a unique collection of words), count the number of times the words appears, then divide by the size of the list.

The following Python code should get you started. It assumes you want frequency as a percent of the total words.

word_list = ['your', 'words', 'here', ...] 
# I'll leave this up to you as an exercise in populating lists

word_set = set(word_list)

freq = {}

for word in word_set:
    freq[word] = word_list.count(word) / float(len(word_list))
erip
  • 16,374
  • 11
  • 66
  • 121
1

The easiest way to achieve what you are looking for is to use a dictionary counting the occurrence of each word.

def read_file(filename):
    with open(filename, 'r') as f:
        return f.read()

def word_count(filename):
    d = {}
    words = read_file(filename).split()
    for word in words:
        if word in d:
            d[word] +=1
        else:
            d[word] = 1
    for keys,values in d.items():
        print(keys)
        print(values)

    return len(words)
Bawn
  • 509
  • 3
  • 13
  • 36