Testing.
This commit is contained in:
parent
f247f531fe
commit
e7a549a196
53
Tarpit/Tarpit/DatabaseUtils.cs
Normal file
53
Tarpit/Tarpit/DatabaseUtils.cs
Normal 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();
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
}*/
|
@ -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);
|
||||
|
@ -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 [];
|
||||
}
|
||||
}
|
@ -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>
|
||||
|
@ -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"];
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user