about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--malaprop.cpp137
-rw-r--r--malaprop.h31
2 files changed, 0 insertions, 168 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}
diff --git a/malaprop.h b/malaprop.h deleted file mode 100644 index 91a18eb..0000000 --- a/malaprop.h +++ /dev/null
@@ -1,31 +0,0 @@
1#ifndef MALAPROP_H_8F382336
2#define MALAPROP_H_8F382336
3
4#include <string>
5#include <map>
6#include <set>
7
8class malaprop
9{
10public:
11 void addWord(std::string word);
12 void stats();
13 std::string alternate(std::string word);
14
15private:
16 struct soundex {
17 char prefix;
18 int code;
19
20 bool operator<(const soundex& other) const
21 {
22 return (prefix < other.prefix) || (code < other.code);
23 }
24 };
25
26 std::map<soundex, std::set<std::string> > dict;
27
28 soundex soundify(std::string l);
29};
30
31#endif /* end of include guard: MALAPROP_H_8F382336 */