Added boilerplate
This commit is contained in:
parent
a8fa21ad11
commit
045a07fc0a
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();
|
||||
}
|
@ -3,33 +3,55 @@ using System.Text;
|
||||
|
||||
namespace Tarpit;
|
||||
|
||||
public static class ServerUtils
|
||||
public class ServerUtils
|
||||
{
|
||||
public static async Task ProcessRequestAsync(HttpListenerContext context)
|
||||
private int _step = 0;
|
||||
|
||||
public async Task ProcessRequestAsync(HttpListenerContext context)
|
||||
{
|
||||
HttpListenerRequest request = context.Request;
|
||||
HttpListenerResponse response = context.Response;
|
||||
|
||||
// Determine the next HTML chunk to send
|
||||
const string htmlChunk = "<html><head><title>Progressive HTML</title></head><body><h1>Starting...</h1>";
|
||||
|
||||
// Set the content type
|
||||
response.ContentType = "text/html";
|
||||
|
||||
// Write the HTML chunk to the response stream
|
||||
byte[] buffer = Encoding.UTF8.GetBytes(htmlChunk);
|
||||
response.ContentLength64 = buffer.Length;
|
||||
|
||||
|
||||
response.ContentType = "text/html; charset=utf-8";
|
||||
|
||||
Stream outputStream = response.OutputStream;
|
||||
await outputStream.WriteAsync(buffer, 0, buffer.Length);
|
||||
|
||||
// Close the output stream
|
||||
|
||||
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>
|
@ -2,7 +2,7 @@ namespace Tarpit;
|
||||
|
||||
public class WordPredictor
|
||||
{
|
||||
private Dictionary<string, List<string>> wordFrequencies;
|
||||
private Dictionary<string, List<string>> _wordFrequencies = new();
|
||||
|
||||
public WordPredictor(string filePath)
|
||||
{
|
||||
@ -11,31 +11,28 @@ public class WordPredictor
|
||||
|
||||
private void LoadData(string filePath)
|
||||
{
|
||||
wordFrequencies = new Dictionary<string, List<string>>();
|
||||
|
||||
_wordFrequencies = new();
|
||||
|
||||
try
|
||||
{
|
||||
using (StreamReader reader = new StreamReader(filePath))
|
||||
using StreamReader reader = new(filePath);
|
||||
string? previousWord = null;
|
||||
|
||||
while (reader.ReadLine() is { } line)
|
||||
{
|
||||
string line;
|
||||
string previousWord = null;
|
||||
|
||||
while ((line = reader.ReadLine()) != null)
|
||||
string[] words = line.Split(new char[] { ' ', '.', ',', '!', '?', ';', ':', '(', ')', '\n', '\r', '\t' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
foreach (string word in words)
|
||||
{
|
||||
string[] words = line.ToLower().Split(new char[] { ' ', '.', ',', '!', '?', ';', ':', '\n', '\r', '\t' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
foreach (string word in words)
|
||||
if (previousWord != null)
|
||||
{
|
||||
if (previousWord != null)
|
||||
if (!_wordFrequencies.ContainsKey(previousWord))
|
||||
{
|
||||
if (!wordFrequencies.ContainsKey(previousWord))
|
||||
{
|
||||
wordFrequencies[previousWord] = new List<string>();
|
||||
}
|
||||
wordFrequencies[previousWord].Add(word);
|
||||
_wordFrequencies[previousWord] = [];
|
||||
}
|
||||
previousWord = word;
|
||||
_wordFrequencies[previousWord].Add(word);
|
||||
}
|
||||
previousWord = word;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -50,41 +47,29 @@ public class WordPredictor
|
||||
Environment.Exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
public string PredictNextWord(string inputWord)
|
||||
|
||||
public List<string?> PredictNextWord(string inputWord)
|
||||
{
|
||||
if (!wordFrequencies.ContainsKey(inputWord.ToLower()))
|
||||
if (!_wordFrequencies.ContainsKey(inputWord.ToLower()))
|
||||
{
|
||||
return "Word not found in training data.";
|
||||
return ["Word not found in training data."];
|
||||
}
|
||||
|
||||
List<string> nextWords = wordFrequencies[inputWord.ToLower()];
|
||||
|
||||
// Find the most frequent next word
|
||||
string mostFrequentWord = nextWords
|
||||
.GroupBy(w => w)
|
||||
.OrderByDescending(g => g.Count())
|
||||
.First()
|
||||
.Key;
|
||||
|
||||
return mostFrequentWord;
|
||||
}
|
||||
|
||||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
// Replace "your_text_file.txt" with the path to your text file.
|
||||
string filePath = "your_text_file.txt";
|
||||
|
||||
WordPredictor predictor = new WordPredictor(filePath);
|
||||
|
||||
Console.WriteLine("Enter a word:");
|
||||
string inputWord = Console.ReadLine();
|
||||
|
||||
string predictedWord = predictor.PredictNextWord(inputWord);
|
||||
|
||||
Console.WriteLine($"The most common word after '{inputWord}' is: '{predictedWord}'");
|
||||
|
||||
Console.ReadKey(); // Keep the console window open until a key is pressed
|
||||
|
||||
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