diff --git a/.gitignore b/.gitignore index 57940fd..22ad34e 100644 --- a/.gitignore +++ b/.gitignore @@ -77,3 +77,7 @@ fabric.properties # Android studio 3.1+ serialized cache file .idea/caches/build_file_checksums.ser +*/bin +*/obj +*/bin/* +*/obj/* diff --git a/Tarpit/Tarpit/ServerUtils.cs b/Tarpit/Tarpit/ServerUtils.cs new file mode 100644 index 0000000..ed03bde --- /dev/null +++ b/Tarpit/Tarpit/ServerUtils.cs @@ -0,0 +1,35 @@ +using System.Net; +using System.Text; + +namespace Tarpit; + +public static class ServerUtils +{ + public static 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; + + Stream outputStream = response.OutputStream; + await outputStream.WriteAsync(buffer, 0, buffer.Length); + + // Close the output stream + outputStream.Close(); + } + + + + + + +} \ No newline at end of file diff --git a/Tarpit/Tarpit/WordPredictor.cs b/Tarpit/Tarpit/WordPredictor.cs new file mode 100644 index 0000000..6693d23 --- /dev/null +++ b/Tarpit/Tarpit/WordPredictor.cs @@ -0,0 +1,90 @@ +namespace Tarpit; + +public class WordPredictor +{ + private Dictionary> wordFrequencies; + + public WordPredictor(string filePath) + { + LoadData(filePath); + } + + private void LoadData(string filePath) + { + wordFrequencies = new Dictionary>(); + + try + { + using (StreamReader reader = new StreamReader(filePath)) + { + string line; + string previousWord = null; + + while ((line = reader.ReadLine()) != null) + { + string[] words = line.ToLower().Split(new char[] { ' ', '.', ',', '!', '?', ';', ':', '\n', '\r', '\t' }, StringSplitOptions.RemoveEmptyEntries); + + foreach (string word in words) + { + if (previousWord != null) + { + if (!wordFrequencies.ContainsKey(previousWord)) + { + wordFrequencies[previousWord] = new List(); + } + wordFrequencies[previousWord].Add(word); + } + previousWord = word; + } + } + } + } + catch (FileNotFoundException) + { + Console.WriteLine($"Error: File not found at {filePath}"); + Environment.Exit(1); // Exit the program if the file isn't found. Important! + } + catch (Exception ex) + { + Console.WriteLine($"An error occurred while reading the file: {ex.Message}"); + Environment.Exit(1); + } + } + + public string PredictNextWord(string inputWord) + { + if (!wordFrequencies.ContainsKey(inputWord.ToLower())) + { + 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 + } +} \ No newline at end of file