nahidjamalli

ThreadPool based version #2

Jul 23rd, 2024
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.87 KB | None | 0 0
  1. namespace WordFrequencyFinder
  2. {
  3.     class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.             if (args.Length < 2)
  8.             {
  9.                 Console.WriteLine("Usage: WordFrequencyFinder <directory path> <min word length>");
  10.                 return;
  11.             }
  12.  
  13.             string directoryPath = args[0];
  14.             if (!int.TryParse(args[1], out int minWordLength))
  15.             {
  16.                 Console.WriteLine("Invalid minimum word length.");
  17.                 return;
  18.             }
  19.  
  20.             if (!Directory.Exists(directoryPath))
  21.             {
  22.                 Console.WriteLine("Directory does not exist.");
  23.                 return;
  24.             }
  25.  
  26.             var wordFrequency = GetWordFrequencies(directoryPath, minWordLength);
  27.             var topWords = wordFrequency.OrderByDescending(kvp => kvp.Value).Take(10);
  28.  
  29.             Console.WriteLine("Top-10 most used words:");
  30.             foreach (var word in topWords)
  31.             {
  32.                 Console.WriteLine($"{word.Key}: {word.Value}");
  33.             }
  34.         }
  35.  
  36.         static ConcurrentDictionary<string, int> GetWordFrequencies(string directoryPath, int minWordLength)
  37.         {
  38.             var wordFrequency = new ConcurrentDictionary<string, int>(-1, 100);
  39.             var files = Directory.GetFiles(directoryPath, "*.txt");
  40.  
  41.             var resetEvents = new ManualResetEvent[files.Length];
  42.  
  43.             for (int i = 0; i < files.Length; i++)
  44.             {
  45.                 resetEvents[i] = new ManualResetEvent(false);
  46.                 var state = new ThreadPoolState
  47.                 {
  48.                     FilePath = files[i],
  49.                     MinWordLength = minWordLength,
  50.                     WordFrequency = wordFrequency,
  51.                     ResetEvent = resetEvents[i]
  52.                 };
  53.  
  54.                 ThreadPool.QueueUserWorkItem(ProcessFile, state);
  55.             }
  56.  
  57.             WaitHandle.WaitAll(resetEvents);
  58.  
  59.             return wordFrequency;
  60.         }
  61.  
  62.         static void ProcessFile(object state)
  63.         {
  64.             var threadState = (ThreadPoolState)state;
  65.             var content = File.ReadAllText(threadState.FilePath);
  66.  
  67.             var words = Regex.Split(content.ToLowerInvariant(), @"\W+")
  68.                              .Where(word => word.Length >= threadState.MinWordLength);
  69.  
  70.             foreach (var word in words)
  71.             {
  72.                 threadState.WordFrequency.AddOrUpdate(word, 1, (key, oldValue) => oldValue + 1);
  73.             }
  74.  
  75.             threadState.ResetEvent.Set();
  76.         }
  77.  
  78.         class ThreadPoolState
  79.         {
  80.             public required string FilePath { get; set; }
  81.             public int MinWordLength { get; set; }
  82.             public required ConcurrentDictionary<string, int> WordFrequency { get; set; }
  83.             public required ManualResetEvent ResetEvent { get; set; }
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment