Menu
Coddy logo textTech

Wortfrequenz-Zähler

Teil des Abschnitts Logik & Ablauf der Dart-Journey von Coddy — Lektion 56 von 65.

challenge icon

Aufgabe

Einfach

In Fortsetzung Ihres Textanalyse-Projekts werden Sie nun die komplexeste Funktion implementieren: einen Wortfrequenzzähler. Dieser Schritt erstellt eine Map, die verfolgt, wie oft jedes eindeutige Wort in Ihrem Text vorkommt, und bietet so detaillierte Einblicke in die Wortnutzungsmuster.

  1. Verwenden Sie die Variable textToAnalyze aus Ihrem vorherigen Setup
  2. Verwenden Sie die Variable totalCharacters aus Ihrem Schritt zur Zeichenzählung
  3. Verwenden Sie die Variable wordList aus Ihrem Schritt zur Worttrennung
  4. Verwenden Sie die Variable totalWords aus Ihrem Schritt zur Wortzählung
  5. Verwenden Sie die Variable uniqueWordsSet aus Ihrem Schritt zur Zählung eindeutiger Wörter
  6. Verwenden Sie die Variable uniqueWordCount aus Ihrem Schritt zur Zählung eindeutiger Wörter
  7. Erstellen Sie eine leere Map namens wordFrequency, um die Wortanzahlen zu speichern
  8. Iterieren Sie mit einer for-in-Schleife durch jedes Wort in der wordList
  9. Prüfen Sie für jedes Wort mit containsKey(), ob es bereits in der Map existiert
  10. Wenn das Wort existiert, erhöhen Sie dessen Anzahl um 1
  11. Wenn das Wort nicht existiert, fügen Sie es mit einer Anzahl von 1 zur Map hinzu
  12. Zeigen Sie die Ergebnisse der Wortfrequenzanalyse im unten gezeigten exakten Format an
  13. Zeigen Sie alle vorherigen Analysedaten plus die Wortfrequenz-Map mit korrekter Formatierung an

Wenn Ihr Textanalysator beispielsweise mit "hello world hello" eingerichtet wurde, sollte Ihr Programm Folgendes ausgeben:

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.

Wenn Ihr Textanalysator mit "the cat and the dog and the bird" eingerichtet wurde, sollte Ihr Programm Folgendes ausgeben:

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.

Wenn Ihr Textanalysator mit "apple banana apple cherry banana apple" eingerichtet wurde, sollte Ihr Programm Folgendes ausgeben:

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.

Ihr Programm sollte die Wortliste durchlaufen und eine Frequenz-Map erstellen, indem es prüft, ob jedes Wort bereits in der Map vorhanden ist. Verwenden Sie containsKey(), um nach vorhandenen Wörtern zu suchen, und erhöhen Sie entweder den vorhandenen Zähler oder initialisieren Sie einen neuen Eintrag mit dem Wert 1. Diese Frequenzanalyse liefert den detailliertesten Einblick in die Wortnutzungsmuster Ihres Textes.

Probier es selbst

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.');
}

Alle Lektionen in Logik & Ablauf