Menu
Coddy logo textTech

단어 빈도수 계산하기

Coddy Dart 여정의 로직 & 흐름 섹션에 포함된 레슨 — 65개 중 56번째.

challenge icon

챌린지

쉬움

텍스트 분석기 프로젝트를 계속 진행하면서, 이제 가장 복잡한 기능인 단어 빈도 카운터를 구현하게 됩니다. 이 단계에서는 텍스트에서 각 고유 단어가 몇 번 나타나는지 추적하는 맵(Map)을 생성하여 단어 사용 패턴에 대한 상세한 통찰력을 제공합니다.

  1. 이전 설정에서 사용한 textToAnalyze 변수를 사용하세요.
  2. 글자 수 세기 단계에서 사용한 totalCharacters 변수를 사용하세요.
  3. 단어 분리 단계에서 사용한 wordList 변수를 사용하세요.
  4. 단어 수 세기 단계에서 사용한 totalWords 변수를 사용하세요.
  5. 고유 단어 수 세기 단계에서 사용한 uniqueWordsSet 변수를 사용하세요.
  6. 고유 단어 수 세기 단계에서 사용한 uniqueWordCount 변수를 사용하세요.
  7. 단어 빈도를 저장하기 위해 wordFrequency라는 빈 Map을 생성하세요.
  8. for-in 루프를 사용하여 wordList의 각 단어를 반복합니다.
  9. 각 단어에 대해 containsKey()를 사용하여 맵에 이미 존재하는지 확인합니다.
  10. 단어가 존재하면 해당 카운트를 1 증가시킵니다.
  11. 단어가 존재하지 않으면 카운트 1과 함께 맵에 추가합니다.
  12. 아래에 표시된 정확한 형식으로 단어 빈도 분석 결과를 표시합니다.
  13. 모든 이전 분석 데이터와 적절한 형식을 갖춘 단어 빈도 맵을 함께 보여줍니다.

예를 들어, 텍스트 분석기가 "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.');
}

로직 & 흐름의 모든 레슨