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 HTMLStarting...
";
-
- // 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 HTMLStarting...
";
+ 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 "
";
+ 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