nahidjamalli

Parallel.ForEach version #4

Jul 23rd, 2024
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.03 KB | None | 0 0
  1. class Program
  2. {
  3.     private static readonly ConcurrentDictionary<string, int> wordCounts = new ConcurrentDictionary<string, int>();
  4.  
  5.     static void Main(string[] args)
  6.     {
  7.         if (args.Length < 2)
  8.         {
  9.             Console.WriteLine("Usage: <path> <minWordLength>");
  10.             return;
  11.         }
  12.  
  13.         string path = args[0];
  14.         if (!int.TryParse(args[1], out int minWordLength) || minWordLength <= 0)
  15.         {
  16.             Console.WriteLine("Invalid minimum word length.");
  17.             return;
  18.         }
  19.  
  20.         if (!Directory.Exists(path))
  21.         {
  22.             Console.WriteLine("Directory does not exist.");
  23.             return;
  24.         }
  25.  
  26.         var files = Directory.GetFiles(path, "*.txt", SearchOption.AllDirectories);
  27.  
  28.         // Process files using Parallel.ForEach
  29.         Parallel.ForEach(files, (filePath) =>
  30.         {
  31.             ProcessFile(filePath, minWordLength);
  32.         });
  33.  
  34.         // Get top-10 most frequent words
  35.         var topWords = wordCounts
  36.             .OrderByDescending(kv => kv.Value)
  37.             .Take(10)
  38.             .ToList();
  39.  
  40.         Console.WriteLine("Top 10 most used words:");
  41.         foreach (var word in topWords)
  42.         {
  43.             Console.WriteLine($"{word.Key}: {word.Value}");
  44.         }
  45.     }
  46.  
  47.     private static void ProcessFile(string filePath, int minWordLength)
  48.     {
  49.         try
  50.         {
  51.             var text = File.ReadAllText(filePath);
  52.             var words = text.Split(new[] { ' ', '\r', '\n', '\t', '.', ',', ';', '!', '?' }, StringSplitOptions.RemoveEmptyEntries);
  53.  
  54.             foreach (var word in words)
  55.             {
  56.                 if (word.Length >= minWordLength)
  57.                 {
  58.                     var normalizedWord = word.ToLowerInvariant();
  59.                     wordCounts.AddOrUpdate(normalizedWord, 1, (key, oldValue) => oldValue + 1);
  60.                 }
  61.             }
  62.         }
  63.         catch (Exception ex)
  64.         {
  65.             Console.WriteLine($"Error processing file {filePath}: {ex.Message}");
  66.         }
  67.     }
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment