Just testing a bit
This commit is contained in:
parent
69d1898879
commit
190e9aa7b6
3
.gitignore
vendored
3
.gitignore
vendored
@ -86,3 +86,6 @@ dkms.conf
|
||||
*.out
|
||||
*.app
|
||||
|
||||
# Misc
|
||||
*.txt
|
||||
CMakeFiles/
|
||||
|
262
Makefile
Normal file
262
Makefile
Normal file
@ -0,0 +1,262 @@
|
||||
# CMAKE generated file: DO NOT EDIT!
|
||||
# Generated by "Unix Makefiles" Generator, CMake Version 3.31
|
||||
|
||||
# Default target executed when no arguments are given to make.
|
||||
default_target: all
|
||||
.PHONY : default_target
|
||||
|
||||
# Allow only one "make -f Makefile2" at a time, but pass parallelism.
|
||||
.NOTPARALLEL:
|
||||
|
||||
#=============================================================================
|
||||
# Special targets provided by cmake.
|
||||
|
||||
# Disable implicit rules so canonical targets will work.
|
||||
.SUFFIXES:
|
||||
|
||||
# Disable VCS-based implicit rules.
|
||||
% : %,v
|
||||
|
||||
# Disable VCS-based implicit rules.
|
||||
% : RCS/%
|
||||
|
||||
# Disable VCS-based implicit rules.
|
||||
% : RCS/%,v
|
||||
|
||||
# Disable VCS-based implicit rules.
|
||||
% : SCCS/s.%
|
||||
|
||||
# Disable VCS-based implicit rules.
|
||||
% : s.%
|
||||
|
||||
.SUFFIXES: .hpux_make_needs_suffix_list
|
||||
|
||||
# Command-line flag to silence nested $(MAKE).
|
||||
$(VERBOSE)MAKESILENT = -s
|
||||
|
||||
#Suppress display of executed commands.
|
||||
$(VERBOSE).SILENT:
|
||||
|
||||
# A target that is always out of date.
|
||||
cmake_force:
|
||||
.PHONY : cmake_force
|
||||
|
||||
#=============================================================================
|
||||
# Set environment variables for the build.
|
||||
|
||||
# The shell in which to execute make rules.
|
||||
SHELL = /bin/sh
|
||||
|
||||
# The CMake executable.
|
||||
CMAKE_COMMAND = /usr/bin/cmake
|
||||
|
||||
# The command to remove a file.
|
||||
RM = /usr/bin/cmake -E rm -f
|
||||
|
||||
# Escaping for special characters.
|
||||
EQUALS = =
|
||||
|
||||
# The top-level source directory on which CMake was run.
|
||||
CMAKE_SOURCE_DIR = /home/skingging/Documents/Projects/CPP/AI-Tarpit-Reimagined
|
||||
|
||||
# The top-level build directory on which CMake was run.
|
||||
CMAKE_BINARY_DIR = /home/skingging/Documents/Projects/CPP/AI-Tarpit-Reimagined
|
||||
|
||||
#=============================================================================
|
||||
# Targets provided globally by CMake.
|
||||
|
||||
# Special rule for the target edit_cache
|
||||
edit_cache:
|
||||
@$(CMAKE_COMMAND) -E cmake_echo_color "--switch=$(COLOR)" --cyan "Running CMake cache editor..."
|
||||
/usr/bin/ccmake -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
|
||||
.PHONY : edit_cache
|
||||
|
||||
# Special rule for the target edit_cache
|
||||
edit_cache/fast: edit_cache
|
||||
.PHONY : edit_cache/fast
|
||||
|
||||
# Special rule for the target rebuild_cache
|
||||
rebuild_cache:
|
||||
@$(CMAKE_COMMAND) -E cmake_echo_color "--switch=$(COLOR)" --cyan "Running CMake to regenerate build system..."
|
||||
/usr/bin/cmake --regenerate-during-build -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
|
||||
.PHONY : rebuild_cache
|
||||
|
||||
# Special rule for the target rebuild_cache
|
||||
rebuild_cache/fast: rebuild_cache
|
||||
.PHONY : rebuild_cache/fast
|
||||
|
||||
# The main all target
|
||||
all: cmake_check_build_system
|
||||
$(CMAKE_COMMAND) -E cmake_progress_start /home/skingging/Documents/Projects/CPP/AI-Tarpit-Reimagined/CMakeFiles /home/skingging/Documents/Projects/CPP/AI-Tarpit-Reimagined//CMakeFiles/progress.marks
|
||||
$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 all
|
||||
$(CMAKE_COMMAND) -E cmake_progress_start /home/skingging/Documents/Projects/CPP/AI-Tarpit-Reimagined/CMakeFiles 0
|
||||
.PHONY : all
|
||||
|
||||
# The main clean target
|
||||
clean:
|
||||
$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 clean
|
||||
.PHONY : clean
|
||||
|
||||
# The main clean target
|
||||
clean/fast: clean
|
||||
.PHONY : clean/fast
|
||||
|
||||
# Prepare targets for installation.
|
||||
preinstall: all
|
||||
$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 preinstall
|
||||
.PHONY : preinstall
|
||||
|
||||
# Prepare targets for installation.
|
||||
preinstall/fast:
|
||||
$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 preinstall
|
||||
.PHONY : preinstall/fast
|
||||
|
||||
# clear depends
|
||||
depend:
|
||||
$(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1
|
||||
.PHONY : depend
|
||||
|
||||
#=============================================================================
|
||||
# Target rules for targets named tarpit
|
||||
|
||||
# Build rule for target.
|
||||
tarpit: cmake_check_build_system
|
||||
$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 tarpit
|
||||
.PHONY : tarpit
|
||||
|
||||
# fast build rule for target.
|
||||
tarpit/fast:
|
||||
$(MAKE) $(MAKESILENT) -f CMakeFiles/tarpit.dir/build.make CMakeFiles/tarpit.dir/build
|
||||
.PHONY : tarpit/fast
|
||||
|
||||
src/FileUtils.o: src/FileUtils.cpp.o
|
||||
.PHONY : src/FileUtils.o
|
||||
|
||||
# target to build an object file
|
||||
src/FileUtils.cpp.o:
|
||||
$(MAKE) $(MAKESILENT) -f CMakeFiles/tarpit.dir/build.make CMakeFiles/tarpit.dir/src/FileUtils.cpp.o
|
||||
.PHONY : src/FileUtils.cpp.o
|
||||
|
||||
src/FileUtils.i: src/FileUtils.cpp.i
|
||||
.PHONY : src/FileUtils.i
|
||||
|
||||
# target to preprocess a source file
|
||||
src/FileUtils.cpp.i:
|
||||
$(MAKE) $(MAKESILENT) -f CMakeFiles/tarpit.dir/build.make CMakeFiles/tarpit.dir/src/FileUtils.cpp.i
|
||||
.PHONY : src/FileUtils.cpp.i
|
||||
|
||||
src/FileUtils.s: src/FileUtils.cpp.s
|
||||
.PHONY : src/FileUtils.s
|
||||
|
||||
# target to generate assembly for a file
|
||||
src/FileUtils.cpp.s:
|
||||
$(MAKE) $(MAKESILENT) -f CMakeFiles/tarpit.dir/build.make CMakeFiles/tarpit.dir/src/FileUtils.cpp.s
|
||||
.PHONY : src/FileUtils.cpp.s
|
||||
|
||||
src/ServerUtils.o: src/ServerUtils.cpp.o
|
||||
.PHONY : src/ServerUtils.o
|
||||
|
||||
# target to build an object file
|
||||
src/ServerUtils.cpp.o:
|
||||
$(MAKE) $(MAKESILENT) -f CMakeFiles/tarpit.dir/build.make CMakeFiles/tarpit.dir/src/ServerUtils.cpp.o
|
||||
.PHONY : src/ServerUtils.cpp.o
|
||||
|
||||
src/ServerUtils.i: src/ServerUtils.cpp.i
|
||||
.PHONY : src/ServerUtils.i
|
||||
|
||||
# target to preprocess a source file
|
||||
src/ServerUtils.cpp.i:
|
||||
$(MAKE) $(MAKESILENT) -f CMakeFiles/tarpit.dir/build.make CMakeFiles/tarpit.dir/src/ServerUtils.cpp.i
|
||||
.PHONY : src/ServerUtils.cpp.i
|
||||
|
||||
src/ServerUtils.s: src/ServerUtils.cpp.s
|
||||
.PHONY : src/ServerUtils.s
|
||||
|
||||
# target to generate assembly for a file
|
||||
src/ServerUtils.cpp.s:
|
||||
$(MAKE) $(MAKESILENT) -f CMakeFiles/tarpit.dir/build.make CMakeFiles/tarpit.dir/src/ServerUtils.cpp.s
|
||||
.PHONY : src/ServerUtils.cpp.s
|
||||
|
||||
src/WordUtils.o: src/WordUtils.cpp.o
|
||||
.PHONY : src/WordUtils.o
|
||||
|
||||
# target to build an object file
|
||||
src/WordUtils.cpp.o:
|
||||
$(MAKE) $(MAKESILENT) -f CMakeFiles/tarpit.dir/build.make CMakeFiles/tarpit.dir/src/WordUtils.cpp.o
|
||||
.PHONY : src/WordUtils.cpp.o
|
||||
|
||||
src/WordUtils.i: src/WordUtils.cpp.i
|
||||
.PHONY : src/WordUtils.i
|
||||
|
||||
# target to preprocess a source file
|
||||
src/WordUtils.cpp.i:
|
||||
$(MAKE) $(MAKESILENT) -f CMakeFiles/tarpit.dir/build.make CMakeFiles/tarpit.dir/src/WordUtils.cpp.i
|
||||
.PHONY : src/WordUtils.cpp.i
|
||||
|
||||
src/WordUtils.s: src/WordUtils.cpp.s
|
||||
.PHONY : src/WordUtils.s
|
||||
|
||||
# target to generate assembly for a file
|
||||
src/WordUtils.cpp.s:
|
||||
$(MAKE) $(MAKESILENT) -f CMakeFiles/tarpit.dir/build.make CMakeFiles/tarpit.dir/src/WordUtils.cpp.s
|
||||
.PHONY : src/WordUtils.cpp.s
|
||||
|
||||
src/main.o: src/main.cpp.o
|
||||
.PHONY : src/main.o
|
||||
|
||||
# target to build an object file
|
||||
src/main.cpp.o:
|
||||
$(MAKE) $(MAKESILENT) -f CMakeFiles/tarpit.dir/build.make CMakeFiles/tarpit.dir/src/main.cpp.o
|
||||
.PHONY : src/main.cpp.o
|
||||
|
||||
src/main.i: src/main.cpp.i
|
||||
.PHONY : src/main.i
|
||||
|
||||
# target to preprocess a source file
|
||||
src/main.cpp.i:
|
||||
$(MAKE) $(MAKESILENT) -f CMakeFiles/tarpit.dir/build.make CMakeFiles/tarpit.dir/src/main.cpp.i
|
||||
.PHONY : src/main.cpp.i
|
||||
|
||||
src/main.s: src/main.cpp.s
|
||||
.PHONY : src/main.s
|
||||
|
||||
# target to generate assembly for a file
|
||||
src/main.cpp.s:
|
||||
$(MAKE) $(MAKESILENT) -f CMakeFiles/tarpit.dir/build.make CMakeFiles/tarpit.dir/src/main.cpp.s
|
||||
.PHONY : src/main.cpp.s
|
||||
|
||||
# Help Target
|
||||
help:
|
||||
@echo "The following are some of the valid targets for this Makefile:"
|
||||
@echo "... all (the default if no target is provided)"
|
||||
@echo "... clean"
|
||||
@echo "... depend"
|
||||
@echo "... edit_cache"
|
||||
@echo "... rebuild_cache"
|
||||
@echo "... tarpit"
|
||||
@echo "... src/FileUtils.o"
|
||||
@echo "... src/FileUtils.i"
|
||||
@echo "... src/FileUtils.s"
|
||||
@echo "... src/ServerUtils.o"
|
||||
@echo "... src/ServerUtils.i"
|
||||
@echo "... src/ServerUtils.s"
|
||||
@echo "... src/WordUtils.o"
|
||||
@echo "... src/WordUtils.i"
|
||||
@echo "... src/WordUtils.s"
|
||||
@echo "... src/main.o"
|
||||
@echo "... src/main.i"
|
||||
@echo "... src/main.s"
|
||||
.PHONY : help
|
||||
|
||||
|
||||
|
||||
#=============================================================================
|
||||
# Special targets to cleanup operation of make.
|
||||
|
||||
# Special rule to run CMake to check the build system integrity.
|
||||
# No rule that depends on this can have commands that come from listfiles
|
||||
# because they might be regenerated.
|
||||
cmake_check_build_system:
|
||||
$(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0
|
||||
.PHONY : cmake_check_build_system
|
||||
|
BIN
Testing.png
Normal file
BIN
Testing.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 360 KiB |
66
cmake_install.cmake
Normal file
66
cmake_install.cmake
Normal file
@ -0,0 +1,66 @@
|
||||
# Install script for directory: /home/skingging/Documents/Projects/CPP/AI-Tarpit-Reimagined
|
||||
|
||||
# Set the install prefix
|
||||
if(NOT DEFINED CMAKE_INSTALL_PREFIX)
|
||||
set(CMAKE_INSTALL_PREFIX "/usr/local")
|
||||
endif()
|
||||
string(REGEX REPLACE "/$" "" CMAKE_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}")
|
||||
|
||||
# Set the install configuration name.
|
||||
if(NOT DEFINED CMAKE_INSTALL_CONFIG_NAME)
|
||||
if(BUILD_TYPE)
|
||||
string(REGEX REPLACE "^[^A-Za-z0-9_]+" ""
|
||||
CMAKE_INSTALL_CONFIG_NAME "${BUILD_TYPE}")
|
||||
else()
|
||||
set(CMAKE_INSTALL_CONFIG_NAME "")
|
||||
endif()
|
||||
message(STATUS "Install configuration: \"${CMAKE_INSTALL_CONFIG_NAME}\"")
|
||||
endif()
|
||||
|
||||
# Set the component getting installed.
|
||||
if(NOT CMAKE_INSTALL_COMPONENT)
|
||||
if(COMPONENT)
|
||||
message(STATUS "Install component: \"${COMPONENT}\"")
|
||||
set(CMAKE_INSTALL_COMPONENT "${COMPONENT}")
|
||||
else()
|
||||
set(CMAKE_INSTALL_COMPONENT)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Install shared libraries without execute permission?
|
||||
if(NOT DEFINED CMAKE_INSTALL_SO_NO_EXE)
|
||||
set(CMAKE_INSTALL_SO_NO_EXE "0")
|
||||
endif()
|
||||
|
||||
# Is this installation the result of a crosscompile?
|
||||
if(NOT DEFINED CMAKE_CROSSCOMPILING)
|
||||
set(CMAKE_CROSSCOMPILING "FALSE")
|
||||
endif()
|
||||
|
||||
# Set path to fallback-tool for dependency-resolution.
|
||||
if(NOT DEFINED CMAKE_OBJDUMP)
|
||||
set(CMAKE_OBJDUMP "/usr/bin/objdump")
|
||||
endif()
|
||||
|
||||
string(REPLACE ";" "\n" CMAKE_INSTALL_MANIFEST_CONTENT
|
||||
"${CMAKE_INSTALL_MANIFEST_FILES}")
|
||||
if(CMAKE_INSTALL_LOCAL_ONLY)
|
||||
file(WRITE "/home/skingging/Documents/Projects/CPP/AI-Tarpit-Reimagined/install_local_manifest.txt"
|
||||
"${CMAKE_INSTALL_MANIFEST_CONTENT}")
|
||||
endif()
|
||||
if(CMAKE_INSTALL_COMPONENT)
|
||||
if(CMAKE_INSTALL_COMPONENT MATCHES "^[a-zA-Z0-9_.+-]+$")
|
||||
set(CMAKE_INSTALL_MANIFEST "install_manifest_${CMAKE_INSTALL_COMPONENT}.txt")
|
||||
else()
|
||||
string(MD5 CMAKE_INST_COMP_HASH "${CMAKE_INSTALL_COMPONENT}")
|
||||
set(CMAKE_INSTALL_MANIFEST "install_manifest_${CMAKE_INST_COMP_HASH}.txt")
|
||||
unset(CMAKE_INST_COMP_HASH)
|
||||
endif()
|
||||
else()
|
||||
set(CMAKE_INSTALL_MANIFEST "install_manifest.txt")
|
||||
endif()
|
||||
|
||||
if(NOT CMAKE_INSTALL_LOCAL_ONLY)
|
||||
file(WRITE "/home/skingging/Documents/Projects/CPP/AI-Tarpit-Reimagined/${CMAKE_INSTALL_MANIFEST}"
|
||||
"${CMAKE_INSTALL_MANIFEST_CONTENT}")
|
||||
endif()
|
14
include/FileUtils.h
Normal file
14
include/FileUtils.h
Normal file
@ -0,0 +1,14 @@
|
||||
#ifndef FILEUTILS_H
|
||||
#define FILEUTILS_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct FileUtils {
|
||||
static bool fileExists(const char *path);
|
||||
static char* getFiles(const char *path);
|
||||
};
|
||||
|
||||
#endif
|
23
include/ServerUtils.h
Normal file
23
include/ServerUtils.h
Normal file
@ -0,0 +1,23 @@
|
||||
#ifndef SERVERUTILS_H
|
||||
#define SERVERUTILS_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <unistd.h>
|
||||
#include <netinet/in.h>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
#include <unordered_map>
|
||||
#include <map>
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct ServerUtils {
|
||||
static void serve(map<string, map<string, int>> wordwordFrequencies);
|
||||
static void process_request(int client_fd, map<string, map<string, int>> wordwordFrequencies);
|
||||
};
|
||||
|
||||
#endif
|
20
include/WordUtils.h
Normal file
20
include/WordUtils.h
Normal file
@ -0,0 +1,20 @@
|
||||
#ifndef WORDUTILS_H
|
||||
#define WORDUTILS_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
#include <map>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct WordUtils {
|
||||
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 string load_file(const char *path);
|
||||
static vector<string> split_string(const string& input, const char *delimiters);
|
||||
static string extract_url(const string& input);
|
||||
};
|
||||
|
||||
#endif
|
6
src/FileUtils.cpp
Normal file
6
src/FileUtils.cpp
Normal file
@ -0,0 +1,6 @@
|
||||
#include "../include/FileUtils.h"
|
||||
#include <filesystem>
|
||||
|
||||
bool FileUtils::fileExists(const char *path){
|
||||
return filesystem::exists(path);
|
||||
}
|
86
src/ServerUtils.cpp
Normal file
86
src/ServerUtils.cpp
Normal 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
src/WordUtils.cpp
Normal file
104
src/WordUtils.cpp
Normal 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
src/main.cpp
Normal file
42
src/main.cpp
Normal 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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user