From e7a549a1965cef63b1498b54409b51699058c4a6 Mon Sep 17 00:00:00 2001 From: rasmus Date: Mon, 21 Apr 2025 18:45:23 +0200 Subject: [PATCH] Testing. --- Tarpit/Tarpit/DatabaseUtils.cs | 53 ++++++++++++++++++++++++ Tarpit/Tarpit/Program.cs | 20 ++++----- Tarpit/Tarpit/ServerUtils.cs | 6 +-- Tarpit/Tarpit/TagUtils.cs | 75 +++++++++++++++++++--------------- Tarpit/Tarpit/Tarpit.csproj | 6 ++- Tarpit/Tarpit/WordUtils.cs | 11 ++++- 6 files changed, 123 insertions(+), 48 deletions(-) create mode 100644 Tarpit/Tarpit/DatabaseUtils.cs diff --git a/Tarpit/Tarpit/DatabaseUtils.cs b/Tarpit/Tarpit/DatabaseUtils.cs new file mode 100644 index 0000000..c7a97ff --- /dev/null +++ b/Tarpit/Tarpit/DatabaseUtils.cs @@ -0,0 +1,53 @@ +using Microsoft.Data.Sqlite; + +namespace Tarpit; + +public class DatabaseUtils +{ + public readonly string[] ConnectionStrings; + + private const string DefaultStatement = "PRAGMA synchronous = OFF; PRAGMA temp_store = MEMORY;" + + " PRAGMA journal_mode = MEMORY; PRAGMA foreign_keys = off;" + + " INSERT INTO Tags (Paragraph)" + + " VALUES (@paragraph)"; + + public DatabaseUtils(string path) + { + string[] wordLists = Directory.GetFiles(path, "*.txt", SearchOption.TopDirectoryOnly); + + ConnectionStrings = new string[wordLists.Length]; + + for (int i = 0; i < wordLists.Length; i++) + { + FileInfo fileInfo = new(wordLists[i]); + ConnectionStrings[i] = $"Data Source={Directory.GetParent(path)}/Database/{fileInfo.Name.Split('.')[0]}.db"; + + CreateDatabase(ConnectionStrings[i]); + } + } + + public void InsertTags(string tableName, string tag) + { + SqliteConnection connection = new(tableName); + connection.Open(); + + using SqliteCommand command = new(DefaultStatement, connection); + + command.Parameters.AddWithValue("@paragraph", tag); + + _ = command.ExecuteNonQuery(); + + connection.Close(); + } + + private void CreateDatabase(string connectionString) + { + string createStatement = $"CREATE TABLE IF NOT EXISTS Tags (Id INTEGER NOT NULL, Paragraph TEXT NOT NULL, PRIMARY KEY(Id AUTOINCREMENT))"; + + using SqliteConnection connection = new(connectionString); + connection.Open(); + + using SqliteCommand command = new(createStatement, connection); + command.ExecuteNonQuery(); + } +} \ No newline at end of file diff --git a/Tarpit/Tarpit/Program.cs b/Tarpit/Tarpit/Program.cs index d20ab6f..b9fb685 100644 --- a/Tarpit/Tarpit/Program.cs +++ b/Tarpit/Tarpit/Program.cs @@ -2,16 +2,17 @@ using System.Net; using Tarpit; -const string filePath = "/home/skingging/Documents/Projects/CSharp/AI-Tarpit/Tarpit/words/wordlist.txt"; -WordUtils utils = new(); -Dictionary> words = WordUtils.LoadData(filePath); +const string filePath = "/home/skingging/Documents/Projects/CSharp/AI-Tarpit/Tarpit/words"; -// TODO: Create a thread that continuously fills the queue, that can later be consumed by the server utils. -ConcurrentQueue tags = new(); -Thread createTags = new(() => TagUtils.CreateTags(tags, words)); -createTags.Start(); -const string baseAddress = "http://localhost:10000/"; +DatabaseUtils db = new DatabaseUtils(filePath); + +TagUtils tagUtils = new TagUtils(); +tagUtils.CreateTags(filePath, db); + + +/*const string baseAddress = "http://localhost:10000/"; +Console.WriteLine($"Listening on {baseAddress}"); while (true) { @@ -26,6 +27,5 @@ while (true) Thread thread = new(() => _ = serverUtils.Serve(context, tags)); thread.Start(); - //await serverUtils.Serve(context); } -} \ No newline at end of file +}*/ \ No newline at end of file diff --git a/Tarpit/Tarpit/ServerUtils.cs b/Tarpit/Tarpit/ServerUtils.cs index 184f6f5..c21256d 100644 --- a/Tarpit/Tarpit/ServerUtils.cs +++ b/Tarpit/Tarpit/ServerUtils.cs @@ -24,7 +24,7 @@ public class ServerUtils Console.WriteLine(ex.Message); } } - + private async Task ProcessRequestAsync(HttpListenerContext context, ConcurrentQueue tags) { context.Response.ContentType = "text/html; charset=utf-8"; @@ -32,7 +32,7 @@ public class ServerUtils int i = 0; string tag; - while (true) + while (i < 50) { if (tags.IsEmpty) { @@ -62,7 +62,7 @@ public class ServerUtils i++; - Task.Delay(250).Wait(); + Task.Delay(750).Wait(); } byte[] temp = Encoding.UTF8.GetBytes(BoilerplateEnd); diff --git a/Tarpit/Tarpit/TagUtils.cs b/Tarpit/Tarpit/TagUtils.cs index 6dda555..9a3454c 100644 --- a/Tarpit/Tarpit/TagUtils.cs +++ b/Tarpit/Tarpit/TagUtils.cs @@ -3,49 +3,60 @@ using System.Text; namespace Tarpit; -public static class TagUtils +public class TagUtils { - public static void CreateTags(ConcurrentQueue tags, Dictionary> words) + public void CreateTags(string wordListPath, DatabaseUtils database) { - string[] startWords = ["Andersen", "Denmark", "The", "Earth", "Who", "His", "Her", "Following", "At", "A"]; - List tagsList = []; - StringBuilder sb = new(); + string[] wordLists = Directory.GetFiles(wordListPath, "*.txt", SearchOption.TopDirectoryOnly); - while (true) + for (int j = 0; j < wordLists.Length; j++) { - if (tags.Count >= 100) - { - Thread.Sleep(100); - continue; - } + Dictionary> words = WordUtils.LoadData(wordLists[j]); - tagsList.Add(startWords[Random.Shared.Next(startWords.Length - 1)]); + string[] startWords = WordUtils.CreateStartwords(wordListPath); - for (int i = 0; i < 20; i++) + List tagsList = []; + StringBuilder sb = new(); + + for (int k = 0; k < 1000; k++) { - List 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 < 30; i++) + { + List 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)]); + } + + sb.Append(tagsList[i]); + sb.Append(' '); + } + + database.InsertTags(database.ConnectionStrings[j], sb.ToString()); + + sb.Clear(); + + tagsList.Clear(); + + //Thread.Sleep(250); } } - - for (int i = 0; i < tagsList.Count; i++) - { - sb.Append(tagsList[i]); - sb.Append(' '); - } - - tags.Enqueue($"

{sb}

"); - - sb.Clear(); - - tagsList.Clear(); } } + + public string[] CreatePage(int paragraphs, bool header = true, int headings = 0, bool footer = true, int imagesAmount = 0) + { + + + + return []; + } } \ No newline at end of file diff --git a/Tarpit/Tarpit/Tarpit.csproj b/Tarpit/Tarpit/Tarpit.csproj index b52aae9..9a74c77 100644 --- a/Tarpit/Tarpit/Tarpit.csproj +++ b/Tarpit/Tarpit/Tarpit.csproj @@ -5,7 +5,11 @@ net9.0 enable enable - true + false + + + + diff --git a/Tarpit/Tarpit/WordUtils.cs b/Tarpit/Tarpit/WordUtils.cs index 5f795c6..2cb2bfd 100644 --- a/Tarpit/Tarpit/WordUtils.cs +++ b/Tarpit/Tarpit/WordUtils.cs @@ -1,6 +1,6 @@ namespace Tarpit; -public class WordUtils +public static class WordUtils { public static Dictionary> LoadData(string filePath) { @@ -37,7 +37,7 @@ public class WordUtils { if (!wordFrequencies.ContainsKey(inputWord.ToLower())) { - return [inputWord]; + return []; } List nextWords = wordFrequencies[inputWord.ToLower()]; @@ -60,4 +60,11 @@ public class WordUtils return words; } + + public static string[] CreateStartwords(string path) + { + + + return ["The"]; + } } \ No newline at end of file