Compare commits
5 Commits
3e4a3949f8
...
045a07fc0a
Author | SHA1 | Date | |
---|---|---|---|
045a07fc0a | |||
a8fa21ad11 | |||
ca6a123467 | |||
c0458a4d51 | |||
6282edcc13 |
8
.gitignore
vendored
8
.gitignore
vendored
@ -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*
|
||||||
|
10
Tarpit/.idea/.idea.Tarpit/.idea/.gitignore
vendored
Normal file
10
Tarpit/.idea/.idea.Tarpit/.idea/.gitignore
vendored
Normal 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/
|
4
Tarpit/.idea/.idea.Tarpit/.idea/encodings.xml
Normal file
4
Tarpit/.idea/.idea.Tarpit/.idea/encodings.xml
Normal 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>
|
8
Tarpit/.idea/.idea.Tarpit/.idea/indexLayout.xml
Normal file
8
Tarpit/.idea/.idea.Tarpit/.idea/indexLayout.xml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="UserContentModel">
|
||||||
|
<attachedFolders />
|
||||||
|
<explicitIncludes />
|
||||||
|
<explicitExcludes />
|
||||||
|
</component>
|
||||||
|
</project>
|
6
Tarpit/.idea/.idea.Tarpit/.idea/vcs.xml
Normal file
6
Tarpit/.idea/.idea.Tarpit/.idea/vcs.xml
Normal 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
16
Tarpit/Tarpit.sln
Normal 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
40
Tarpit/Tarpit/Program.cs
Normal 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();
|
||||||
|
}
|
57
Tarpit/Tarpit/ServerUtils.cs
Normal file
57
Tarpit/Tarpit/ServerUtils.cs
Normal 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>";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
10
Tarpit/Tarpit/Tarpit.csproj
Normal file
10
Tarpit/Tarpit/Tarpit.csproj
Normal 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>
|
75
Tarpit/Tarpit/WordPredictor.cs
Normal file
75
Tarpit/Tarpit/WordPredictor.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user