2023-12-07 21:36:51 -06:00
|
|
|
#include "generator.hpp"
|
2023-12-08 18:19:14 -06:00
|
|
|
#include <cstdlib>
|
2023-12-08 18:03:58 -06:00
|
|
|
#include <fstream>
|
2023-12-07 21:36:51 -06:00
|
|
|
#include <iostream>
|
2023-12-08 22:45:30 -06:00
|
|
|
#include <iterator>
|
2023-12-08 18:19:14 -06:00
|
|
|
#include <random>
|
2023-12-08 22:45:30 -06:00
|
|
|
#include <sstream>
|
2023-12-08 18:19:14 -06:00
|
|
|
|
|
|
|
std::default_random_engine generator;
|
2023-12-08 22:45:30 -06:00
|
|
|
|
2023-12-08 18:19:14 -06:00
|
|
|
void InitializeGenerator(void)
|
|
|
|
{
|
|
|
|
generator.seed(std::random_device{}());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a newly generated number
|
|
|
|
int GenerateRandomNumber(int generationLimit)
|
|
|
|
{
|
|
|
|
int generatedNumber;
|
|
|
|
std::uniform_int_distribution<> distribution(0, generationLimit - 1);
|
2023-12-08 22:45:30 -06:00
|
|
|
generatedNumber = distribution(generator);
|
2023-12-08 18:19:14 -06:00
|
|
|
return generatedNumber;
|
|
|
|
}
|
2023-12-07 21:36:51 -06:00
|
|
|
|
2023-12-08 22:45:30 -06:00
|
|
|
TrieNode::TrieNode(void) {
|
|
|
|
isEndOfWord = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Trie::Trie(void) {
|
|
|
|
root = new TrieNode();
|
|
|
|
}
|
2023-12-08 18:03:58 -06:00
|
|
|
|
2023-12-08 22:45:30 -06:00
|
|
|
void Trie::Insert(std::string kgram) {
|
2023-12-08 18:03:58 -06:00
|
|
|
TrieNode* current = root;
|
|
|
|
|
2023-12-08 22:45:30 -06:00
|
|
|
for (char ch : kgram) {
|
2023-12-08 18:03:58 -06:00
|
|
|
if (current->children.find(ch) == current->children.end()) {
|
|
|
|
current->children[ch] = new TrieNode();
|
2023-12-08 22:45:30 -06:00
|
|
|
} else { ++current->occurrences; }
|
|
|
|
Recalculate(current->children);
|
2023-12-08 18:03:58 -06:00
|
|
|
current = current->children[ch];
|
|
|
|
}
|
|
|
|
|
|
|
|
current->isEndOfWord = true;
|
|
|
|
}
|
|
|
|
|
2023-12-08 22:45:30 -06:00
|
|
|
char Trie::GetNextCharacter(const std::string& kgram) {
|
2023-12-08 18:03:58 -06:00
|
|
|
TrieNode* current = root;
|
2023-12-08 22:45:30 -06:00
|
|
|
char nextChar = ' ';
|
|
|
|
for (char ch: kgram) {
|
|
|
|
auto it = current->children.find(ch);
|
|
|
|
if (it == current->children.end()) {
|
|
|
|
return nextChar;
|
|
|
|
}
|
|
|
|
current = it->second;
|
|
|
|
}
|
|
|
|
double roll = ((double) GenerateRandomNumber(RAND_MAX)) / ((double) RAND_MAX);
|
|
|
|
double minimum = 0.;
|
|
|
|
for (const auto& i : current->children) {
|
|
|
|
minimum += i.second->probability;
|
|
|
|
if (roll <= minimum) {
|
|
|
|
nextChar = i.first;
|
|
|
|
break;
|
2023-12-08 18:03:58 -06:00
|
|
|
}
|
|
|
|
}
|
2023-12-08 22:45:30 -06:00
|
|
|
return nextChar;
|
|
|
|
}
|
2023-12-08 18:03:58 -06:00
|
|
|
|
2023-12-08 22:45:30 -06:00
|
|
|
void Trie::Recalculate(std::unordered_map<char, TrieNode*> children) {
|
|
|
|
int total = 0;
|
|
|
|
for (auto i : children) {
|
|
|
|
total += i.second->occurrences;
|
|
|
|
}
|
|
|
|
for (auto i : children) {
|
|
|
|
i.second->probability = ((double)i.second->occurrences) / ((double) total);
|
|
|
|
}
|
2023-12-08 18:03:58 -06:00
|
|
|
}
|
|
|
|
|
2023-12-08 22:45:30 -06:00
|
|
|
void Generator::SetArguments(const int argc, char* argv[]) {
|
2023-12-07 21:36:51 -06:00
|
|
|
std::string tempStr;
|
2023-12-07 23:58:54 -06:00
|
|
|
for (int i = 1; i < argc; i += 2) {
|
2023-12-07 21:36:51 -06:00
|
|
|
tempStr.assign(argv[i]);
|
|
|
|
if (tempStr == "-i") {
|
|
|
|
setup.isFileSet = true;
|
2023-12-07 23:58:54 -06:00
|
|
|
setup.filename.assign(argv[i+1]);
|
2023-12-07 21:36:51 -06:00
|
|
|
}
|
|
|
|
if (tempStr == "-k") {
|
|
|
|
setup.isPrefixSet = true;
|
2023-12-07 23:58:54 -06:00
|
|
|
setup.prefixLength = std::stoi(argv[i+1]);
|
2023-12-07 21:36:51 -06:00
|
|
|
}
|
|
|
|
if (tempStr == "-n") {
|
|
|
|
setup.isOutputSet = true;
|
2023-12-07 23:58:54 -06:00
|
|
|
setup.outputLength = std::stoi(argv[i+1]);
|
2023-12-07 21:36:51 -06:00
|
|
|
}
|
|
|
|
if (tempStr == "-h") {
|
|
|
|
PrintUsage();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!setup.isFileSet) { std::cerr << "[Setup - Error] Filename not specified" << std::endl; }
|
|
|
|
if (!setup.prefixLength) { std::cerr << "[Setup - Error] Prefix length not specified" << std::endl; }
|
|
|
|
if (!setup.outputLength) { std::cerr << "[Setup - Error] Output length not specified" << std::endl; }
|
|
|
|
if (!setup.isFileSet || !setup.isPrefixSet || !setup.isOutputSet) { PrintUsage(); }
|
|
|
|
}
|
|
|
|
|
2023-12-08 22:45:30 -06:00
|
|
|
void Generator::Train(void) {
|
2023-12-08 18:03:58 -06:00
|
|
|
std::ifstream inputFile(setup.filename);
|
|
|
|
if (!inputFile.is_open()) {
|
|
|
|
std::cerr << "[ReadFile - Error] Could not open file: " << setup.filename << std::endl;
|
|
|
|
exit(1);
|
|
|
|
}
|
2023-12-08 22:45:30 -06:00
|
|
|
std::stringstream iss;
|
|
|
|
iss << inputFile.rdbuf();
|
|
|
|
std::vector<std::string> words(std::istream_iterator<std::string>{iss},
|
|
|
|
std::istream_iterator<std::string>());
|
|
|
|
|
|
|
|
std::cout << "[Setup - Info] Begin training" << std::endl;
|
|
|
|
for (const auto& word : words) {
|
|
|
|
if (word.size() < setup.prefixLength) {
|
|
|
|
trie.Insert(word);
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < word.size() - setup.prefixLength; ++i) {
|
|
|
|
trie.Insert(word.substr(i, setup.prefixLength));
|
|
|
|
}
|
|
|
|
}
|
2023-12-08 18:03:58 -06:00
|
|
|
}
|
2023-12-08 22:45:30 -06:00
|
|
|
std::cout << "[Setup - Info] Finished training" << std::endl;
|
2023-12-08 18:03:58 -06:00
|
|
|
}
|
|
|
|
|
2023-12-08 22:45:30 -06:00
|
|
|
void Generator::Generation(void) {
|
|
|
|
std::cout << "[Generation - Info] Output start" << std::endl;
|
|
|
|
for (int i = 0; i < setup.outputLength; ++i) {
|
|
|
|
char nextChar = trie.GetNextCharacter(currentKGram);
|
|
|
|
std::cout << nextChar;
|
|
|
|
if (nextChar == ' ') {
|
|
|
|
currentKGram.clear();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (currentKGram.size() < setup.prefixLength) {
|
|
|
|
currentKGram += nextChar;
|
|
|
|
} else {
|
|
|
|
currentKGram = currentKGram.substr(1) + nextChar;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::cout << std::endl << "[Generation - Info] Output finished" << std::endl;
|
2023-12-08 18:03:58 -06:00
|
|
|
}
|
|
|
|
|
2023-12-07 21:36:51 -06:00
|
|
|
void PrintUsage(void) {
|
|
|
|
std::cout << "Usage: markov -i input_file -k prefix_length -n output_length" << std::endl;
|
|
|
|
std::cout << " -i: Direct path to input file for basis" << std::endl;
|
|
|
|
std::cout << " -k: Prefix length for Markov chain" << std::endl;
|
|
|
|
std::cout << " -n: Length of output to be generated (words)" << std::endl;
|
|
|
|
std::cout << " -h: Prints this usage text" << std::endl;
|
|
|
|
}
|