Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace WordFrequencyFinder
- {
- class Program
- {
- static void Main(string[] args)
- {
- if (args.Length < 2)
- {
- Console.WriteLine("Usage: WordFrequencyFinder <directory path> <min word length>");
- return;
- }
- string directoryPath = args[0];
- if (!int.TryParse(args[1], out int minWordLength))
- {
- Console.WriteLine("Invalid minimum word length.");
- return;
- }
- if (!Directory.Exists(directoryPath))
- {
- Console.WriteLine("Directory does not exist.");
- return;
- }
- var wordFrequency = GetWordFrequencies(directoryPath, minWordLength);
- var topWords = wordFrequency.OrderByDescending(kvp => kvp.Value).Take(10);
- Console.WriteLine("Top-10 most used words:");
- foreach (var word in topWords)
- {
- Console.WriteLine($"{word.Key}: {word.Value}");
- }
- }
- static ConcurrentDictionary<string, int> GetWordFrequencies(string directoryPath, int minWordLength)
- {
- var wordFrequency = new ConcurrentDictionary<string, int>(-1, 100);
- var files = Directory.GetFiles(directoryPath, "*.txt");
- var resetEvents = new ManualResetEvent[files.Length];
- for (int i = 0; i < files.Length; i++)
- {
- resetEvents[i] = new ManualResetEvent(false);
- var state = new ThreadPoolState
- {
- FilePath = files[i],
- MinWordLength = minWordLength,
- WordFrequency = wordFrequency,
- ResetEvent = resetEvents[i]
- };
- ThreadPool.QueueUserWorkItem(ProcessFile, state);
- }
- WaitHandle.WaitAll(resetEvents);
- return wordFrequency;
- }
- static void ProcessFile(object state)
- {
- var threadState = (ThreadPoolState)state;
- var content = File.ReadAllText(threadState.FilePath);
- var words = Regex.Split(content.ToLowerInvariant(), @"\W+")
- .Where(word => word.Length >= threadState.MinWordLength);
- foreach (var word in words)
- {
- threadState.WordFrequency.AddOrUpdate(word, 1, (key, oldValue) => oldValue + 1);
- }
- threadState.ResetEvent.Set();
- }
- class ThreadPoolState
- {
- public required string FilePath { get; set; }
- public int MinWordLength { get; set; }
- public required ConcurrentDictionary<string, int> WordFrequency { get; set; }
- public required ManualResetEvent ResetEvent { get; set; }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment