단어 빈도수 계산하기
Coddy Dart 여정의 로직 & 흐름 섹션에 포함된 레슨 — 65개 중 56번째.
챌린지
쉬움텍스트 분석기 프로젝트를 계속 진행하면서, 이제 가장 복잡한 기능인 단어 빈도수 계산기를 구현해 보겠습니다. 이 단계에서는 텍스트에서 각 고유 단어가 몇 번 나타나는지 추적하는 맵(Map)을 생성하여, 단어 사용 패턴에 대한 상세한 통찰력을 제공할 것입니다.
- 이전 설정에서 사용한
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: 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.텍스트 분석기가 "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.');
}로직 & 흐름의 모든 레슨
1고급 리스트 조작
리스트 속성: first 및 last리스트 상태: isEmpty 및 isNotEmpty리스트 뒤집기리스트에 추가: insert리스트 요소 제거: removeWhere리스트에서 찾기: indexOf리스트 정렬리스트 섞기요약 - 리스트 정리 도구