Added reading in text file and trie structure

This commit is contained in:
Trianta 2023-12-08 18:03:58 -06:00
parent 9ae148e5ee
commit d3f007b7f6
3 changed files with 87 additions and 0 deletions

View File

@ -1,6 +1,36 @@
#include "generator.hpp"
#include <fstream>
#include <iostream>
void Trie::insert(const std::deque<char>& currentKGram) {
TrieNode* current = root;
for (char ch : currentKGram) {
if (current->children.find(ch) == current->children.end()) {
current->children[ch] = new TrieNode();
} else { ++current->occurances; }
current = current->children[ch];
}
current->isEndOfWord = true;
}
bool Trie::search(const std::deque<char>& currentKGram) const {
TrieNode* current = root;
for (char ch : currentKGram) {
if (current->children.find(ch) == current->children.end()) {
return false;
}
current = current->children[ch];
}
return current->isEndOfWord;
}
void Generator::SetArguments(int argc, char* argv[]) {
std::string tempStr;
for (int i = 1; i < argc; i += 2) {
@ -28,6 +58,36 @@ void Generator::SetArguments(int argc, char* argv[]) {
if (!setup.isFileSet || !setup.isPrefixSet || !setup.isOutputSet) { PrintUsage(); }
}
void Generator::ReadFile(void) {
std::ifstream inputFile(setup.filename);
if (!inputFile.is_open()) {
std::cerr << "[ReadFile - Error] Could not open file: " << setup.filename << std::endl;
exit(1);
}
std::deque<char> currentKGram;
char tempChar;
// Read in first k-gram
{
std::string initializeKGram;
inputFile.get(&initializeKGram[0], setup.prefixLength);
for (char ch : initializeKGram) { currentKGram.emplace_back(ch); }
trie.insert(currentKGram);
}
// Read rest of file
while (inputFile.get(tempChar)) {
currentKGram.emplace_back(tempChar);
if (currentKGram.size() > setup.prefixLength) { currentKGram.pop_front(); }
trie.insert(currentKGram);
}
}
void Generator::GenerateOutput(void) {
}
char GenerateCharacter(void) {
return 'z';
}
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;

View File

@ -1,6 +1,8 @@
#ifndef GENERATOR_HPP
#define GENERATOR_HPP
#include <deque>
#include <unordered_map>
#include <string>
void PrintUsage(void);
@ -14,13 +16,36 @@ struct ArgumentList {
bool isOutputSet = false;
};
class TrieNode {
public:
std::unordered_map<char, TrieNode*> children;
int occurances = 1;
bool isEndOfWord;
TrieNode() : isEndOfWord(false) {}
};
class Trie {
public:
Trie() : root(new TrieNode()) {}
void insert(const std::deque<char>& currentKGram);
bool search(const std::deque<char>& currentKGram) const;
private:
TrieNode* root;
};
struct Generator {
public:
Generator(void) = default;
~Generator(void) = default;
void SetArguments(int argc, char* argv[]);
void ReadFile(void);
void GenerateOutput(void);
private:
char GenerateCharacter(void);
ArgumentList setup;
Trie trie;
};
#endif // !GENERATOR_HPP

View File

@ -7,5 +7,7 @@ int main(int argc, char* argv[]) {
}
Generator markovChain;
markovChain.SetArguments(argc, argv);
markovChain.ReadFile();
markovChain.GenerateOutput();
return 0;
}