generated from Trianta/cpp-unity-template
Added reading in text file and trie structure
This commit is contained in:
parent
9ae148e5ee
commit
d3f007b7f6
@ -1,6 +1,36 @@
|
|||||||
#include "generator.hpp"
|
#include "generator.hpp"
|
||||||
|
#include <fstream>
|
||||||
#include <iostream>
|
#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[]) {
|
void Generator::SetArguments(int argc, char* argv[]) {
|
||||||
std::string tempStr;
|
std::string tempStr;
|
||||||
for (int i = 1; i < argc; i += 2) {
|
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(); }
|
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) {
|
void PrintUsage(void) {
|
||||||
std::cout << "Usage: markov -i input_file -k prefix_length -n output_length" << std::endl;
|
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 << " -i: Direct path to input file for basis" << std::endl;
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
#ifndef GENERATOR_HPP
|
#ifndef GENERATOR_HPP
|
||||||
#define GENERATOR_HPP
|
#define GENERATOR_HPP
|
||||||
|
|
||||||
|
#include <deque>
|
||||||
|
#include <unordered_map>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
void PrintUsage(void);
|
void PrintUsage(void);
|
||||||
@ -14,13 +16,36 @@ struct ArgumentList {
|
|||||||
bool isOutputSet = false;
|
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 {
|
struct Generator {
|
||||||
public:
|
public:
|
||||||
Generator(void) = default;
|
Generator(void) = default;
|
||||||
~Generator(void) = default;
|
~Generator(void) = default;
|
||||||
void SetArguments(int argc, char* argv[]);
|
void SetArguments(int argc, char* argv[]);
|
||||||
|
void ReadFile(void);
|
||||||
|
void GenerateOutput(void);
|
||||||
private:
|
private:
|
||||||
|
char GenerateCharacter(void);
|
||||||
ArgumentList setup;
|
ArgumentList setup;
|
||||||
|
Trie trie;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // !GENERATOR_HPP
|
#endif // !GENERATOR_HPP
|
||||||
|
@ -7,5 +7,7 @@ int main(int argc, char* argv[]) {
|
|||||||
}
|
}
|
||||||
Generator markovChain;
|
Generator markovChain;
|
||||||
markovChain.SetArguments(argc, argv);
|
markovChain.SetArguments(argc, argv);
|
||||||
|
markovChain.ReadFile();
|
||||||
|
markovChain.GenerateOutput();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user