Just testing a bit

This commit is contained in:
2025-04-29 19:30:31 +02:00
parent 69d1898879
commit 190e9aa7b6
11 changed files with 626 additions and 0 deletions
+6
View File
@@ -0,0 +1,6 @@
#include "../include/FileUtils.h"
#include <filesystem>
bool FileUtils::fileExists(const char *path){
return filesystem::exists(path);
}
+86
View File
@@ -0,0 +1,86 @@
#include "../include/ServerUtils.h"
#include "../include/WordUtils.h"
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <string>
#include <unistd.h>
#include <netinet/in.h>
#include <thread>
#include <chrono>
using namespace std;
void ServerUtils::serve(map<string, map<string, int>> wordwordFrequencies) {
// server_fd is a file descriptor.
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in addr{};
addr.sin_family = AF_INET;
addr.sin_port = htons(8888);
addr.sin_addr.s_addr = INADDR_ANY;
bind(server_fd, (sockaddr*)&addr, sizeof(addr));
listen(server_fd, 5);
cout << "Server is running on http://localhost:8888 \n";
while (true) {
int client_fd = accept(server_fd, nullptr, nullptr);
thread(process_request, client_fd, wordwordFrequencies).detach();
}
close(server_fd);
}
void ServerUtils::process_request(int client_fd, map<string, map<string, int>> wordwordFrequencies) {
char buffer[1024];
int bytes_received = recv(client_fd, buffer, sizeof(buffer) - 1, 0);
if (bytes_received > 0) {
buffer[bytes_received] = '\0';
string temp = WordUtils::extract_url(string(buffer));
cerr << temp << endl;
} else {
cerr << "AAAA \n";
}
string headers =
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html; charset=utf-8\r\n"
"Transfer-Encoding: chunked\r\n"
"Connection: close\r\n\r\n";
send(client_fd, headers.c_str(), headers.size(), 0);
const string html_chunks[] = {
"<!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) {
ostringstream oss;
oss << hex << chunk.size() << "\r\n" << chunk << "\r\n";
string to_send = oss.str();
send(client_fd, to_send.c_str(), to_send.size(), 0);
this_thread::sleep_for(chrono::seconds(1));
}
// Send final zero-length chunk to end the response
send(client_fd, "0\r\n\r\n", 5, 0);
close(client_fd);
}
+104
View File
@@ -0,0 +1,104 @@
#include <string>
#include <fstream>
#include "../include/WordUtils.h"
#include <iostream>
#include <algorithm>
map<string, map<string, int>> WordUtils::load_data(const char *path) {
const char del[] = {' ', '.', ',', '!', '?', ';', ':', '(', ')', '\n', '\r', '\t'};
vector<string> data = split_string(load_file(path), del);
map<string, map<string, int>> word_frequencies = {};
for (long unsigned int i = 0; i + 1 < data.size(); i++) {
word_frequencies[data[i]][data[i+1]]++;
}
vector<pair<string, vector<string, int>>> test;
return word_frequencies;
}
vector<string> WordUtils::predictNextWord(const string& input, const map<string, map<string, int>>& model, size_t count) {
auto it = model.find(input);
if (it == model.end()) return {"(no prediction)"};
const map<string, int> nextWords = it->second;
vector<pair<string, int>> sortedWords(nextWords.begin(), nextWords.end());
// Sort by frequency (descending)
sort(sortedWords.begin(), sortedWords.end(),[](const auto& a, const auto& b) {
return a.second > b.second;
});
vector<string> results;
// Take up to `count` most common words
for (size_t i = 0; i < min(count, sortedWords.size()); ++i) {
results.push_back(sortedWords[i].first);
}
return results;
}
vector<string> WordUtils::split_string(const string& input, const char *delimiters) {
vector<string> data;
size_t start = 0;
size_t end = 0;
// Create a string from the delimiters array
string delimiter_string(delimiters);
while ((end = input.find_first_of(delimiter_string, start)) != string::npos) {
if (end > start) {
data.push_back(input.substr(start, end - start));
}
start = end + 1;
}
// Add the last token, if any
if (start < input.length()) {
data.push_back(input.substr(start));
}
return data;
}
string WordUtils::load_file(const char *path) {
ifstream file(path);
string temp;
string data;
while (getline(file, temp)) {
data += temp;
}
file.close();
return data;
}
string WordUtils::extract_url(const string& input) {
int first_line_end = input.find("\n");
if (first_line_end == string::npos) return "";
string first_line = input.substr(0, first_line_end);
int method_end = first_line.find(' ');
if (method_end == string::npos) return "";
int path_end = first_line.find(' ', method_end + 1);
if (path_end == string::npos) return "";
return first_line.substr(method_end + 1, path_end - method_end - 1);
}
+42
View File
@@ -0,0 +1,42 @@
#include "../include/FileUtils.h"
#include "../include/WordUtils.h"
#include "../include/ServerUtils.h"
using namespace std;
int main(int argc, char* argv[]) {
if (argc > 2)
{
cout << "Too many arguments";
return 0;
}
if (!FileUtils::fileExists(argv[1]))
{
cout << "Filepath: " << argv[1] << " doesn't exist.";
return 0;
}
auto lol = WordUtils::load_data(argv[1]);
string word;
while (true)
{
cout << "Please input word: \n";
cin >> word;
vector<string> words = WordUtils::predictNextWord(word, lol, 10);
for (const auto& out : words)
{
cout << "Next word is: " << out << endl;
}
}
//ServerUtils::serve(WordUtils::load_data(argv[1]));
return 0;
}