Compare commits

..

5 Commits

Author SHA1 Message Date
045a07fc0a Added boilerplate 2025-04-17 10:28:34 +02:00
a8fa21ad11 Changed gitignore hopefully for the last time 2025-04-17 10:28:23 +02:00
ca6a123467 Changed gitignore 2025-04-17 10:27:40 +02:00
c0458a4d51 Changed gitignore 2025-04-17 10:26:58 +02:00
6282edcc13 Changed gitignore 2025-04-17 10:25:48 +02:00
10 changed files with 234 additions and 0 deletions

8
.gitignore vendored
View File

@ -77,3 +77,11 @@ fabric.properties
# Android studio 3.1+ serialized cache file # Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser .idea/caches/build_file_checksums.ser
*/bin
*/obj
*/bin/*
*/obj/*
*obj/
*bin/
wordlist*
*DotSettings*

View File

@ -0,0 +1,10 @@
# Default ignored files
/shelf/
/workspace.xml
# Rider ignored files
/modules.xml
/contentModel.xml
/projectSettingsUpdater.xml
/.idea.Tarpit.iml
# Editor-based HTTP Client requests
/httpRequests/

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" addBOMForNewFiles="with BOM under Windows, with no BOM otherwise" />
</project>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="UserContentModel">
<attachedFolders />
<explicitIncludes />
<explicitExcludes />
</component>
</project>

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

16
Tarpit/Tarpit.sln Normal file
View File

@ -0,0 +1,16 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tarpit", "Tarpit\Tarpit.csproj", "{9AABA4AF-ECB0-4307-B4D3-201A10A19F31}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{9AABA4AF-ECB0-4307-B4D3-201A10A19F31}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9AABA4AF-ECB0-4307-B4D3-201A10A19F31}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9AABA4AF-ECB0-4307-B4D3-201A10A19F31}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9AABA4AF-ECB0-4307-B4D3-201A10A19F31}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

40
Tarpit/Tarpit/Program.cs Normal file
View File

@ -0,0 +1,40 @@
using System.Diagnostics;
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 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();
}

View File

@ -0,0 +1,57 @@
using System.Net;
using System.Text;
namespace Tarpit;
public class ServerUtils
{
private int _step = 0;
public async Task ProcessRequestAsync(HttpListenerContext context)
{
HttpListenerResponse response = context.Response;
response.ContentType = "text/html; charset=utf-8";
Stream outputStream = response.OutputStream;
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 "<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>";
}
}
}

View File

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,75 @@
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;
}
}