nahidjamalli

CountdownEvent version #3

Jul 23rd, 2024 (edited)
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.83 KB | None | 0 0
  1.  
  2. class Program
  3. {
  4.     static void Main(string[] args)
  5.     {
  6.         if (args.Length < 2)
  7.         {
  8.             Console.WriteLine("Usage: <path> <minWordLength>");
  9.             return;
  10.         }
  11.  
  12.         string path = args[0];
  13.         if (!int.TryParse(args[1], out int minWordLength) || minWordLength <= 0)
  14.         {
  15.             Console.WriteLine("Invalid minimum word length.");
  16.             return;
  17.         }
  18.  
  19.         if (!Directory.Exists(path))
  20.         {
  21.             Console.WriteLine("Directory does not exist.");
  22.             return;
  23.         }
  24.  
  25.         var files = Directory.GetFiles(path, "*.txt", SearchOption.AllDirectories);
  26.         var countdownEvent = new CountdownEvent(files.Length);
  27.         var localWordCounts = new ConcurrentBag<Dictionary<string, int>>();
  28.  
  29.         foreach (var filePath in files)
  30.         {
  31.             ThreadPool.QueueUserWorkItem(state =>
  32.             {
  33.                 var localCounts = ProcessFile(filePath, minWordLength);
  34.                 localWordCounts.Add(localCounts);
  35.                 countdownEvent.Signal();
  36.             });
  37.         }
  38.  
  39.         // Wait for all threads to complete
  40.         countdownEvent.Wait();
  41.  
  42.         // Aggregate results
  43.         var aggregatedWordCounts = new Dictionary<string, int>();
  44.         foreach (var localCounts in localWordCounts)
  45.         {
  46.             foreach (var kvp in localCounts)
  47.             {
  48.                 if (aggregatedWordCounts.ContainsKey(kvp.Key))
  49.                 {
  50.                     aggregatedWordCounts[kvp.Key] += kvp.Value;
  51.                 }
  52.                 else
  53.                 {
  54.                     aggregatedWordCounts[kvp.Key] = kvp.Value;
  55.                 }
  56.             }
  57.         }
  58.  
  59.         // Get top-10 most frequent words
  60.         var topWords = aggregatedWordCounts
  61.             .OrderByDescending(kv => kv.Value)
  62.             .Take(10)
  63.             .ToList();
  64.  
  65.         Console.WriteLine("Top 10 most used words:");
  66.         foreach (var word in topWords)
  67.         {
  68.             Console.WriteLine($"{word.Key}: {word.Value}");
  69.         }
  70.     }
  71.  
  72.     private static Dictionary<string, int> ProcessFile(string filePath, int minWordLength)
  73.     {
  74.         var wordCounts = new Dictionary<string, int>(100);
  75.         try
  76.         {
  77.             var text = File.ReadAllText(filePath);
  78.             var words = Regex.Matches(text, $"\\w{{{minWordLength},}}", RegexOptions.IgnoreCase);
  79.  
  80.             foreach (Match word in words)
  81.             {
  82.                 if (wordCounts.TryGetValue(word.Value, out int value))
  83.                     wordCounts[word.Value] = ++value;
  84.                 else
  85.                     wordCounts[word.Value] = 1;
  86.             }
  87.         }
  88.         catch (Exception ex)
  89.         {
  90.             Console.WriteLine($"Error processing file {filePath}: {ex.Message}");
  91.         }
  92.         return wordCounts;
  93.     }
  94. }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment