单词频率统计器
本课属于 Coddy Dart 学习旅程的“逻辑与流程控制”单元。第 56 课,共 65 课。
挑战
简单继续完善你的文本分析器项目,现在你将实现最复杂的功能:单词频率计数器。这一步会创建一个映射,用来跟踪每个不同的单词在文本中出现的次数,从而深入了解单词的使用模式。
- 使用之前设置中的
textToAnalyze变量 - 使用字符计数步骤中的
totalCharacters变量 - 使用单词拆分步骤中的
wordList变量 - 使用单词计数步骤中的
totalWords变量 - 使用不同单词计数步骤中的
uniqueWordsSet变量 - 使用不同单词计数步骤中的
uniqueWordCount变量 - 创建一个名为
wordFrequency的空 Map,用于存储单词计数 - 使用 for-in 循环遍历
wordList中的每个单词 - 对于每个单词,使用
containsKey()检查映射中是否已存在该单词 - 如果该单词已存在,将它的计数加 1
- 如果该单词不存在,将它添加到映射中,并将计数设为 1
- 按照下方显示的确切格式展示单词频率分析结果
- 正确格式化并展示所有之前的分析数据,以及单词频率映射
例如,如果你的文本分析器使用 "hello world hello" 进行设置,程序应输出:
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.如果你的文本分析器使用 "the cat and the dog and the bird" 进行设置,程序应输出:
Text Analysis Tool - Word Frequency
====================================
Analyzing text: "the cat and the dog and the bird"
Total characters: 32
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.如果你的文本分析器使用 "apple banana apple cherry banana apple" 进行设置,程序应输出:
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.你的程序应遍历单词列表,并通过检查每个单词是否已存在于映射中来构建频率映射。使用 containsKey() 检查是否已有该单词,然后增加现有计数,或者以计数 1 初始化新条目。这项频率分析能让你最深入地了解文本中的单词使用模式。
亲手试试
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.');
}逻辑与流程控制中的所有课
自己练习: Dart 在线编译器