-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWordFilter.cpp
More file actions
186 lines (155 loc) · 3.9 KB
/
Copy pathWordFilter.cpp
File metadata and controls
186 lines (155 loc) · 3.9 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "WordFilter.h"
#include "LogComm.h"
#define PACE 1
WordFilter *WordFilter::pInstace = NULL;
WordNode::WordNode(string character)
{
if (character.size() == PACE)
m_character.assign(character);
else
cout << "error" << endl;
}
WordNode *WordNode::findChild(string &nextCharacter)
{
auto TreeMapIt = m_map.find(nextCharacter);
if (TreeMapIt == m_map.end())
return NULL;
return &TreeMapIt->second;
}
WordNode *WordNode::insertChild(string &nextCharacter)
{
if (!findChild(nextCharacter))
{
m_map.insert(pair<string, WordNode>(nextCharacter, WordNode(nextCharacter)));
return &(m_map.find(nextCharacter)->second);
}
return NULL;
}
WordNode *WordTree::insert(string &keyword)
{
return insert(&m_emptyRoot, keyword);
}
WordNode *WordTree::insert(const char *keyword)
{
string wordstr(keyword);
return insert(wordstr);
}
WordNode *WordTree::find(string &keyword)
{
return find(&m_emptyRoot, keyword);
}
WordNode *WordTree::insert(WordNode *parent, string &keyword)
{
if (keyword.empty())
{
return NULL;
}
string firstChar = keyword.substr(0, PACE);
auto firstNode = parent->findChild(firstChar);
if (!firstNode)
{
return insertBranch(parent, keyword);
}
string restChar = keyword.substr(PACE, keyword.size());
return insert(firstNode, restChar);
}
WordNode *WordTree::insertBranch(WordNode *parent, string &keyword)
{
string firstChar = keyword.substr(0, PACE);
auto firstNode = parent->insertChild(firstChar);
if (firstNode)
{
string restChar = keyword.substr(PACE, keyword.size());
if (!restChar.empty())
{
return insertBranch(firstNode, restChar);
}
}
return NULL;
}
WordNode *WordTree::find(WordNode *parent, string &keyword)
{
string firstChar = keyword.substr(0, PACE);
auto firstNode = parent->findChild(firstChar);
if (!firstNode)
{
count = 0;
return NULL;
}
string restChar = keyword.substr(PACE, keyword.size());
if (firstNode->m_map.empty())
{
return firstNode;
}
if (keyword.size() == PACE)
{
return NULL;
}
count++;
return find(firstNode, restChar);
}
WordFilter *WordFilter::sharedInstace()
{
if (pInstace)
{
return pInstace;
}
pInstace = new WordFilter;
return pInstace;
}
void WordFilter::release()
{
if (pInstace)
{
delete pInstace;
}
pInstace = NULL;
}
void WordFilter::load(const char *filepath)
{
wbl::WriteLocker lock(m_rwlock);
ifstream infile(filepath, ios::in);
if (!infile)
{
ROLLLOG_ERROR << "open file error" << endl;
return;
}
ROLLLOG_DEBUG << "open file succeed" << endl;
string read;
while (getline(infile, read))
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
string s;
s = read.substr(0, read.length() - 1);
m_tree.insert(s);
#endif
}
infile.close();
}
void WordFilter::load(const vector<string> &words)
{
wbl::WriteLocker lock(m_rwlock);
for (auto it = words.begin(); it != words.end(); ++it)
{
string s = *it;
m_tree.insert(s);
}
}
bool WordFilter::censor(string &source)
{
wbl::WriteLocker lock(m_rwlock);
bool bRet = false;
int lenght = source.size();
for (int i = 0; i < lenght; i += 1)
{
string substring = source.substr(i, lenght - i);
if (m_tree.find(substring) != NULL)
{
source.replace(i, (m_tree.count + 1), "**");
lenght = source.size();
bRet = true;
// cout << "source = " << source << endl;
}
}
return bRet;
}