Word Frequency Counter
Part of the Logic & Flow section of Coddy's Dart journey — lesson 56 of 65.
Challenge
EasyContinuing 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.
- Use the
textToAnalyzevariable from your previous setup - Use the
totalCharactersvariable from your character counting step - Use the
wordListvariable from your word splitting step - Use the
totalWordsvariable from your word counting step - Use the
uniqueWordsSetvariable from your unique word counting step - Use the
uniqueWordCountvariable from your unique word counting step - Create an empty Map called
wordFrequencyto store word counts - Iterate through each word in the
wordListusing a for-in loop - For each word, check if it already exists in the map using
containsKey() - If the word exists, increment its count by 1
- If the word doesn't exist, add it to the map with a count of 1
- Display the word frequency analysis results in the exact format shown below
- 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
1Advanced List Manipulation
List Properties: first & lastList State: isEmpty & isNotEmpReversing a ListAdding to a List: insertList Removal: removeWhereFinding in a List: indexOfSorting a ListShuffling a ListRecap - List Organizer4Advanced Map Manipulation
Iterating Over a MapChecking for Keys and ValuesMap Properties: keys & valuesConditional Add: putIfAbsentRemoving Entries from a MapNested MapsRecap - Inventory Update2Functional List Operations
Transforming with 'map'Filtering with 'where'Using '.toList()'Checking Conditions with 'any'Conditions with 'every'Finding with 'firstWhere'Recap - Data Filtering5Project: Shopping Cart Calc
Project SetupAdding Items to the Cart8Project: Simple Text Analyzer
Project Overview and SetupCounting Characters3Sets
What is a Set?Creating a SetAdding and Removing from SetsChecking for Elements in a SetConverting a List to a SetSet UnionSet IntersectionSet DifferenceRecap - Unique Guest List