Menu
Coddy logo textTech

Word Frequency Counter

Part of the Logic & Flow section of Coddy's Dart journey — lesson 56 of 65.

challenge icon

Challenge

Easy

Continuing your text analyzer project, you'll now implement the most complex feature: a word frequency counter. This step will create a map that tracks how many times each unique word appears in your text, providing detailed insights into word usage patterns.

  1. Use the textToAnalyze variable from your previous setup
  2. Use the totalCharacters variable from your character counting step
  3. Use the wordList variable from your word splitting step
  4. Use the totalWords variable from your word counting step
  5. Use the uniqueWordsSet variable from your unique word counting step
  6. Use the uniqueWordCount variable from your unique word counting step
  7. Create an empty Map called wordFrequency to store word counts
  8. Iterate through each word in the wordList using a for-in loop
  9. For each word, check if it already exists in the map using containsKey()
  10. If the word exists, increment its count by 1
  11. If the word doesn't exist, add it to the map with a count of 1
  12. Display the word frequency analysis results in the exact format shown below
  13. Show all previous analysis data plus the word frequency map with proper formatting

For example, if your text analyzer was set up with "hello world hello", your program should output:

Text Analysis Tool - Word Frequency
====================================
Analyzing text: "hello world hello"
Total characters: 17
Words found: [hello, world, hello]
Total words: 3
Unique words: {hello, world}
Unique word count: 2
====================================
Word Frequency Analysis Results:
Word frequencies: {hello: 2, world: 1}
====================================
Word frequency analysis completed successfully.

If your text analyzer was set up with "the cat and the dog and the bird", your program should output:

Text Analysis Tool - Word Frequency
====================================
Analyzing text: "the cat and the dog and the bird"
Total characters: 33
Words found: [the, cat, and, the, dog, and, the, bird]
Total words: 8
Unique words: {the, cat, and, dog, bird}
Unique word count: 5
====================================
Word Frequency Analysis Results:
Word frequencies: {the: 3, cat: 1, and: 2, dog: 1, bird: 1}
====================================
Word frequency analysis completed successfully.

If your text analyzer was set up with "apple banana apple cherry banana apple", your program should output:

Text Analysis Tool - Word Frequency
====================================
Analyzing text: "apple banana apple cherry banana apple"
Total characters: 38
Words found: [apple, banana, apple, cherry, banana, apple]
Total words: 6
Unique words: {apple, banana, cherry}
Unique word count: 3
====================================
Word Frequency Analysis Results:
Word frequencies: {apple: 3, banana: 2, cherry: 1}
====================================
Word frequency analysis completed successfully.

Your program should iterate through the word list and build a frequency map by checking if each word already exists in the map. Use containsKey() to check for existing words, and either increment the existing count or initialize a new entry with count 1. This frequency analysis will provide the most detailed insight into your text's word usage patterns.

Try it yourself

import 'dart:io';

void main() {
  // Text analyzer setup
  String? textToAnalyze = stdin.readLineSync();
  
  // Character counting
  int totalCharacters = textToAnalyze!.length;
  
  // Word splitting and counting
  List<String> wordList = textToAnalyze.split(' ');
  int totalWords = wordList.length;
  
  // Unique word analysis
  Set<String> uniqueWordsSet = Set.from(wordList);
  int uniqueWordCount = uniqueWordsSet.length;
  
  // Display results
  print('Text Analysis Tool - Unique Words');
  print('====================================');
  print('Analyzing text: "$textToAnalyze"');
  print('Total characters: $totalCharacters');
  print('Words found: $wordList');
  print('Total words: $totalWords');
  print('====================================');
  print('Unique Word Analysis Results:');
  print('Unique words: $uniqueWordsSet');
  print('Unique word count: $uniqueWordCount');
  print('====================================');
  print('Unique word analysis completed successfully.');
}

All lessons in Logic & Flow