From 931b43efa60bc209835f8ec99479432d6b7f1c17 Mon Sep 17 00:00:00 2001 From: codegonegirth Date: Thu, 31 Jul 2025 13:02:33 +0100 Subject: [PATCH] Update Program.cs --- TestProcessing-1/Program.cs | 68 +++++++++++++++++++++++++------------ 1 file changed, 46 insertions(+), 22 deletions(-) diff --git a/TestProcessing-1/Program.cs b/TestProcessing-1/Program.cs index 696b14e..65dc857 100644 --- a/TestProcessing-1/Program.cs +++ b/TestProcessing-1/Program.cs @@ -6,39 +6,63 @@ using System.Text.RegularExpressions; using System.Threading.Tasks; -namespace TestProcessing_1 +namespace WordStats { internal class Program { static void Main(string[] args) { - Console.WriteLine("gimmefile"); - var z = Console.ReadLine(); - var q = File.ReadAllLines(z); - Dictionary a = new Dictionary(); - for (int i=0; i wordSummary = ProcessFile(userInput); + DisplayWordSummary(wordSummary); + } + catch (Exception error) { - var s = Regex.Replace(q[i],@"[^\w\s]", "").ToLower(); - var t = s.Split(' '); - for (int j=0;j ProcessFile(string path) + { + var wordTracker = new Dictionary(); + + foreach (string line in File.ReadLines(path)) + { + string cleanText = Regex.Replace(line, @"[^\w\s]", "").ToLower(); + string[] splitWords = cleanText.Split(' ', StringSplitOptions.RemoveEmptyEntries); + + foreach (string word in splitWords) { - var w = t[j]; - if (w != "") - { - if (a.ContainsKey(w)) - a[w] = a[w] + 1; - else - a.Add(w, 1); - } + if (wordTracker.ContainsKey(word)) + wordTracker[word]++; + else + wordTracker[word] = 1; } } - Console.WriteLine("Done"); - foreach (var e in a) + + return wordTracker; + } + + static void DisplayWordSummary(Dictionary words) + { + Console.WriteLine("\n--- Word Frequency Report ---\n"); + + foreach (var pair in words.OrderBy(x => x.Key)) { - Console.WriteLine(e.Key + ":" + e.Value); + Console.WriteLine($"Word: '{pair.Key}' | Count: {pair.Value}"); } - Console.WriteLine("uniquez = " + a.Count); - Console.ReadKey(); + + Console.WriteLine($"\nTotal unique words found: {words.Count}"); } } }