Computer Science – 10.3 Files | e-Consult
10.3 Files (1 questions)
Login to see all questions.
Click on a question to view the answer
Algorithm: Word Frequency Counter
Data Structure: A dictionary where the keys are the words (in lowercase) and the values are the counts of how many times each word appears.
- Start
- Open the text file for reading.
- Create an empty dictionary called
wordFrequencies. - For each line in the file:
- Read the line.
- Convert the line to lowercase.
- Split the line into individual words using whitespace as a delimiter.
- For each word:
- If the word is already a key in
wordFrequencies:- Increment the value (count) associated with the word by 1.
- Otherwise:
- Add the word as a new key to
wordFrequencieswith a value of 1.
- Add the word as a new key to
- If the word is already a key in
- Print the
wordFrequenciesdictionary. This will show each word and its count. - Close the text file.
- End