From 01746a0e03267b6c082b58436c1370567f7cb7c5 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sun, 22 Nov 2015 18:49:58 -0500 Subject: Added malapropisms --- malaprop.cpp | 127 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 malaprop.cpp (limited to 'malaprop.cpp') diff --git a/malaprop.cpp b/malaprop.cpp new file mode 100644 index 0000000..bfea579 --- /dev/null +++ b/malaprop.cpp @@ -0,0 +1,127 @@ +#include "malaprop.h" +#include +#include + +bool removeIfM(char c) +{ + return !isalpha(c); +} + +char soundID(char l) +{ + switch (l) + { + case 'b': + case 'f': + case 'p': + case 'v': + return '1'; + + case 'c': + case 'g': + case 'j': + case 'k': + case 'q': + case 's': + case 'x': + case 'z': + return '2'; + + case 'd': + case 't': + return '3'; + + case 'l': + return '4'; + + case 'm': + case 'n': + return '5'; + + case 'r': + return '6'; + } + + return l; +} + +std::string canonizetwo(std::string f) +{ + std::string canonical(f); + std::transform(canonical.begin(), canonical.end(), canonical.begin(), ::tolower); + + std::string result; + std::remove_copy_if(canonical.begin(), canonical.end(), std::back_inserter(result), removeIfM); + + return result; +} + +malaprop::soundex malaprop::soundify(std::string f) +{ + std::string result(canonizetwo(f)); + + soundex ex; + ex.prefix = result[0]; + + std::string output; + + for (int i = 1; i >::iterator it = dict.begin(); it != dict.end(); it++) + { + printf("%c%03d (%d): ", it->first.prefix, it->first.code, it->second.size()); + + for (std::set::iterator jt = it->second.begin(); jt != it->second.end(); jt++) + { + std::cout << *jt << ", "; + } + + std::cout << std::endl; + } + + exit(0); +} + +std::string malaprop::alternate(std::string word) +{ + soundex ex = soundify(word); + std::set& opts = dict[ex]; + int opt = rand() % opts.size(); + for (std::set::iterator it = opts.begin(); it != opts.end(); it++) + { + if (opt == 0) + { + return *it; + } + + opt--; + } + + return word; +} -- cgit 1.4.1