/
Soundex.cpp
95 lines (80 loc) · 3.09 KB
/
Soundex.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <string>
#include <unordered_map>
class Soundex {
public:
static const size_t MaxCodeLength { 4 };
const std::string NotADigit{"*"};
std::string encode(const std::string& word) const {
return zeroPad(upperFront(head(word)) + tail(encodedDigits(word)));
}
std::string encodedDigit(char letter) const {
const std::unordered_map<char, std::string> encodings {
{'b', "1"}, {'f', "1"}, {'p', "1"}, {'v', "1"},
{'c', "2"}, {'g', "2"}, {'j', "2"}, {'k', "2"}, {'q', "2"},
{'s', "2"}, {'x', "2"}, {'z', "2"},
{'d', "3"}, {'t', "3"},
{'l', "4"},
{'m', "5"}, {'n', "5"},
{'r', "6"}
};
auto iterator = encodings.find(lower(letter));
return encodings.end() == iterator ? NotADigit : iterator->second;
}
private:
std::string encodedDigits(const std::string& word) const {
std::string encoding;
encodeHead(encoding, word);
encodeTail(encoding, word);
return encoding;
}
void encodeHead(std::string& encoding, const std::string& word) const {
encoding += encodedDigit(word.front());
}
void encodeTail(std::string& encoding, const std::string& word) const {
for (auto i = 1u; i < word.length(); i++) {
if (!isComplete(encoding)) {
encodeLetter(encoding, word[i], word[i - 1]);
}
}
}
void encodeLetter(std::string& encoding,
char letter, char lastLetter) const
{
auto digit = encodedDigit(letter);
if (NotADigit != digit &&
(lastDigit(encoding) != digit || isVowel(lastLetter)))
{
encoding += digit;
}
}
bool isComplete(const std::string& encoding) const {
return encoding.length() == MaxCodeLength;
}
std::string lastDigit(const std::string& encoding) const {
if (encoding.empty()) {
return NotADigit;
}
return std::string(1, encoding.back());
}
bool isVowel(char letter) const {
return
std::string("aeiouy").find(lower(letter)) != std::string::npos;
}
std::string upperFront(const std::string& string) const {
return std::string(1,
std::toupper(static_cast<unsigned char>(string.front())));
}
char lower(char c) const {
return std::tolower(static_cast<unsigned char>(c));
}
std::string zeroPad(const std::string& word) const {
auto zerosNeeded = MaxCodeLength - word.length();
return word + std::string(zerosNeeded, '0');
}
std::string head(const std::string& word) const {
return word.substr(0, 1);
}
std::string tail(const std::string& word) const {
return word.substr(1);
}
};