Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- static void Main(string[] args)
- {
- if (args.Length < 2)
- {
- Console.WriteLine("Usage: <path> <minWordLength>");
- return;
- }
- string path = args[0];
- if (!int.TryParse(args[1], out int minWordLength) || minWordLength <= 0)
- {
- Console.WriteLine("Invalid minimum word length.");
- return;
- }
- if (!Directory.Exists(path))
- {
- Console.WriteLine("Directory does not exist.");
- return;
- }
- var files = Directory.GetFiles(path, "*.txt", SearchOption.AllDirectories);
- var countdownEvent = new CountdownEvent(files.Length);
- var localWordCounts = new ConcurrentBag<Dictionary<string, int>>();
- foreach (var filePath in files)
- {
- ThreadPool.QueueUserWorkItem(state =>
- {
- var localCounts = ProcessFile(filePath, minWordLength);
- localWordCounts.Add(localCounts);
- countdownEvent.Signal();
- });
- }
- // Wait for all threads to complete
- countdownEvent.Wait();
- // Aggregate results
- var aggregatedWordCounts = new Dictionary<string, int>();
- foreach (var localCounts in localWordCounts)
- {
- foreach (var kvp in localCounts)
- {
- if (aggregatedWordCounts.ContainsKey(kvp.Key))
- {
- aggregatedWordCounts[kvp.Key] += kvp.Value;
- }
- else
- {
- aggregatedWordCounts[kvp.Key] = kvp.Value;
- }
- }
- }
- // Get top-10 most frequent words
- var topWords = aggregatedWordCounts
- .OrderByDescending(kv => kv.Value)
- .Take(10)
- .ToList();
- Console.WriteLine("Top 10 most used words:");
- foreach (var word in topWords)
- {
- Console.WriteLine($"{word.Key}: {word.Value}");
- }
- }
- private static Dictionary<string, int> ProcessFile(string filePath, int minWordLength)
- {
- var wordCounts = new Dictionary<string, int>(100);
- try
- {
- var text = File.ReadAllText(filePath);
- var words = Regex.Matches(text, $"\\w{{{minWordLength},}}", RegexOptions.IgnoreCase);
- foreach (Match word in words)
- {
- if (wordCounts.TryGetValue(word.Value, out int value))
- wordCounts[word.Value] = ++value;
- else
- wordCounts[word.Value] = 1;
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine($"Error processing file {filePath}: {ex.Message}");
- }
- return wordCounts;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment