Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions TestProcessing-1/adafdsa
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

namespace TestProcessing_1
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please enter the file path:");
var filePath = Console.ReadLine();

if (string.IsNullOrWhiteSpace(filePath) || !File.Exists(filePath))
{
Console.WriteLine("Invalid file path. Please try again.");
return;
}

try
{
var wordCount = CountWordsInFile(filePath);
PrintResults(wordCount);
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}

private static Dictionary<string, int> CountWordsInFile(string filePath)
{
var wordCount = new Dictionary<string, int>();
var lines = File.ReadAllLines(filePath);

foreach (var line in lines)
{
var cleanedLine = Regex.Replace(line, @"[^\w\s]", "").ToLower();
var words = cleanedLine.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

foreach (var word in words)
{
if (wordCount.ContainsKey(word))
wordCount[word]++;
else
wordCount[word] = 1;
}
}

return wordCount;
}

private static void PrintResults(Dictionary<string, int> wordCount)
{
Console.WriteLine("Done");
foreach (var entry in wordCount)
{
Console.WriteLine($"{entry.Key}: {entry.Value}");
}
Console.WriteLine($"Unique words count: {wordCount.Count}");
}
}
}