You are on page 1of 3

RandomWriter.

cpp

8/2/13 12:44 PM

#include <iostream> #include <String> #include "console.h" #include"tokenscanner.h" #include "fstream" #include "map.h" #include "filelib.h" #include "vector.h" #include "simpio.h" #include "random.h" using namespace std; /* Function Prototypes and Constants*/ const int textLength = 2000; void createSeedMap(int order, ifstream & input, Map <string, Vector<char> > & seedMap); string findMostCommonSeed(Map <string, Vector<char> > & seedMap); void randomWriting(string mostCommonSeed, Map <string, Vector<char> > & seedMap, int order); int main() { /* Open the file for reading. */ ifstream input; promptUserForFile(input, "Enter the source text: "); int order = getInteger("Enter the Markov order [1-10] "); while (true) { if (order > 0 && order <= 10) { break; }else{ cout << "Please enter a number from 1 to 10." << endl; order = getInteger("Enter the Markov order [1-10] "); } } /* Initialize and create the seed map */ Map <string, Vector<char> > seedMap; createSeedMap(order, input, seedMap); /* Find the most common seed and begin the random writing procedure based on this seed */ string mostCommonSeed = findMostCommonSeed(seedMap); randomWriting(mostCommonSeed, seedMap, order); return 0; } /** This function reads in the source text and creates a map in which the keys are seeds (strings with * a length that corresponds to the Markov order) and the values are vectors of the characters * that follow the associated seed.
Page 1 of 3

RandomWriter.cpp

8/2/13 12:44 PM

*/ void createSeedMap(int order, ifstream & input, Map <string, Vector<char> > & seedMap) { char ch; string seed; int seedLength = order; /* Get the initial seed */ for (int i = 0; i < seedLength; i++ ) { input.get(ch); seed += ch; } while (input.get(ch)) { /* If there is already an entry for this seed, add the following char to the * vector of chars associated with the seed. */ if (seedMap.containsKey(seed)) { Vector<char> updateFrequencies = seedMap.get(seed); updateFrequencies += ch; seedMap.put(seed, updateFrequencies); /* If there is no entry for this seed, creates a vector of chars associated * with the seed and adds the char to this vector. */ }else { Vector <char> charFrequencies; charFrequencies += ch; seedMap.put(seed, charFrequencies); } /* Shifts the seed one character over in the source text */ seed = seed.substr(1) + ch; } /* Account for the exceptional case of the final seed in the source text and adds * it to the seed map. */ if (!seedMap.containsKey(seed)) { Vector <char> charFrequencies; charFrequencies += ch; seedMap.put(seed, charFrequencies); } } /** This function returns the sequence that appears most frequently in the source, with * the length of the seed corresponding to the order number of the Markov model. */ string findMostCommonSeed(Map <string, Vector<char> > & seedMap) { string mostCommonSeed; int maxFrequencies = 0;
Page 2 of 3

RandomWriter.cpp

8/2/13 12:44 PM

/* Iterates through the map of seeds and finds the most frequently appearing seed * based on the size of the associated vector of characters (the size of which * reflects how often the seed is seen in the text. */ foreach(string seed in seedMap) { Vector <char> frequencies = seedMap.get(seed); int size = frequencies.size(); if (size > maxFrequencies) { mostCommonSeed = seed; maxFrequencies = size; } } return mostCommonSeed; } /** This function outputs 2000 characters of random text generated from the Markov model. */ void randomWriting(string mostCommonSeed, Map <string, Vector<char> > & seedMap, int order) { Vector <char> chars = seedMap.get(mostCommonSeed); string currentSeed = mostCommonSeed; cout << mostCommonSeed; for (int i = order; i < textLength; i++ ) { Vector <char> chars = seedMap.get(currentSeed); if (chars.isEmpty()) { break; } /* Randomly generates a character from the vector of characters that follow the * corresponding seed in the source text */ int index = randomInteger(0, chars.size()-1); char next = chars.get(index); /* Shifts the seed one character over in the output text, prints out the most recently * generated character, and then updates variable that keeps track of the current seed. */ string nextSeed = currentSeed.substr(1) + next; cout << next; currentSeed = nextSeed; } cout << endl; }

Page 3 of 3

You might also like