C# :: Aufgabe #156 :: Lösung #2

3 Lösungen Lösungen öffentlich
#156

Wörterzählen in Textdateien

Anfänger - C# von hollst - 23.12.2016 um 09:50 Uhr
Man schreibe ein Programm, das bei einer beliebigen Textdatei (ASCII)

a) alle Wörter und
b) alle unterschiedlichen Wörter der Längen 1, 2, 3 ... zählt, wobei zwischen Groß- und Kleinbuchstaben nicht zu unterscheiden sei.

Als Wörter-Trennzeichen sind zu verwenden:

a) alle Sonderzeichen (' ', '.', ',', tab ...) sowie Zahlen (0, 1 ... 9) und
b) alle Chars, die nicht zum Aphabet (abc...xyzäöü) gehören (damit wäre a) bereits automatisch erfüllt).

Anmerkungen:
a) Es gibt auch im Deutschen Wörter der Länge 1, z. B. in "a tempo" oder " a priori" oder bei Abkürzungen ("z. B.").
b) Als Beispieltext kann von hier die komplette Bibelausgabe nach Martin Luther 1912 als ASCII-Datei heruntergeladen und verwendet werden (ist mit 4 MB zu lang als Anhang hier, selbst als *.rar noch ca. 1.2 MB).

Viel Spaß und schöne Weihnachten sowie einen guten Rutsch und Start in 2017.
#2
vote_ok
von daniel59 (4260 Punkte) - 04.01.2017 um 10:14 Uhr
Program.cs
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;

namespace ConsoleCountWords
{
    static class Program
    {
        const string allChars = "abcdefghijklmnopqrstuvwxyzäöüß";

        [STAThread]
        static void Main(string[] args)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Textdateien (*.txt)|*.txt";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                string file = ofd.FileName;
                string text = File.ReadAllText(file, Encoding.ASCII);
                object missing = System.Reflection.Missing.Value;

                Word.Application app = new Word.Application();
                Word.Document document = app.Documents.Add(ref missing, ref missing, ref missing, ref missing);
                document.Content.SetRange(0, 0);
                document.Content.Text = text;
                Dictionary<int, int> wordLength;
                int count = document.CountWords(out wordLength);

                if (count == 1)
                { Console.WriteLine("1 Wort gezählt"); }
                else
                { Console.WriteLine("{0} Wörter gezählt", count); }

                foreach (var item in wordLength.OrderBy(a => a.Key))
                {
                    if (item.Value == 1)
                    {
                        Console.WriteLine("1 Wort der Länge {0} gezählt", item.Key);
                    }
                    else
                    {
                        Console.WriteLine("{0} Wörter der Länge {1} gezählt", item.Value, item.Key);
                    }
                }

                document.Close(Word.WdSaveOptions.wdDoNotSaveChanges);
                app.Quit();

                Console.ReadLine();
            }
        }

        static int CountWords(this Word.Document doc, out Dictionary<int, int> wordLength)
        {
            int c = 0;
            wordLength = new Dictionary<int, int>();
            for (int i = 1; i <= doc.Words.Count; i++)
            {
                string word = doc.Words[i].Text.ToLower();
                if (!allChars.Contains(word.Last()))
                { word = word.Substring(0, word.Length - 1); }
                if (word.All(a => allChars.Contains(a)) && word.Length > 0)
                {
                    c++;
                    if (wordLength.ContainsKey(word.Length))
                    { wordLength[word.Length]++; }
                    else
                    {
                        wordLength.Add(word.Length, 1);
                    }
                }
            }
            return c;
        }
    }
}

Kommentare:

Für diese Lösung gibt es noch keinen Kommentar

Bitte melden Sie sich an um eine Kommentar zu schreiben.
Kommentar schreiben