From 045a07fc0a5ae1967beee8f9371d18fcb70862a8 Mon Sep 17 00:00:00 2001 From: rasmus Date: Thu, 17 Apr 2025 10:28:34 +0200 Subject: [PATCH] Added boilerplate --- Tarpit/.idea/.idea.Tarpit/.idea/.gitignore | 10 +++ Tarpit/.idea/.idea.Tarpit/.idea/encodings.xml | 4 + .../.idea/.idea.Tarpit/.idea/indexLayout.xml | 8 ++ Tarpit/.idea/.idea.Tarpit/.idea/vcs.xml | 6 ++ Tarpit/Tarpit.sln | 16 ++++ Tarpit/Tarpit/Program.cs | 40 +++++++++ Tarpit/Tarpit/ServerUtils.cs | 66 +++++++++----- Tarpit/Tarpit/Tarpit.csproj | 10 +++ Tarpit/Tarpit/WordPredictor.cs | 87 ++++++++----------- 9 files changed, 174 insertions(+), 73 deletions(-) create mode 100644 Tarpit/.idea/.idea.Tarpit/.idea/.gitignore create mode 100644 Tarpit/.idea/.idea.Tarpit/.idea/encodings.xml create mode 100644 Tarpit/.idea/.idea.Tarpit/.idea/indexLayout.xml create mode 100644 Tarpit/.idea/.idea.Tarpit/.idea/vcs.xml create mode 100644 Tarpit/Tarpit.sln create mode 100644 Tarpit/Tarpit/Program.cs create mode 100644 Tarpit/Tarpit/Tarpit.csproj diff --git a/Tarpit/.idea/.idea.Tarpit/.idea/.gitignore b/Tarpit/.idea/.idea.Tarpit/.idea/.gitignore new file mode 100644 index 0000000..b3d453c --- /dev/null +++ b/Tarpit/.idea/.idea.Tarpit/.idea/.gitignore @@ -0,0 +1,10 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Rider ignored files +/modules.xml +/contentModel.xml +/projectSettingsUpdater.xml +/.idea.Tarpit.iml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/Tarpit/.idea/.idea.Tarpit/.idea/encodings.xml b/Tarpit/.idea/.idea.Tarpit/.idea/encodings.xml new file mode 100644 index 0000000..df87cf9 --- /dev/null +++ b/Tarpit/.idea/.idea.Tarpit/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Tarpit/.idea/.idea.Tarpit/.idea/indexLayout.xml b/Tarpit/.idea/.idea.Tarpit/.idea/indexLayout.xml new file mode 100644 index 0000000..7b08163 --- /dev/null +++ b/Tarpit/.idea/.idea.Tarpit/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Tarpit/.idea/.idea.Tarpit/.idea/vcs.xml b/Tarpit/.idea/.idea.Tarpit/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/Tarpit/.idea/.idea.Tarpit/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Tarpit/Tarpit.sln b/Tarpit/Tarpit.sln new file mode 100644 index 0000000..ec2eca6 --- /dev/null +++ b/Tarpit/Tarpit.sln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tarpit", "Tarpit\Tarpit.csproj", "{9AABA4AF-ECB0-4307-B4D3-201A10A19F31}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {9AABA4AF-ECB0-4307-B4D3-201A10A19F31}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9AABA4AF-ECB0-4307-B4D3-201A10A19F31}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9AABA4AF-ECB0-4307-B4D3-201A10A19F31}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9AABA4AF-ECB0-4307-B4D3-201A10A19F31}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/Tarpit/Tarpit/Program.cs b/Tarpit/Tarpit/Program.cs new file mode 100644 index 0000000..69ed1e8 --- /dev/null +++ b/Tarpit/Tarpit/Program.cs @@ -0,0 +1,40 @@ +using System.Diagnostics; +using System.Net; +using Tarpit; +/* +const string filePath = "/home/skingging/Documents/Projects/CSharp/AI-Tarpit/Tarpit/words/wordlist.txt"; +WordPredictor predictor = new(filePath); +string inputWord = Console.ReadLine() ?? string.Empty; +List predictedWord = predictor.PredictNextWord(inputWord.ToLower()); +*/ + + +const string baseAddress = "http://localhost:10000/"; + + +HttpListener listener = new(); +listener.Prefixes.Add(baseAddress); + +ServerUtils serverUtils = new(); + +try +{ + listener.Start(); + Console.WriteLine("Listening on " + baseAddress); + + while (true) + { + HttpListenerContext context = await listener.GetContextAsync(); + await serverUtils.ProcessRequestAsync(context); + } +} +catch (HttpListenerException ex) +{ + Console.WriteLine("Error starting or running the listener: " + ex.Message); + Console.WriteLine("Make sure you have the necessary permissions (requires running as administrator sometimes)"); +} +finally +{ + listener.Stop(); + listener.Close(); +} diff --git a/Tarpit/Tarpit/ServerUtils.cs b/Tarpit/Tarpit/ServerUtils.cs index ed03bde..cef9691 100644 --- a/Tarpit/Tarpit/ServerUtils.cs +++ b/Tarpit/Tarpit/ServerUtils.cs @@ -3,33 +3,55 @@ using System.Text; namespace Tarpit; -public static class ServerUtils +public class ServerUtils { - public static async Task ProcessRequestAsync(HttpListenerContext context) + private int _step = 0; + + public async Task ProcessRequestAsync(HttpListenerContext context) { - HttpListenerRequest request = context.Request; HttpListenerResponse response = context.Response; - - // Determine the next HTML chunk to send - const string htmlChunk = "Progressive HTML

Starting...

"; - - // Set the content type - response.ContentType = "text/html"; - - // Write the HTML chunk to the response stream - byte[] buffer = Encoding.UTF8.GetBytes(htmlChunk); - response.ContentLength64 = buffer.Length; - + + response.ContentType = "text/html; charset=utf-8"; + Stream outputStream = response.OutputStream; - await outputStream.WriteAsync(buffer, 0, buffer.Length); - - // Close the output stream + + int i = 0; + while (true) + { + byte[] buffer = Encoding.UTF8.GetBytes(GetNextHtmlChunk()); + await outputStream.WriteAsync(buffer, 0, buffer.Length); + await outputStream.FlushAsync(); + + i++; + } + outputStream.Close(); } - - - - - + string GetNextHtmlChunk() + { + Task.Delay(1).Wait(); + + switch (_step) + { + case 0: + _step++; + return "Progressive HTML

Starting...

"; + case 1: + _step++; + return "

This is the first paragraph.

"; + case 2: + _step++; + return "

Here's another paragraph with some bold text.

"; + case 3: + _step++; + return ""; + case 4: + _step++; + return "\"Placeholder"; + default: + _step = 0; + return ""; + } + } } \ No newline at end of file diff --git a/Tarpit/Tarpit/Tarpit.csproj b/Tarpit/Tarpit/Tarpit.csproj new file mode 100644 index 0000000..85b4959 --- /dev/null +++ b/Tarpit/Tarpit/Tarpit.csproj @@ -0,0 +1,10 @@ + + + + Exe + net9.0 + enable + enable + + + diff --git a/Tarpit/Tarpit/WordPredictor.cs b/Tarpit/Tarpit/WordPredictor.cs index 6693d23..39142d0 100644 --- a/Tarpit/Tarpit/WordPredictor.cs +++ b/Tarpit/Tarpit/WordPredictor.cs @@ -2,7 +2,7 @@ namespace Tarpit; public class WordPredictor { - private Dictionary> wordFrequencies; + private Dictionary> _wordFrequencies = new(); public WordPredictor(string filePath) { @@ -11,31 +11,28 @@ public class WordPredictor private void LoadData(string filePath) { - wordFrequencies = new Dictionary>(); - + _wordFrequencies = new(); + try { - using (StreamReader reader = new StreamReader(filePath)) + using StreamReader reader = new(filePath); + string? previousWord = null; + + while (reader.ReadLine() is { } line) { - string line; - string previousWord = null; - - while ((line = reader.ReadLine()) != null) + string[] words = line.Split(new char[] { ' ', '.', ',', '!', '?', ';', ':', '(', ')', '\n', '\r', '\t' }, StringSplitOptions.RemoveEmptyEntries); + + foreach (string word in words) { - string[] words = line.ToLower().Split(new char[] { ' ', '.', ',', '!', '?', ';', ':', '\n', '\r', '\t' }, StringSplitOptions.RemoveEmptyEntries); - - foreach (string word in words) + if (previousWord != null) { - if (previousWord != null) + if (!_wordFrequencies.ContainsKey(previousWord)) { - if (!wordFrequencies.ContainsKey(previousWord)) - { - wordFrequencies[previousWord] = new List(); - } - wordFrequencies[previousWord].Add(word); + _wordFrequencies[previousWord] = []; } - previousWord = word; + _wordFrequencies[previousWord].Add(word); } + previousWord = word; } } } @@ -50,41 +47,29 @@ public class WordPredictor Environment.Exit(1); } } - - public string PredictNextWord(string inputWord) + + public List PredictNextWord(string inputWord) { - if (!wordFrequencies.ContainsKey(inputWord.ToLower())) + if (!_wordFrequencies.ContainsKey(inputWord.ToLower())) { - return "Word not found in training data."; + return ["Word not found in training data."]; } - - List nextWords = wordFrequencies[inputWord.ToLower()]; - - // Find the most frequent next word - string mostFrequentWord = nextWords - .GroupBy(w => w) - .OrderByDescending(g => g.Count()) - .First() - .Key; - - return mostFrequentWord; - } - - - public static void Main(string[] args) - { - // Replace "your_text_file.txt" with the path to your text file. - string filePath = "your_text_file.txt"; - - WordPredictor predictor = new WordPredictor(filePath); - - Console.WriteLine("Enter a word:"); - string inputWord = Console.ReadLine(); - - string predictedWord = predictor.PredictNextWord(inputWord); - - Console.WriteLine($"The most common word after '{inputWord}' is: '{predictedWord}'"); - - Console.ReadKey(); // Keep the console window open until a key is pressed + + List nextWords = _wordFrequencies[inputWord.ToLower()]; + + List words = []; + + for (int i = 0; i < 10; i++) + { + string? mostFrequentWord = nextWords + .GroupBy(w => w) + .OrderByDescending(g => g.Count()) + .ElementAtOrDefault(i) + ?.Key; + + words.Add(mostFrequentWord); + } + + return words; } } \ No newline at end of file