Menu
Coddy logo textTech

ユニークな単語数のカウント

CoddyのDartジャーニー「論理とフロー」セクションの一部 — レッスン 55/65。

challenge icon

チャレンジ

簡単

テキスト分析プロジェクトの続きとして、今回はユニークな単語のカウント機能を実装します。このステップでは、Set(セット)を使用して単語リストから重複を自動的に削除し、テキスト内にいくつの異なる単語が含まれているかを判断します。

  1. 前のセットアップで使用した textToAnalyze 変数を使用します
  2. 文字数カウントのステップで使用した totalCharacters 変数を使用します
  3. 単語分割のステップで使用した wordList 変数を使用します
  4. 単語数カウントのステップで使用した totalWords 変数を使用します
  5. Set.from() を使用して単語リストを Set に変換し、重複する単語を自動的に削除します
  6. 結果の Set を uniqueWordsSet という名前の変数に格納します
  7. Set の .length プロパティを使用して、ユニークな単語の数をカウントします
  8. ユニークな単語の数を uniqueWordCount という名前の変数に格納します
  9. ユニークな単語の分析結果を、以下に示す正確な形式で表示します
  10. 元のテキスト、文字数、単語リスト、総単語数、ユニークな単語のセット、およびユニークな単語の数を、適切なフォーマットで表示します

例えば、テキスト分析ツールが "Hello world hello" でセットアップされている場合、プログラムは次のように出力する必要があります:

Text Analysis Tool - Unique Words
====================================
Analyzing text: "Hello world hello"
Total characters: 17
Words found: [Hello, world, hello]
Total words: 3
====================================
Unique Word Analysis Results:
Unique words: {Hello, world, hello}
Unique word count: 3
====================================
Unique word analysis completed successfully.

テキスト分析ツールが "The cat and the dog" でセットアップされている場合、プログラムは次のように出力する必要があります:

Text Analysis Tool - Unique Words
====================================
Analyzing text: "The cat and the dog"
Total characters: 19
Words found: [The, cat, and, the, dog]
Total words: 5
====================================
Unique Word Analysis Results:
Unique words: {The, cat, and, the, dog}
Unique word count: 4
====================================
Unique word analysis completed successfully.

テキスト分析ツールが "apple banana apple cherry banana" でセットアップされている場合、プログラムは次のように出力する必要があります:

Text Analysis Tool - Unique Words
====================================
Analyzing text: "apple banana apple cherry banana"
Total characters: 33
Words found: [apple, banana, apple, cherry, banana]
Total words: 5
====================================
Unique Word Analysis Results:
Unique words: {apple, banana, cherry}
Unique word count: 3
====================================
Unique word analysis completed successfully.

プログラムでは Set.from() を使用して単語リストを Set に変換する必要があります。これにより、ユニークな単語を保持したまま重複する単語が自動的に削除されます。Set の .length プロパティを使用することで、ユニークな単語の数を取得できます。この分析はテキストの語彙の豊富さを特定するのに役立ち、今後のレッスンで他の指標とともに使用されます。

自分で試してみよう

import 'dart:io';

void main() {
  String? textToAnalyze = stdin.readLineSync();
  int totalCharacters = textToAnalyze!.length;
  List<String> wordList = textToAnalyze.split(' ');
  int totalWords = wordList.length;
  
  print('Text Analysis Tool - Word Count');
  print('====================================');
  print('Analyzing text: "$textToAnalyze"');
  print('Total characters: $totalCharacters');
  print('Words found: $wordList');
  print('====================================');
  print('Word Count Results:');
  print('Total words: $totalWords');
  print('====================================');
  print('Word counting completed successfully.');
}

論理とフローのすべてのレッスン