-
Notifications
You must be signed in to change notification settings - Fork 0
/
mouthwash.cpp
165 lines (136 loc) · 4.68 KB
/
mouthwash.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//========================================================================
// This conversion was produced by the Free Edition of
// Java to C++ Converter courtesy of Tangible Software Solutions.
// Order the Premium Edition at https://www.tangiblesoftwaresolutions.com
//========================================================================
#include "mouthwash.h"
namespace mouthwash
{
AntiCurse::AntiCurse(const std::wstring &text) : badWords(std::vector<std::wstring>{L"bad", L"martin", L"okay"}), correctiveSymbol(L"*****")
{
this->text = text;
this->parsedText = text;
}
AntiCurse::AntiCurse(const std::wstring &text, std::vector<std::wstring> &listOfBadWord) : badWords(listOfBadWord), correctiveSymbol(L"*****")
{
this->text = text;
this->parsedText = text;
}
void AntiCurse::run()
{
for (int i = 0; i < badWords.size(); i++)
{
std::wstring currentBadWord = badWords[i];
//Phase 1: Replace all bad words with exact match
parsedText = parsedText.replaceAll(currentBadWord, correctiveSymbol);
//Phase 2: Find Exaggerated bad words. for instance 'Bad' --> 'Baaaaadd'
int searchingIndex = 0;
while (searchingIndex < parsedText.length())
{
std::wstring firstLetterOfCurrentBadWord = StringHelper::toString(currentBadWord[0])->toLowerCase();
//Get index of first letter
int indexFirstLetter = StringHelper::toLower(parsedText)->find(L" " + firstLetterOfCurrentBadWord, searchingIndex);
//Get index of last letter
if (indexFirstLetter != -1)
{
int indexLastLetter1 = StringHelper::toLower(parsedText)->find(L" ", indexFirstLetter + 1);
int indexLastLetter2 = StringHelper::toLower(parsedText)->find(L"\t", indexFirstLetter + 1);
int indexLastLetter3 = StringHelper::toLower(parsedText)->find(L"\n", indexFirstLetter + 1);
int indexLastLetter = StringHelper::toLower(parsedText)->length() - 1;
if (indexLastLetter1 != -1)
{
indexLastLetter = indexLastLetter1;
}
else if (indexLastLetter2 != -1)
{
indexLastLetter = indexLastLetter2;
}
else if (indexLastLetter3 != -1)
{
indexLastLetter = indexLastLetter3;
}
//get potential bad word
std::wstring word = parsedText.substr(indexFirstLetter + 1, indexLastLetter - (indexFirstLetter + 1));
//following sequence of letters to uncover bad word
int indexOfCurrentWordLetter = 0;
bool wordIsBad = true;
for (int j = 0; j < word.length(); j++)
{
if (!(StringHelper::toString(currentBadWord[indexOfCurrentWordLetter])->toLowerCase()->equals(StringHelper::toString(word[j])->toLowerCase())))
{
//if the current character is not a letter from the alphabet
if (!letterIsInAlphabet(StringHelper::toLower(word)->charAt(j)))
{
continue;
}
//look at the next letter is continuing the bad word sequence
if (indexOfCurrentWordLetter < currentBadWord.length() - 1)
{
if (StringHelper::toString(currentBadWord[indexOfCurrentWordLetter + 1])->toLowerCase()->equals(StringHelper::toString(word[j])->toLowerCase()))
{
//make loop repeat with different index
indexOfCurrentWordLetter++;
--j;
}
else
{
wordIsBad = false;
}
}
else
{
wordIsBad = false;
}
}
else
{
if (j == word.length() - 1)
{
//if the letter of the last word matches the last letter of the criteria
if (!StringHelper::toString(word[j])->toLowerCase()->equals(StringHelper::toString(currentBadWord[currentBadWord.length() - 1])->toLowerCase()))
{
wordIsBad = false;
}
}
}
}
//replace word if it is bad
if (wordIsBad)
{
parsedText = parsedText.replaceAll(word, correctiveSymbol);
//reset loop
searchingIndex = -1;
}
}
//continue loop
searchingIndex++;
}
}
}
bool AntiCurse::letterIsInAlphabet(wchar_t letter)
{
std::vector<wchar_t> alphabet = {L'a', L'b', L'c', L'd', L'e', L'f', L'g', L'h', L'i', L'j', L'k', L'l', L'm', L'n', L'o', L'p', L'q', L'r', L's', L't', L'u', L'v', L'w', L'x', L'y', L'z'};
bool exist = false;
for (int i = 0; i < alphabet.size(); i++)
{
if (alphabet[i] == letter)
{
exist = true;
}
}
return exist;
}
void AntiCurse::setText(const std::wstring &text)
{
this->text = text;
this->parsedText = text;
}
std::wstring AntiCurse::getText()
{
return text;
}
std::wstring AntiCurse::getParsedText()
{
return parsedText;
}
}