Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help implementing my codes to generate random text using markov chain with c++. my code keeps printout the output same as input, but

I need help implementing my codes to generate random text using markov chain with c++. my code keeps printout the output same as input, but I can't solve the issue. I need to have same function name, and file names so don't remove any functions just implement stuffs inside of function. RandWriter.cpp // Copyright Jiho Lee 2023

#include

#include

#include "RandWriter.hpp"

#include

RandWriter::RandWriter(std::string txt, size_t k) : text(txt), k_order(k) {

if (text.length() < k_order) {

throw std::invalid_argument("Text length should be at least the order k.");

}

buildModel();

}

size_t RandWriter::orderK() const {

return k_order;

}

size_t RandWriter::freq(std::string kgram) const {

if (kgram.length() != k_order) {

throw std::invalid_argument("kgram should be of length k.");

}

return kgram_freq.count(kgram) ? kgram_freq.at(kgram) : 0;

}

size_t RandWriter::freq(std::string kgram, char c) const {

if (kgram.length() != k_order) {

throw std::invalid_argument("kgram should be of length k.");

}

if (kgram_next_char.count(kgram) && kgram_next_char.at(kgram).count(c)) {

return kgram_next_char.at(kgram).at(c);

}

return 0;

}

char RandWriter::kRand(std::string kgram) {

if (kgram.length() != k_order) {

throw std::invalid_argument("kgram should be of length k.");

}

std::vector possible_chars;

for (char c = ' '; c <= '~'; ++c) {

size_t frequency = freq(kgram, c);

for (size_t i = 0; i < frequency; ++i) {

possible_chars.push_back(c);

}

}

if (possible_chars.empty()) {

throw std::runtime_error("No such kgram found.");

}

std::mt19937 gen(std::chrono::system_clock::now().time_since_epoch().count());

std::uniform_int_distribution<> dis(0, static_cast(possible_chars.size()) - 1);

return possible_chars[dis(gen)];

}

std::string RandWriter::generate(std::string kgram, int L) {

if (kgram.length() != k_order) {

throw std::invalid_argument("kgram should be of length k.");

}

std::string result = kgram;

for (int i = k_order; i < L; ++i) {

char nextChar = kRand(kgram);

result += nextChar;

result = result.substr(result.length() - k_order); // Update the kgram based on the generated sequence

kgram = result.substr(result.length() - k_order); // Update the kgram for the next iteration

}

return result;

}

void RandWriter::buildModel() {

for (size_t i = 0; i <= text.length() - k_order; ++i) {

std::string kgram = text.substr(i, k_order);

char nextChar = text[i + k_order];

// Update kgram_freq

kgram_freq[kgram]++;

// Update kgram_next_char

kgram_next_char[kgram][nextChar]++;

}

}

std::ostream& operator<<(std::ostream& os, const RandWriter& rw) {

os << "Order: " << rw.k_order << std::endl;

// Print out k-gram and k+1-gram frequencies

// ...

// Your implementation to print k-gram and k+1-gram frequencies

return os;

}

RandWriter.hpp // Copyright Jiho Lee 2023

#pragma once

#include

#include

#include

class RandWriter {

public:

RandWriter(std::string text, size_t k);

size_t orderK() const;

size_t freq(std::string kgram) const;

size_t freq(std::string kgram, char c) const;

char kRand(std::string kgram);

std::string generate(std::string kgram, int L);

friend std::ostream& operator<<(std::ostream& os, const RandWriter& rw);

private:

std::string text;

size_t k_order;

std::mapstring, size_t> kgram_freq;

std::mapstring, std::mapsize_t>> kgram_next_char;

void buildModel();

};

main.cpp // Copyright Jiho Lee 2023

#include

#include

#include

#include "RandWriter.hpp"

int main(int argc, char* argv[]) {

if (argc != 3) {

std::cerr << "Usage: " << argv[0] << " k L < input.txt" << std::endl;

return 1;

}

int k = std::stoi(argv[1]);

int L = std::stoi(argv[2]);

std::stringstream buffer;

buffer << std::cin.rdbuf();

std::string text = buffer.str();

try {

RandWriter writer(text, k);

std::string generatedText = writer.generate(text.substr(0, k), L);

std::cout << generatedText << std::endl;

} catch (const std::exception& e) {

std::cerr << "Error: " << e.what() << std::endl;

return 1;

}

return 0;

}

below is the expected output for that text in order. Order 2 "Yess been." for gothin, Tome oso; ing, in to weliss of an'te cle armit. Papper a comeasione, and smomenty, fropeck hinticer, sid, a was Tom, be suck tied. He sis tred a youck to themen Order 4 2 en themself, Mr. Welshman, but him awoke, the balmy shore. I'll give him that he couple overy because in the slated snuindeed structure's kind was rath. She said that the wound the door a fever eyes that WITH him. Order 6 people had eaten, leaving. Come didn't stand it better judgment; His hands and bury it again, tramped herself! She'd never would be. He found her spite of anything the one was a prime feature sunset, and hit upon that of the forever. Order 8 look-a-here I told you before, Joe. I've heard a pin drop. The stillness was complete, how- ever, this is awful crime, beyond the village was sucient. He would be a good enough to get that night, Tom and Becky. Order 10 you understanding that they don't come around in the cave should get the word "beauteous" was over-fondled, and that together" and decided that he might as we used to do it's nobby fun. I'll learn you."

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mobile Communications

Authors: Jochen Schiller

2nd edition

978-0321123817, 321123816, 978-8131724262

Students also viewed these Programming questions