General optimization and fixing.
This commit is contained in:
parent
045a07fc0a
commit
f247f531fe
@ -1,40 +1,31 @@
|
||||
using System.Diagnostics;
|
||||
using System.Collections.Concurrent;
|
||||
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<string?> predictedWord = predictor.PredictNextWord(inputWord.ToLower());
|
||||
*/
|
||||
|
||||
const string filePath = "/home/skingging/Documents/Projects/CSharp/AI-Tarpit/Tarpit/words/wordlist.txt";
|
||||
WordUtils utils = new();
|
||||
Dictionary<string, List<string>> words = WordUtils.LoadData(filePath);
|
||||
|
||||
// TODO: Create a thread that continuously fills the queue, that can later be consumed by the server utils.
|
||||
ConcurrentQueue<string> tags = new();
|
||||
Thread createTags = new(() => TagUtils.CreateTags(tags, words));
|
||||
createTags.Start();
|
||||
|
||||
const string baseAddress = "http://localhost:10000/";
|
||||
|
||||
|
||||
HttpListener listener = new();
|
||||
listener.Prefixes.Add(baseAddress);
|
||||
|
||||
ServerUtils serverUtils = new();
|
||||
|
||||
try
|
||||
while (true)
|
||||
{
|
||||
HttpListener listener = new();
|
||||
listener.Prefixes.Add(baseAddress);
|
||||
listener.Start();
|
||||
Console.WriteLine("Listening on " + baseAddress);
|
||||
|
||||
while (true)
|
||||
{
|
||||
HttpListenerContext context = await listener.GetContextAsync();
|
||||
await serverUtils.ProcessRequestAsync(context);
|
||||
ServerUtils serverUtils = new();
|
||||
|
||||
Thread thread = new(() => _ = serverUtils.Serve(context, tags));
|
||||
thread.Start();
|
||||
//await serverUtils.Serve(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();
|
||||
}
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
|
||||
@ -5,53 +6,69 @@ namespace Tarpit;
|
||||
|
||||
public class ServerUtils
|
||||
{
|
||||
private int _step = 0;
|
||||
private const string BoilerplateHead = "<!DOCTYPE html>\n\n<html lang=\"en\">\n\n<head>\n\n <meta charset=\"UTF-8\" />\n\n <title>Hello, world!</title>\n\n <meta name=\"viewport\" content=\"width=device-width,initial-scale=1\" />\n\n <meta name=\"description\" content=\"\" />\n\n <link rel=\"icon\" href=\"favicon.png\">\n\n</head>";
|
||||
private const string BoilerplateBodyStart = "<body>";
|
||||
private const string BoilerplateEnd = "</body>\n\n</html>";
|
||||
|
||||
public async Task ProcessRequestAsync(HttpListenerContext context)
|
||||
public async Task Serve(HttpListenerContext context, ConcurrentQueue<string> tags)
|
||||
{
|
||||
HttpListenerResponse response = context.Response;
|
||||
|
||||
response.ContentType = "text/html; charset=utf-8";
|
||||
|
||||
Stream outputStream = response.OutputStream;
|
||||
try
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
await ProcessRequestAsync(context, tags);
|
||||
}
|
||||
}
|
||||
catch (HttpListenerException ex)
|
||||
{
|
||||
Console.WriteLine(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ProcessRequestAsync(HttpListenerContext context, ConcurrentQueue<string> tags)
|
||||
{
|
||||
context.Response.ContentType = "text/html; charset=utf-8";
|
||||
Stream outputStream = context.Response.OutputStream;
|
||||
|
||||
int i = 0;
|
||||
string tag;
|
||||
while (true)
|
||||
{
|
||||
byte[] buffer = Encoding.UTF8.GetBytes(GetNextHtmlChunk());
|
||||
await outputStream.WriteAsync(buffer, 0, buffer.Length);
|
||||
await outputStream.FlushAsync();
|
||||
if (tags.IsEmpty)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (i == 0)
|
||||
{
|
||||
byte[] buffer = Encoding.UTF8.GetBytes(BoilerplateHead);
|
||||
await outputStream.WriteAsync(buffer, 0, buffer.Length);
|
||||
await outputStream.FlushAsync();
|
||||
}
|
||||
|
||||
if (i == 1)
|
||||
{
|
||||
byte[] buffer = Encoding.UTF8.GetBytes(BoilerplateBodyStart);
|
||||
await outputStream.WriteAsync(buffer, 0, buffer.Length);
|
||||
await outputStream.FlushAsync();
|
||||
}
|
||||
|
||||
if (tags.TryDequeue(out tag!))
|
||||
{
|
||||
byte[] buffer = Encoding.UTF8.GetBytes(tag);
|
||||
await outputStream.WriteAsync(buffer, 0, buffer.Length);
|
||||
await outputStream.FlushAsync();
|
||||
}
|
||||
|
||||
i++;
|
||||
|
||||
Task.Delay(250).Wait();
|
||||
}
|
||||
|
||||
byte[] temp = Encoding.UTF8.GetBytes(BoilerplateEnd);
|
||||
await outputStream.WriteAsync(temp, 0, temp.Length);
|
||||
await outputStream.FlushAsync();
|
||||
|
||||
outputStream.Close();
|
||||
}
|
||||
|
||||
string GetNextHtmlChunk()
|
||||
{
|
||||
Task.Delay(1).Wait();
|
||||
|
||||
switch (_step)
|
||||
{
|
||||
case 0:
|
||||
_step++;
|
||||
return "<html><head><title>Progressive HTML</title></head><body><h1>Starting...</h1>";
|
||||
case 1:
|
||||
_step++;
|
||||
return "<p>This is the first paragraph.</p>";
|
||||
case 2:
|
||||
_step++;
|
||||
return "<p>Here's another paragraph with some <strong>bold text</strong>.</p>";
|
||||
case 3:
|
||||
_step++;
|
||||
return "<ul><li>Item 1</li><li>Item 2</li></ul>";
|
||||
case 4:
|
||||
_step++;
|
||||
return "<img src=\"https://via.placeholder.com/150\" alt=\"Placeholder Image\">";
|
||||
default:
|
||||
_step = 0;
|
||||
return "</body></html>";
|
||||
}
|
||||
}
|
||||
}
|
51
Tarpit/Tarpit/TagUtils.cs
Normal file
51
Tarpit/Tarpit/TagUtils.cs
Normal file
@ -0,0 +1,51 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Text;
|
||||
|
||||
namespace Tarpit;
|
||||
|
||||
public static class TagUtils
|
||||
{
|
||||
public static void CreateTags(ConcurrentQueue<string> tags, Dictionary<string, List<string>> words)
|
||||
{
|
||||
string[] startWords = ["Andersen", "Denmark", "The", "Earth", "Who", "His", "Her", "Following", "At", "A"];
|
||||
List<string> tagsList = [];
|
||||
StringBuilder sb = new();
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (tags.Count >= 100)
|
||||
{
|
||||
Thread.Sleep(100);
|
||||
continue;
|
||||
}
|
||||
|
||||
tagsList.Add(startWords[Random.Shared.Next(startWords.Length - 1)]);
|
||||
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
List<string> temp = WordUtils.PredictNextWords(tagsList[i], words);
|
||||
|
||||
if (temp.Count != 0)
|
||||
{
|
||||
tagsList.Add(temp[Random.Shared.Next(temp.Count - 1)]);
|
||||
}
|
||||
else
|
||||
{
|
||||
tagsList.Add(startWords[Random.Shared.Next(startWords.Length - 1)]);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < tagsList.Count; i++)
|
||||
{
|
||||
sb.Append(tagsList[i]);
|
||||
sb.Append(' ');
|
||||
}
|
||||
|
||||
tags.Enqueue($"<p>{sb}</p>");
|
||||
|
||||
sb.Clear();
|
||||
|
||||
tagsList.Clear();
|
||||
}
|
||||
}
|
||||
}
|
@ -5,6 +5,7 @@
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<PublishAot>true</PublishAot>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -1,75 +0,0 @@
|
||||
namespace Tarpit;
|
||||
|
||||
public class WordPredictor
|
||||
{
|
||||
private Dictionary<string, List<string>> _wordFrequencies = new();
|
||||
|
||||
public WordPredictor(string filePath)
|
||||
{
|
||||
LoadData(filePath);
|
||||
}
|
||||
|
||||
private void LoadData(string filePath)
|
||||
{
|
||||
_wordFrequencies = new();
|
||||
|
||||
try
|
||||
{
|
||||
using StreamReader reader = new(filePath);
|
||||
string? previousWord = null;
|
||||
|
||||
while (reader.ReadLine() is { } line)
|
||||
{
|
||||
string[] words = line.Split(new char[] { ' ', '.', ',', '!', '?', ';', ':', '(', ')', '\n', '\r', '\t' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
foreach (string word in words)
|
||||
{
|
||||
if (previousWord != null)
|
||||
{
|
||||
if (!_wordFrequencies.ContainsKey(previousWord))
|
||||
{
|
||||
_wordFrequencies[previousWord] = [];
|
||||
}
|
||||
_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 List<string?> PredictNextWord(string inputWord)
|
||||
{
|
||||
if (!_wordFrequencies.ContainsKey(inputWord.ToLower()))
|
||||
{
|
||||
return ["Word not found in training data."];
|
||||
}
|
||||
|
||||
List<string> nextWords = _wordFrequencies[inputWord.ToLower()];
|
||||
|
||||
List<string?> 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;
|
||||
}
|
||||
}
|
63
Tarpit/Tarpit/WordUtils.cs
Normal file
63
Tarpit/Tarpit/WordUtils.cs
Normal file
@ -0,0 +1,63 @@
|
||||
namespace Tarpit;
|
||||
|
||||
public class WordUtils
|
||||
{
|
||||
public static Dictionary<string, List<string>> LoadData(string filePath)
|
||||
{
|
||||
Dictionary<string, List<string>> wordFrequencies = new();
|
||||
|
||||
using StreamReader reader = new(filePath);
|
||||
string? previousWord = null;
|
||||
|
||||
while (reader.ReadLine() is { } line)
|
||||
{
|
||||
string[] words = line.Split(new char[] { ' ', '.', ',', '!', '?', ';', ':', '(', ')', '\n', '\r', '\t' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
for (int i = 0; i < words.Length; i++)
|
||||
{
|
||||
string word = words[i];
|
||||
if (previousWord != null)
|
||||
{
|
||||
if (!wordFrequencies.ContainsKey(previousWord))
|
||||
{
|
||||
wordFrequencies[previousWord] = [];
|
||||
}
|
||||
|
||||
wordFrequencies[previousWord].Add(word);
|
||||
}
|
||||
|
||||
previousWord = word;
|
||||
}
|
||||
}
|
||||
|
||||
return wordFrequencies;
|
||||
}
|
||||
|
||||
public static List<string> PredictNextWords(string inputWord, Dictionary<string, List<string>> wordFrequencies)
|
||||
{
|
||||
if (!wordFrequencies.ContainsKey(inputWord.ToLower()))
|
||||
{
|
||||
return [inputWord];
|
||||
}
|
||||
|
||||
List<string> nextWords = wordFrequencies[inputWord.ToLower()];
|
||||
|
||||
List<string> words = [];
|
||||
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
string? mostFrequentWord = nextWords
|
||||
.GroupBy(w => w)
|
||||
.OrderByDescending(g => g.Count())
|
||||
.ElementAtOrDefault(i)
|
||||
?.Key;
|
||||
|
||||
if (!string.IsNullOrEmpty(mostFrequentWord))
|
||||
{
|
||||
words.Add(mostFrequentWord);
|
||||
}
|
||||
}
|
||||
|
||||
return words;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user