単語の出現頻度のカウント
CoddyのDartジャーニー「論理とフロー」セクションの一部 — レッスン 56/65。
チャレンジ
簡単テキスト分析プロジェクトの続きとして、最も複雑な機能である単語頻度カウンターを実装します。このステップでは、テキスト内に各ユニークな単語が何回出現するかを追跡するマップを作成し、単語の使用パターンに関する詳細な洞察を提供します。
- 前のセットアップで使用した
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リストのソートリストのシャッフルまとめ - リストオーガナイザー