From 9e89002477d1358de9be9cabdc1edba26bd32836 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Mon, 4 Jan 2016 23:16:17 -0500 Subject: Rewrote quite a bit of kgramstats The algorithm still treats most tokens literally, but now groups together tokens that terminate a clause somehow (so, contain .?!,), without distinguishing between the different terminating characters. For each word that can terminate a sentence, the algorithm creates a histogram of the terminating characters and number of occurrences of those characters for that word (number of occurrences is to allow things like um???? and um,,,,, to still be folded down into um.). Then, when the terminating version of that token is invoked, a random terminating string is added to that token based on the histogram for that word (again, to allow things like the desu-ly use of multiple commas to end clauses). The algorithm now also has a slightly advanced kgram structure; a special "sentence wildcard" kgram value is set aside from normal strings of tokens that can match any terminating token. This kgram value is never printed (it is only ever present in the query kgrams and cannot actually be present in the histograms (it is of a different datatype)) and is used at the beginning of sentence generation to make sure that the first couple of words generated actually form the beginning of a sentence instead of picking up somewhere in the middle of a sentence. It is also used to reset sentence generation in the rare occasion that the end of the corpus is reached. --- gen.cpp | 98 +++++++++++++++++++++++++++++++---------------------------------- 1 file changed, 46 insertions(+), 52 deletions(-) (limited to 'gen.cpp') diff --git a/gen.cpp b/gen.cpp index 7e47d45..400c0a5 100644 --- a/gen.cpp +++ b/gen.cpp @@ -11,72 +11,66 @@ int main(int argc, char** args) { - srand(time(NULL)); + srand(time(NULL)); - if (argc == 1) - { - std::cout << "rawr-gen, version 1.0" << std::endl; - std::cout << "Usage: rawr-gen corpus-file" << std::endl; - std::cout << " where 'corpus-file' is the path to your input" << std::endl; + if (argc == 1) + { + std::cout << "rawr-gen, version 1.0" << std::endl; + std::cout << "Usage: rawr-gen corpus-file" << std::endl; + std::cout << " where 'corpus-file' is the path to your input" << std::endl; - return 0; - } + return 0; + } - std::ifstream infile(args[1]); - if (!infile) - { - std::cout << "rawr-gen, version 1.0" << std::endl; - std::cout << "Usage: rawr-gen corpus-file" << std::endl; - std::cout << " where 'corpus-file' is the path to your input" << std::endl; - std::cout << std::endl; - std::cout << "The file you specified does not exist." << std::endl; + std::ifstream infile(args[1]); + if (!infile) + { + std::cout << "rawr-gen, version 1.0" << std::endl; + std::cout << "Usage: rawr-gen corpus-file" << std::endl; + std::cout << " where 'corpus-file' is the path to your input" << std::endl; + std::cout << std::endl; + std::cout << "The file you specified does not exist." << std::endl; - return 0; - } + return 0; + } - std::string corpus; - std::string line; - while (getline(infile, line)) - { - corpus += line + "\n "; - } + std::string corpus; + std::string line; + while (getline(infile, line)) + { + corpus += line + "\n "; + } - std::cout << "Preprocessing corpus..." << std::endl; - kgramstats* stats = new kgramstats(corpus, 4); + std::cout << "Preprocessing corpus..." << std::endl; + kgramstats* stats = new kgramstats(corpus, 4); - std::cout << "Preprocessing freevars..." << std::endl; - freevars* vars = new freevars(); - vars->addVar("name", "names.txt"); - vars->addVar("noun", "nouns.txt"); + std::cout << "Preprocessing freevars..." << std::endl; + freevars* vars = new freevars(); + vars->addVar("name", "names.txt"); + vars->addVar("noun", "nouns.txt"); - std::cout << "Generating..." << std::endl; - for (;;) - { - std::vector doc = stats->randomSentence(rand() % 35 + 45); - std::string hi; - for (std::vector::iterator it = doc.begin(); it != doc.end(); ++it) - { - hi += vars->parse(*it) + " "; - } - - size_t firstperiod = hi.find_first_of(".!?"); - if (firstperiod != std::string::npos) + std::cout << "Generating..." << std::endl; + for (;;) + { + std::vector doc = stats->randomSentence(rand() % 35 + 15); + std::string hi; + for (std::vector::iterator it = doc.begin(); it != doc.end(); ++it) { - hi = hi.substr(firstperiod+2); + hi += vars->parse(*it) + " "; } hi.resize(140); - size_t lastperiod = hi.find_last_of(".!?"); - if ((lastperiod != std::string::npos) && (rand() % 3 > 0)) - { - hi = hi.substr(0, lastperiod+1); - } + size_t lastperiod = hi.find_last_of(".!?,"); + if ((lastperiod != std::string::npos) && (rand() % 3 > 0)) + { + hi = hi.substr(0, lastperiod+1); + } - std::cout << hi << std::endl; + std::cout << hi << std::endl; - getc(stdin); - } + getc(stdin); + } - return 0; + return 0; } -- cgit 1.4.1