Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- private static readonly ConcurrentDictionary<string, int> wordCounts = new ConcurrentDictionary<string, int>();
- 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);
- // Process files using Parallel.ForEach
- Parallel.ForEach(files, (filePath) =>
- {
- ProcessFile(filePath, minWordLength);
- });
- // Get top-10 most frequent words
- var topWords = wordCounts
- .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 void ProcessFile(string filePath, int minWordLength)
- {
- try
- {
- var text = File.ReadAllText(filePath);
- var words = text.Split(new[] { ' ', '\r', '\n', '\t', '.', ',', ';', '!', '?' }, StringSplitOptions.RemoveEmptyEntries);
- foreach (var word in words)
- {
- if (word.Length >= minWordLength)
- {
- var normalizedWord = word.ToLowerInvariant();
- wordCounts.AddOrUpdate(normalizedWord, 1, (key, oldValue) => oldValue + 1);
- }
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine($"Error processing file {filePath}: {ex.Message}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment