显示分析结果
本课属于 Coddy Dart 学习旅程的“逻辑与流程控制”单元。第 57 课,共 65 课。
挑战
简单在“文本分析器”项目的最后一课中,你将创建一个全面的展示界面,呈现你在整个项目中收集的所有分析结果。这一步会整理并展示所有统计数据,形成清晰、专业的摘要,完整呈现文本分析结果。
- 使用之前项目步骤中的所有变量:
- 项目设置中的
textToAnalyze - 字符计数步骤中的
totalCharacters - 拆分单词步骤中的
wordList - 单词计数步骤中的
totalWords - 唯一单词计数步骤中的
uniqueWordsSet - 唯一单词计数步骤中的
uniqueWordCount - 单词频率统计步骤中的
wordFrequency
- 项目设置中的
- 创建一份全面的最终报告,展示所有分析结果
- 设置输出格式,展示完整的文本分析摘要
- 添加所有统计数据,并使用清晰的标签和恰当的格式
- 按照下方所示的确切格式展示结果
例如,如果你的文本分析器设置的文本是 "hello world hello",程序应输出:
=======================================
TEXT ANALYSIS COMPLETE REPORT
=======================================
Original Text: "hello world hello"
=======================================
ANALYSIS SUMMARY:
- Total Characters: 17
- Total Words: 3
- Unique Words: 2
- Most Common Word: hello (appears 2 times)
=======================================
DETAILED BREAKDOWN:
Word List: [hello, world, hello]
Unique Words Set: {hello, world}
Word Frequency Map: {hello: 2, world: 1}
=======================================
Analysis completed successfully!
Text contains 2 unique words out of 3 total words.
=======================================如果你的文本分析器设置的文本是 "the cat and the dog and the bird",程序应输出:
=======================================
TEXT ANALYSIS COMPLETE REPORT
=======================================
Original Text: "the cat and the dog and the bird"
=======================================
ANALYSIS SUMMARY:
- Total Characters: 32
- Total Words: 8
- Unique Words: 5
- Most Common Word: the (appears 3 times)
=======================================
DETAILED BREAKDOWN:
Word List: [the, cat, and, the, dog, and, the, bird]
Unique Words Set: {the, cat, and, dog, bird}
Word Frequency Map: {the: 3, cat: 1, and: 2, dog: 1, bird: 1}
=======================================
Analysis completed successfully!
Text contains 5 unique words out of 8 total words.
=======================================如果你的文本分析器设置的文本是 "apple banana apple cherry banana apple",程序应输出:
=======================================
TEXT ANALYSIS COMPLETE REPORT
=======================================
Original Text: "apple banana apple cherry banana apple"
=======================================
ANALYSIS SUMMARY:
- Total Characters: 38
- Total Words: 6
- Unique Words: 3
- Most Common Word: apple (appears 3 times)
=======================================
DETAILED BREAKDOWN:
Word List: [apple, banana, apple, cherry, banana, apple]
Unique Words Set: {apple, banana, cherry}
Word Frequency Map: {apple: 3, banana: 2, cherry: 1}
=======================================
Analysis completed successfully!
Text contains 3 unique words out of 6 total words.
=======================================你的程序应通过查找 wordFrequency 映射中频率最高的单词,来确定最常见的单词。使用字符串插值,以专业且易读的格式展示收集到的所有数据。这个最终展示会将你在整个项目中完成的所有分析工作汇总到一份全面的摘要中。
亲手试试
import 'dart:io';
void main() {
String? textToAnalyze = stdin.readLineSync();
int totalCharacters = textToAnalyze!.length;
List<String> wordList = textToAnalyze.split(' ');
int totalWords = wordList.length;
Set<String> uniqueWordsSet = wordList.toSet();
int uniqueWordCount = uniqueWordsSet.length;
Map<String, int> wordFrequency = {};
for (String word in wordList) {
if (wordFrequency.containsKey(word)) {
wordFrequency[word] = wordFrequency[word]! + 1;
} else {
wordFrequency[word] = 1;
}
}
print('Text Analysis Tool - Word Frequency');
print('====================================');
print('Analyzing text: "$textToAnalyze"');
print('Total characters: $totalCharacters');
print('Words found: $wordList');
print('Total words: $totalWords');
print('Unique words: $uniqueWordsSet');
print('Unique word count: $uniqueWordCount');
print('====================================');
print('Word Frequency Analysis Results:');
print('Word frequencies: $wordFrequency');
print('====================================');
print('Word frequency analysis completed successfully.');
}逻辑与流程控制中的所有课
自己练习: Dart 在线编译器