Made program actually ussable
This commit is contained in:
parent
46df18dfa8
commit
94ffa8784d
1
.gitignore
vendored
1
.gitignore
vendored
@ -89,3 +89,4 @@ dkms.conf
|
|||||||
# Misc
|
# Misc
|
||||||
*.txt
|
*.txt
|
||||||
CMakeFiles/
|
CMakeFiles/
|
||||||
|
build/
|
||||||
|
@ -11,10 +11,12 @@ using namespace std;
|
|||||||
|
|
||||||
struct WordUtils {
|
struct WordUtils {
|
||||||
static map<string, map<string, int>> load_data(const char *path);
|
static map<string, map<string, int>> load_data(const char *path);
|
||||||
static vector<string> predictNextWord(const string& input, const map<string, map<string, int>>& model, size_t count);
|
static vector<string> predict_next_word(const string& input, const map<string, map<string, int>>& word_frequencies, size_t count);
|
||||||
static string load_file(const char *path);
|
static string load_file(const char *path);
|
||||||
static vector<string> split_string(const string& input, const char *delimiters);
|
static vector<string> split_string(const string& input, const char *delimiters);
|
||||||
static string extract_url(const string& input);
|
static string extract_url(const string& input);
|
||||||
|
static vector<string> create_tag(const map<string, map<string, int>>& word_frequencies, unsigned int hash);
|
||||||
|
static unsigned int hash_url(const string& input);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -13,7 +13,7 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
void ServerUtils::serve(map<string, map<string, int>> wordwordFrequencies) {
|
void ServerUtils::serve(map<string, map<string, int>> word_frequencies) {
|
||||||
// server_fd is a file descriptor.
|
// server_fd is a file descriptor.
|
||||||
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
|
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
|
||||||
@ -23,36 +23,36 @@ void ServerUtils::serve(map<string, map<string, int>> wordwordFrequencies) {
|
|||||||
addr.sin_addr.s_addr = INADDR_ANY;
|
addr.sin_addr.s_addr = INADDR_ANY;
|
||||||
|
|
||||||
bind(server_fd, (sockaddr*)&addr, sizeof(addr));
|
bind(server_fd, (sockaddr*)&addr, sizeof(addr));
|
||||||
listen(server_fd, 5);
|
listen(server_fd, 15);
|
||||||
|
|
||||||
cout << "Server is running on http://localhost:8888 \n";
|
cout << "Server is running on http://localhost:8888 \n";
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
int client_fd = accept(server_fd, nullptr, nullptr);
|
int client_fd = accept(server_fd, nullptr, nullptr);
|
||||||
|
|
||||||
thread(process_request, client_fd, wordwordFrequencies).detach();
|
thread(process_request, client_fd, word_frequencies).detach();
|
||||||
}
|
}
|
||||||
|
|
||||||
close(server_fd);
|
close(server_fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ServerUtils::process_request(int client_fd, map<string, map<string, int>> wordwordFrequencies) {
|
void ServerUtils::process_request(int client_fd, map<string, map<string, int>> word_frequencies) {
|
||||||
char buffer[1024];
|
char buffer[1024];
|
||||||
|
|
||||||
int bytes_received = recv(client_fd, buffer, sizeof(buffer) - 1, 0);
|
int bytes_received = recv(client_fd, buffer, sizeof(buffer) - 1, 0);
|
||||||
|
|
||||||
|
string url;
|
||||||
|
|
||||||
if (bytes_received > 0) {
|
if (bytes_received > 0) {
|
||||||
buffer[bytes_received] = '\0';
|
buffer[bytes_received] = '\0';
|
||||||
|
|
||||||
string temp = WordUtils::extract_url(string(buffer));
|
url = WordUtils::extract_url(string(buffer));
|
||||||
|
|
||||||
cerr << temp << endl;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
cerr << "AAAA \n";
|
cerr << "AAAA \n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsigned int hash = WordUtils::hash_url(url);
|
||||||
|
|
||||||
string headers =
|
string headers =
|
||||||
"HTTP/1.1 200 OK\r\n"
|
"HTTP/1.1 200 OK\r\n"
|
||||||
@ -62,13 +62,7 @@ void ServerUtils::process_request(int client_fd, map<string, map<string, int>> w
|
|||||||
|
|
||||||
send(client_fd, headers.c_str(), headers.size(), 0);
|
send(client_fd, headers.c_str(), headers.size(), 0);
|
||||||
|
|
||||||
const string html_chunks[] = {
|
const vector<string> html_chunks = WordUtils::create_tag(word_frequencies, hash);
|
||||||
"<!DOCTYPE html>\n<html><head><title>Drip</title></head><body>\n",
|
|
||||||
"<h1>Hello</h1>\n",
|
|
||||||
"<p>This is a test</p>\n",
|
|
||||||
"<p>Drip feed complete!</p>\n",
|
|
||||||
"</body></html>\n"
|
|
||||||
};
|
|
||||||
|
|
||||||
for (const auto& chunk : html_chunks) {
|
for (const auto& chunk : html_chunks) {
|
||||||
ostringstream oss;
|
ostringstream oss;
|
||||||
@ -76,7 +70,7 @@ void ServerUtils::process_request(int client_fd, map<string, map<string, int>> w
|
|||||||
string to_send = oss.str();
|
string to_send = oss.str();
|
||||||
|
|
||||||
send(client_fd, to_send.c_str(), to_send.size(), 0);
|
send(client_fd, to_send.c_str(), to_send.size(), 0);
|
||||||
this_thread::sleep_for(chrono::seconds(1));
|
this_thread::sleep_for(chrono::milliseconds(5));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send final zero-length chunk to end the response
|
// Send final zero-length chunk to end the response
|
||||||
|
@ -3,6 +3,10 @@
|
|||||||
#include "../include/WordUtils.h"
|
#include "../include/WordUtils.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <cctype>
|
||||||
|
#include <cstring>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <random>
|
||||||
|
|
||||||
map<string, map<string, int>> WordUtils::load_data(const char *path) {
|
map<string, map<string, int>> WordUtils::load_data(const char *path) {
|
||||||
const char del[] = {' ', '.', ',', '!', '?', ';', ':', '(', ')', '\n', '\r', '\t'};
|
const char del[] = {' ', '.', ',', '!', '?', ';', ':', '(', ')', '\n', '\r', '\t'};
|
||||||
@ -12,22 +16,18 @@ map<string, map<string, int>> WordUtils::load_data(const char *path) {
|
|||||||
map<string, map<string, int>> word_frequencies = {};
|
map<string, map<string, int>> word_frequencies = {};
|
||||||
|
|
||||||
for (long unsigned int i = 0; i + 1 < data.size(); i++) {
|
for (long unsigned int i = 0; i + 1 < data.size(); i++) {
|
||||||
|
transform(data[i].begin(), data[i].end(), data[i].begin(), [](unsigned char c){ return tolower(c); });
|
||||||
|
transform(data[i+1].begin(), data[i+1].end(), data[i+1].begin(), [](unsigned char c){ return tolower(c); });
|
||||||
word_frequencies[data[i]][data[i+1]]++;
|
word_frequencies[data[i]][data[i+1]]++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
vector<pair<string, vector<string, int>>> test;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return word_frequencies;
|
return word_frequencies;
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<string> WordUtils::predictNextWord(const string& input, const map<string, map<string, int>>& model, size_t count) {
|
vector<string> WordUtils::predict_next_word(const string& input, const map<string, map<string, int>>& word_frequencies, size_t count) {
|
||||||
auto it = model.find(input);
|
auto it = word_frequencies.find(input);
|
||||||
|
|
||||||
if (it == model.end()) return {"(no prediction)"};
|
if (it == word_frequencies.end()) return {input};
|
||||||
|
|
||||||
const map<string, int> nextWords = it->second;
|
const map<string, int> nextWords = it->second;
|
||||||
|
|
||||||
@ -48,6 +48,69 @@ vector<string> WordUtils::predictNextWord(const string& input, const map<string,
|
|||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
vector<string> WordUtils::create_tag(const map<string, map<string, int>>& word_frequencies, unsigned int hash) {
|
||||||
|
const vector<string> beginning_sequences = {"the", "but", "with"};
|
||||||
|
const string start = "<!DOCTYPE html>\n<html><head><title>Drip</title></head><body>\n";
|
||||||
|
const string end = "</body></html>\n";
|
||||||
|
vector<string> tags;
|
||||||
|
|
||||||
|
const string hashes = to_string(hash);
|
||||||
|
|
||||||
|
tags.push_back(start);
|
||||||
|
|
||||||
|
int predict_num = 5;
|
||||||
|
|
||||||
|
// Tags
|
||||||
|
for (int i = 0; i < hashes.size(); i++)
|
||||||
|
{
|
||||||
|
vector<string> inner_tags;
|
||||||
|
|
||||||
|
minstd_rand generator((int)hashes[i]);
|
||||||
|
|
||||||
|
uniform_int_distribution<int> outer_distribution(0, beginning_sequences.size() - 1);
|
||||||
|
vector<string> temp_words = predict_next_word(beginning_sequences[outer_distribution(generator)], word_frequencies, predict_num);
|
||||||
|
|
||||||
|
uniform_int_distribution<int> outer_2_distribution(0, temp_words.size() - 1);
|
||||||
|
|
||||||
|
inner_tags.push_back(temp_words[outer_2_distribution(generator)]);
|
||||||
|
|
||||||
|
// Words per tag
|
||||||
|
for (int j = 0; j < 25; j++)
|
||||||
|
{
|
||||||
|
temp_words = predict_next_word(inner_tags[j], word_frequencies, predict_num);
|
||||||
|
|
||||||
|
uniform_int_distribution<int> inner_distribution(0, temp_words.size() - 1);
|
||||||
|
|
||||||
|
if (temp_words.size() != 0)
|
||||||
|
{
|
||||||
|
temp_words = predict_next_word(inner_tags[j], word_frequencies, predict_num);
|
||||||
|
|
||||||
|
uniform_int_distribution<int> inner_2_distribution(0, temp_words.size() - 1);
|
||||||
|
|
||||||
|
inner_tags.push_back(temp_words[inner_2_distribution(generator)]);
|
||||||
|
} else {
|
||||||
|
temp_words = predict_next_word(beginning_sequences[outer_distribution(generator)], word_frequencies, predict_num);
|
||||||
|
|
||||||
|
uniform_int_distribution<int> inner_3_distribution(0, temp_words.size() - 1);
|
||||||
|
|
||||||
|
inner_tags.push_back(temp_words[inner_3_distribution(generator)]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
string temp_string = "<p>";
|
||||||
|
for (int l = 0; l < inner_tags.size(); l++)
|
||||||
|
{
|
||||||
|
temp_string += inner_tags[l] + " ";
|
||||||
|
}
|
||||||
|
|
||||||
|
temp_string += ".</p>";
|
||||||
|
|
||||||
|
tags.push_back(temp_string);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return tags;
|
||||||
|
}
|
||||||
|
|
||||||
vector<string> WordUtils::split_string(const string& input, const char *delimiters) {
|
vector<string> WordUtils::split_string(const string& input, const char *delimiters) {
|
||||||
vector<string> data;
|
vector<string> data;
|
||||||
@ -56,7 +119,7 @@ vector<string> WordUtils::split_string(const string& input, const char *delimite
|
|||||||
size_t end = 0;
|
size_t end = 0;
|
||||||
|
|
||||||
// Create a string from the delimiters array
|
// Create a string from the delimiters array
|
||||||
string delimiter_string(delimiters);
|
string delimiter_string = " .,!?;:()\n\r\t";
|
||||||
|
|
||||||
while ((end = input.find_first_of(delimiter_string, start)) != string::npos) {
|
while ((end = input.find_first_of(delimiter_string, start)) != string::npos) {
|
||||||
if (end > start) {
|
if (end > start) {
|
||||||
@ -101,4 +164,12 @@ string WordUtils::extract_url(const string& input) {
|
|||||||
if (path_end == string::npos) return "";
|
if (path_end == string::npos) return "";
|
||||||
|
|
||||||
return first_line.substr(method_end + 1, path_end - method_end - 1);
|
return first_line.substr(method_end + 1, path_end - method_end - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int WordUtils::hash_url(const string& input) {
|
||||||
|
unsigned int hash = 0;
|
||||||
|
for (char c : input) {
|
||||||
|
hash += static_cast<unsigned int>(c);
|
||||||
|
}
|
||||||
|
return hash;
|
||||||
}
|
}
|
14
src/main.cpp
14
src/main.cpp
@ -5,7 +5,7 @@
|
|||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
if (argc > 2)
|
/*if (argc > 2)
|
||||||
{
|
{
|
||||||
cout << "Too many arguments";
|
cout << "Too many arguments";
|
||||||
return 0;
|
return 0;
|
||||||
@ -15,10 +15,10 @@ int main(int argc, char* argv[]) {
|
|||||||
{
|
{
|
||||||
cout << "Filepath: " << argv[1] << " doesn't exist.";
|
cout << "Filepath: " << argv[1] << " doesn't exist.";
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}*/
|
||||||
|
|
||||||
|
|
||||||
auto lol = WordUtils::load_data(argv[1]);
|
/*auto lol = WordUtils::load_data("/home/skingging/Documents/Projects/CPP/AI-Tarpit-Reimagined/wordlist/wordlist-Food.txt");
|
||||||
string word;
|
string word;
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
@ -26,17 +26,17 @@ int main(int argc, char* argv[]) {
|
|||||||
cout << "Please input word: \n";
|
cout << "Please input word: \n";
|
||||||
cin >> word;
|
cin >> word;
|
||||||
|
|
||||||
vector<string> words = WordUtils::predictNextWord(word, lol, 10);
|
vector<string> words = WordUtils::predict_next_word(word, lol, 10);
|
||||||
|
|
||||||
for (const auto& out : words)
|
for (const auto& out : words)
|
||||||
{
|
{
|
||||||
cout << "Next word is: " << out << endl;
|
cout << "Next word is: " << out << endl;
|
||||||
}
|
}
|
||||||
}
|
}*/
|
||||||
|
|
||||||
|
|
||||||
|
//argv[1]
|
||||||
//ServerUtils::serve(WordUtils::load_data(argv[1]));
|
ServerUtils::serve(WordUtils::load_data("/home/skingging/Documents/Projects/CPP/AI-Tarpit-Reimagined/wordlist/wordlist-Food.txt"));
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user