about summary refs log tree commit diff stats
path: root/malaprop.cpp
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2016-09-11 02:47:25 -0400
committerKelly Rauchenberger <fefferburbia@gmail.com>2016-09-11 02:47:25 -0400
commit4a02cefc939c8b35353927fa0276b267996902d5 (patch)
tree7fd76dc9867bf117a6b6369cb73c3551c4c8dff0 /malaprop.cpp
parentaf41efa7c49fb8beec4484f67fd485621abe7169 (diff)
downloadrawr-ebooks-4a02cefc939c8b35353927fa0276b267996902d5.tar.gz
rawr-ebooks-4a02cefc939c8b35353927fa0276b267996902d5.tar.bz2
rawr-ebooks-4a02cefc939c8b35353927fa0276b267996902d5.zip
Removed legacy malaprop code
Diffstat (limited to 'malaprop.cpp')
-rw-r--r--malaprop.cpp137
1 files changed, 0 insertions, 137 deletions
diff --git a/malaprop.cpp b/malaprop.cpp deleted file mode 100644 index ff0cb5d..0000000 --- a/malaprop.cpp +++ /dev/null
@@ -1,137 +0,0 @@
1#include "malaprop.h"
2#include <cstdlib>
3#include <iostream>
4#include <algorithm>
5#include <cstdio>
6
7bool removeIfM(char c)
8{
9 return !isalpha(c);
10}
11
12char soundID(char l)
13{
14 switch (l)
15 {
16 case 'b':
17 case 'f':
18 case 'p':
19 case 'v':
20 return '1';
21
22 case 'c':
23 case 'g':
24 case 'j':
25 case 'k':
26 case 'q':
27 case 's':
28 case 'x':
29 case 'z':
30 return '2';
31
32 case 'd':
33 case 't':
34 return '3';
35
36 case 'l':
37 return '4';
38
39 case 'm':
40 case 'n':
41 return '5';
42
43 case 'r':
44 return '6';
45 }
46
47 return l;
48}
49
50std::string canonizetwo(std::string f)
51{
52 std::string canonical(f);
53 std::transform(canonical.begin(), canonical.end(), canonical.begin(), ::tolower);
54
55 std::string result;
56 std::remove_copy_if(canonical.begin(), canonical.end(), std::back_inserter(result), removeIfM);
57
58 return result;
59}
60
61malaprop::soundex malaprop::soundify(std::string f)
62{
63 std::string result(canonizetwo(f));
64
65 soundex ex;
66 ex.prefix = result[0];
67
68 std::string output;
69
70 for (int i = 1; i<result.length(); i++)
71 {
72 int c = soundID(result[i]);
73 if (
74 (isdigit(c)) // Not a vowel
75 && (c != soundID(result[i-1])) // Not the same as the previous character
76 && ((i < 2) || ((result[i-1] = 'h' || result[i-1] == 'w') && (c != soundID(result[i-2])))) // Not same as before h/w
77 )
78 {
79 output += c;
80 }
81 }
82
83 output.resize(3, '0');
84 ex.code = atoi(output.c_str());
85
86 return ex;
87}
88
89void malaprop::addWord(std::string word)
90{
91 soundex ex = soundify(word);
92
93 if (ex.prefix != 0)
94 {
95 dict[ex].insert(canonizetwo(word));
96 }
97}
98
99void malaprop::stats()
100{
101 for (std::map<soundex, std::set<std::string> >::iterator it = dict.begin(); it != dict.end(); it++)
102 {
103 printf("%c%03d (%d): ", it->first.prefix, it->first.code, it->second.size());
104
105 for (std::set<std::string>::iterator jt = it->second.begin(); jt != it->second.end(); jt++)
106 {
107 std::cout << *jt << ", ";
108 }
109
110 std::cout << std::endl;
111 }
112
113 exit(0);
114}
115
116std::string malaprop::alternate(std::string word)
117{
118 soundex ex = soundify(word);
119 std::set<std::string>& opts = dict[ex];
120 if (opts.size() == 0)
121 {
122 return word;
123 }
124
125 int opt = rand() % opts.size();
126 for (std::set<std::string>::iterator it = opts.begin(); it != opts.end(); it++)
127 {
128 if (opt == 0)
129 {
130 return *it;
131 }
132
133 opt--;
134 }
135
136 return word;
137}