This week lecture code for Lucky Sentence

I made some edits to the code.

I created two functions that assign values based on the package.

// Package loader, 

void custom_package(vector<string> &prepositions, vector<string> &verbs, vector<string> &adverbs, vector<string> &nouns, vector<string> &adjectives) { string package_folder = "package_folder"; // Location of the packages

read_words(package_folder + "/adjectives.txt", adjectives); read_words(package_folder + "/nouns.txt", nouns); read_words(package_folder + "/adverbs.txt", adverbs); read_words(package_folder + "/verbs.txt", verbs); read_words(package_folder + "/prepositions.txt", prepositions); }

// Default package, void default_package(vector<string> &prepositions, vector<string> &verbs, vector<string> &adverbs, vector<string> &nouns, vector<string> &adjectives) { prepositions = { "on", "under", "over"}; verbs = { "ran", "danced", "walked", "drew", "floated", "flew"}; adverbs = { "swiftly", "broodingly", "happily", "angrily", "jokingly", "firmly", "slowly", "carefully"}; nouns = { "cat", "dog", "bird", "bottle", "fork", "apple", "orange", "watch", "flower"}; adjectives = { "tall", "abandoned", "angry", "happy", "sad", "lovely", "messy", ""}; }

I also combined the two sentence-generators into one that calls the package loader based on argc from main. Lastly, I created a string that takes all the generated words in so we can do more to the program later on.

void generate_lucky_sentence(int argc) {

vector<string> prepositions, verbs, adverbs, nouns, adjectives;

if (argc == 1) // Load package based on argc default_package(prepositions, verbs, adverbs, nouns, adjectives); else custom_package(prepositions, verbs, adverbs, nouns, adjectives);

// Pick random components and string them together // to make: (ART ADJ N) ADV V PP (ART ADJ N) string adj1 = get_random_word_from_vector(adjectives); string n1 = get_random_word_from_vector(nouns); string art1 = get_random_article(adj1, n1); string adv = get_random_word_from_vector(adverbs); string v = get_random_word_from_vector(verbs); string pp = get_random_word_from_vector(prepositions); string adj2 = get_random_word_from_vector(adjectives); string n2 = get_random_word_from_vector(nouns); string art2 = get_random_article(adj2, n2); string luckySentence = art1 + " " + adj1 + " " + n1 + " " + adv + " " + v + " " + pp + " " + art2 + " " + adj2 + " " + n2;

cout << "Your lucky sentence of the day is:" << endl; cout<< luckySentence; }

Here is the full code:

// This program is for generating a random sentence like the following:

// Example: The angry cat broodingly sat on the blue mat // template: ART ADJ N ADV V PP ART ADJ N // Basic idea to pick random one of each of the above categories

include <iostream>

include <vector>

include <sstream>

include <fstream>

using namespace std;

// .txt file reader void read_words(string filename, vector<string> &vec) { ifstream ifs(filename); string word;

vec.clear(); while (getline(ifs, word)) vec.push_back(word); }

// Package loader, void custom_package(vector<string> &prepositions, vector<string> &verbs, vector<string> &adverbs, vector<string> &nouns, vector<string> &adjectives) { string package_folder = "package_folder"; // Location of the packages

read_words(package_folder + "/adjectives.txt", adjectives); read_words(package_folder + "/nouns.txt", nouns); read_words(package_folder + "/adverbs.txt", adverbs); read_words(package_folder + "/verbs.txt", verbs); read_words(package_folder + "/prepositions.txt", prepositions); }

// Default package, void default_package(vector<string> &prepositions, vector<string> &verbs, vector<string> &adverbs, vector<string> &nouns, vector<string> &adjectives) { prepositions = { "on", "under", "over"}; verbs = { "ran", "danced", "walked", "drew", "floated", "flew"}; adverbs = { "swiftly", "broodingly", "happily", "angrily", "jokingly", "firmly", "slowly", "carefully"}; nouns = { "cat", "dog", "bird", "bottle", "fork", "apple", "orange", "watch", "flower"}; adjectives = { "tall", "abandoned", "angry", "happy", "sad", "lovely", "messy", ""}; }

string get_random_word_from_vector(const vector<string> vec) { if (vec.size() == 0) return ""; return vec[rand() % vec.size()]; }

// Return "a", "an", or "the" depending on letter that follows string get_random_article(string adj, string noun) { // Find first letter char c = (adj.length() == 0 ? noun[0] : adj[0]); if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') return (rand() % 2 == 0 ? "the" : "an"); return (rand() % 2 == 0 ? "the" : "a"); }

void generate_lucky_sentence(int argc) {

vector<string> prepositions, verbs, adverbs, nouns, adjectives;

if (argc == 1) // Load package based on argc default_package(prepositions, verbs, adverbs, nouns, adjectives); else custom_package(prepositions, verbs, adverbs, nouns, adjectives);

// Pick random components and string them together // to make: (ART ADJ N) ADV V PP (ART ADJ N) string adj1 = get_random_word_from_vector(adjectives); string n1 = get_random_word_from_vector(nouns); string art1 = get_random_article(adj1, n1); string adv = get_random_word_from_vector(adverbs); string v = get_random_word_from_vector(verbs); string pp = get_random_word_from_vector(prepositions); string adj2 = get_random_word_from_vector(adjectives); string n2 = get_random_word_from_vector(nouns); string art2 = get_random_article(adj2, n2); string luckySentence = art1 + " " + adj1 + " " + n1 + " " + adv + " " + v + " " + pp + " " + art2 + " " + adj2 + " " + n2;

cout << "Your lucky sentence of the day is:" << endl; cout<< luckySentence; }

int main(int argc, char *argv[]) { srand((unsigned)time(0L));

generate_lucky_sentence(argc);

return 0; }

/r/cs2a Thread