Question: #include pch . h #include MarkovChain.h #include #include #include #include #include #include #include #include / / For std::min using namespace std; string
#include pchh
#include "MarkovChain.h
#include
#include
#include
#include
#include
#include
#include
#include For std::min
using namespace std;
string pickRandomWordconst wordsAndCountst& wordsAndCounts
unsigned int totalTickets ;
for const auto& pair : wordsAndCounts
totalTickets pair.second;
if totalTickets
throw runtimeerrorNo words to pick from.";
unsigned int winningTicket rand totalTickets;
unsigned int currentTicket ;
for const auto& pair : wordsAndCounts
currentTicket pair.second;
if winningTicket currentTicket
return pair.first;
throw runtimeerrorFailed to pick a random word.";
void MarkovChain::initializeChainsconst vector& words
sizet end order words.size : words.size order;
for sizet i ; i end; i
vector key;
Create Ngrams if order isn't
if order
key.assignwordsbegin i words.begin i order;
Determine the next word and increment the chain
string nextWord order wordsi : wordsi order;
chainskeynextWord;
For order the key should be an empty vector
if order
vector emptyKey;
for const auto& word : words
chainsemptyKeyword;
string MarkovChain::generateTextconst unsigned int length const
if chainsempty
throw EmptyMarkovChainException; Throw exception if chains are not initialized
vector generatedText;
unsigned int maxLength minlengthU;
if order
Directly generate random words from the source text for grams
const auto& wordMap chains.at;
if wordMapempty
throw runtimeerrorNo words available for zeroorder generation.";
vector allWords;
for const auto& pair : wordMap
for unsigned int i ; i pair.second; i
allWords.pushbackpairfirst;
for unsigned int i ; i maxLength; i
string nextWord allWordsrand allWords.size;
generatedText.pushbacknextWord;
else
Initialize with a random ngram
auto it chains.begin;
advanceit rand chains.size;
vector currentNgram itfirst;
generatedText.insertgeneratedTextend currentNgram.begin currentNgram.end;
while generatedTextsize maxLength
auto nextWordsIt chains.findcurrentNgram;
if nextWordsIt chains.end nextWordsItsecond.empty
break; No possible next words
string nextWord pickRandomWordnextWordsItsecond;
generatedText.pushbacknextWord;
Cycle to the next ngram
currentNgram.erasecurrentNgrambegin;
currentNgram.pushbacknextWord;
Join generated words into a single string
stringstream result;
for sizet i ; i generatedText.size; i
if i
result ;
result generatedTexti;
return result.str;
vector readCorpusconst string fileName
ifstream filefileName;
if file.isopen
cerr "Unable to open file" endl;
throw runtimeerrorCould not open file";
vector words;
string word;
while file word
words.pushbackword;
file.close;
return words;
TESTGenerationTest RejectsOverfitting
srand;
MarkovChain mc;
const vector words readCorpuscorpussmall.txt;
mcinitializeChainswords;
ASSERTTHROW
try
mcgenerateText;
catch const OverfittingException& e
and this tests that it has the correct message
ASSERTSTREQOrder was too high! Overfitting occured. Only one candidate was available at each step.", ewhat;
throw;
OverfittingException;
The code is failing these two test
TESTGenerationTest GenerateGrams
srand;
MarkovChain mc;
const vector words readCorpuscorpussmall.txt;
mcinitializeChainswords;
const auto& chains mcgetChains;
ASSERTEQchainssize "Expected grams chains to have a single key.";
const auto numUniqueWords setwordsbegin words.endsize;
const auto onlyValueSize chains.atsize;
ASSERTEQnumUniqueWords onlyValueSizegrams chain empty vector value should be the size of the words, which is numU
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
