This commit is contained in:
Rasmus Rasmussen 2025-04-21 18:45:23 +02:00
parent f247f531fe
commit e7a549a196
6 changed files with 123 additions and 48 deletions

View File

@ -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();
}
}

View File

@ -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<string, List<string>> 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<string> 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);
}
}
}*/

View File

@ -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);

View File

@ -3,25 +3,27 @@ using System.Text;
namespace Tarpit;
public static class TagUtils
public class TagUtils
{
public static void CreateTags(ConcurrentQueue<string> tags, Dictionary<string, List<string>> words)
public void CreateTags(string wordListPath, DatabaseUtils database)
{
string[] startWords = ["Andersen", "Denmark", "The", "Earth", "Who", "His", "Her", "Following", "At", "A"];
string[] wordLists = Directory.GetFiles(wordListPath, "*.txt", SearchOption.TopDirectoryOnly);
for (int j = 0; j < wordLists.Length; j++)
{
Dictionary<string, List<string>> words = WordUtils.LoadData(wordLists[j]);
string[] startWords = WordUtils.CreateStartwords(wordListPath);
List<string> tagsList = [];
StringBuilder sb = new();
while (true)
for (int k = 0; k < 1000; k++)
{
if (tags.Count >= 100)
{
Thread.Sleep(100);
continue;
}
tagsList.Add(startWords[Random.Shared.Next(startWords.Length - 1)]);
for (int i = 0; i < 20; i++)
for (int i = 0; i < 30; i++)
{
List<string> temp = WordUtils.PredictNextWords(tagsList[i], words);
@ -33,19 +35,28 @@ public static class TagUtils
{
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>");
database.InsertTags(database.ConnectionStrings[j], sb.ToString());
sb.Clear();
tagsList.Clear();
//Thread.Sleep(250);
}
}
}
}
public string[] CreatePage(int paragraphs, bool header = true, int headings = 0, bool footer = true, int imagesAmount = 0)
{
return [];
}
}

View File

@ -5,7 +5,11 @@
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishAot>true</PublishAot>
<PublishAot>false</PublishAot>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Data.Sqlite" Version="9.0.4" />
</ItemGroup>
</Project>

View File

@ -1,6 +1,6 @@
namespace Tarpit;
public class WordUtils
public static class WordUtils
{
public static Dictionary<string, List<string>> LoadData(string filePath)
{
@ -37,7 +37,7 @@ public class WordUtils
{
if (!wordFrequencies.ContainsKey(inputWord.ToLower()))
{
return [inputWord];
return [];
}
List<string> nextWords = wordFrequencies[inputWord.ToLower()];
@ -60,4 +60,11 @@ public class WordUtils
return words;
}
public static string[] CreateStartwords(string path)
{
return ["The"];
}
}