Menu
Coddy logo textTech

単語数のカウント

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

challenge icon

チャレンジ

簡単

テキスト解析プロジェクトを継続し、今回は単語カウント機能を実装します。このステップでは、前回のレッスンで行った単語分割の作業を基に、テキスト内の単語の総数をカウントします。

  1. 前回のセットアップで使用した textToAnalyze 変数を使用します
  2. 文字カウントのステップで使用した totalCharacters 変数を使用します
  3. 単語分割のステップで使用した wordList 変数を使用します
  4. 単語リストの .length プロパティを使用して、単語の総数をカウントします
  5. 単語数を totalWords という名前の変数に格納します
  6. 単語カウントの解析結果を、以下に示す正確な形式で表示します
  7. 元のテキスト、文字数、単語リスト、および単語数を適切なフォーマットで表示します

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

Text Analysis Tool - Word Count
====================================
Analyzing text: "Hello world!"
Total characters: 12
Words found: [Hello, world!]
====================================
Word Count Results:
Total words: 2
====================================
Word counting completed successfully.

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

Text Analysis Tool - Word Count
====================================
Analyzing text: "The quick brown fox"
Total characters: 19
Words found: [The, quick, brown, fox]
====================================
Word Count Results:
Total words: 4
====================================
Word counting completed successfully.

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

Text Analysis Tool - Word Count
====================================
Analyzing text: "Programming is fun!"
Total characters: 19
Words found: [Programming, is, fun!]
====================================
Word Count Results:
Total words: 3
====================================
Word counting completed successfully.

プログラムでは、単語リストの .length プロパティを使用して、テキストを分割した後に見つかった単語の数を特定する必要があります。この単語数は、テキストの複雑さに関する重要な情報を提供し、今後のレッスンで他の解析指標と組み合わせて使用されます。

自分で試してみよう

import 'dart:io';

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

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