-
Notifications
You must be signed in to change notification settings - Fork 1
/
keywordTrie.hpp
314 lines (297 loc) · 9.88 KB
/
keywordTrie.hpp
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
* Copyright (C) 2019 Michael Schellenberger Costa.
*
* This code is based on a C-implementation of the keyword trie construction,
* preprocessing and text search by Bernhard Haubold.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef MISCCO_KEYWORDTRIE_HPP
#define MISCCO_KEYWORDTRIE_HPP
#include <cctype>
#include <memory>
#include <queue>
#include <set>
#include <stdexcept>
#include <string>
#include <vector>
namespace miscco
{
/**
* @brief The trie class representing the keyword trie.
*/
template <bool CaseSensitive = true>
class keyword_trie
{
public:
/**
* @brief The Result struct containing the information about matches during a
* search.
*/
struct Result
{
const std::string keyword; /**< The found keyword */
const std::size_t id; /**< The index of the keyword in the keyword list*/
std::size_t start; /**< The starting position of the match */
std::size_t end; /**< The end position of the match */
explicit Result(const std::string &key, const std::size_t id)
: keyword(key), id(id)
{
}
explicit Result(const Result &res, const std::size_t endPos)
: keyword(res.keyword), id(res.id), start(endPos - res.keyword.size() + 1), end(endPos)
{
}
};
private:
/**
* @brief The Node struct containing the information of a trie Node.
*/
struct Node
{
int id = -1; /**< Keyword index */
const int depth = 0; /**< Depth in the trie*/
const char c = '\0'; /**< Character labelling the incoming edge */
Node *parent; /**< Parent Node */
Node *failure; /**< Failure link */
Node *output; /**< Output link */
std::vector<Node *> children; /**< Child Nodes */
explicit Node() = default;
explicit Node(const int d, const char character, Node *par, Node *root)
: depth(d), c(character), parent(par), failure(root), output(root)
{
}
};
Node *root; /**< The root Node */
std::vector<std::unique_ptr<Node>> trieNodes; /**< Container of the Node pointers */
std::vector<Result> keywords; /**< Container of the Result stubs */
public:
/**
* @brief trie Initializes the trie structure with its root Node.
*/
keyword_trie()
{
trieNodes.emplace_back(std::make_unique<Node>());
root = trieNodes.front().get();
root->parent = root;
root->failure = root;
root->output = root;
}
/**
* @brief addString Insert a new keyword into the keyword trie.
* @param key The new keyword to be inserted.
* @param addFailure Flag to signal whether the failure links should
* immediately be updated.
*/
void addString(const std::string &key, const bool addFailure = true)
{
if (key.empty())
{
return;
}
Node *current = root;
for (const char character : key)
{
current = addChild(current, CaseSensitive ? character : std::tolower(character));
}
if (current->id != -1)
{
throw std::runtime_error(
"Attempted to add two identical strings to the keyword tree.");
}
current->id = keywords.size();
keywords.emplace_back(key, keywords.size());
if (addFailure)
{
addFailureLinks();
}
}
/**
* @brief addString Wrapper around addString(std::string, bool) to add a
* set of strings.
* @param keyList The set containing the keys.
*/
void addString(const std::set<std::string> &keyList)
{
for (const std::string &key : keyList)
{
addString(key, false);
}
addFailureLinks();
}
/**
* @brief addString Wrapper around addString(std::string, bool) to add a
* vector of strings.
* @param keyList The vector containing the keys.
*/
void addString(const std::vector<std::string> &keyList)
{
for (const std::string &key : keyList)
{
addString(key, false);
}
addFailureLinks();
}
/**
* @brief parseText Parses a text with the trie.
* @param text The text to be parsed.
* @return Returns a vector with all matches.
*/
std::vector<Result> parseText(const std::string &text) const
{
std::vector<Result> Results;
if (text.empty())
{
return Results;
}
Node *current = root;
for (size_t i = 0; i < text.size(); i++)
{
current = findChild(current, CaseSensitive ? text.at(i)
: std::tolower(text.at(i)));
if (current->id != -1)
{
Results.emplace_back(keywords.at(current->id), i);
}
/* Process the output links for possible additional matches */
Node *temp = current->output;
while (temp != root)
{
Results.emplace_back(keywords.at(temp->id), i);
temp = temp->output;
}
}
return Results;
}
private:
/**
* @brief addChild Add a child Node to the trie.
* @param parrent The pointer to the parrent Node of the new one.
* @param character The character on the edge to the new Node.
* @return The pointer to the newly created Node.
*/
Node *addChild(Node *current, const char &character)
{
for (Node *child : current->children)
{
if (child->c == character)
{
return child;
}
}
trieNodes.emplace_back(std::make_unique<Node>(current->depth + 1,
character,
current,
root));
current->children.emplace_back(trieNodes.back().get());
return trieNodes.back().get();
}
/**
* @brief addFailureLinks Utilize a breadth first search to generate the
* failure links.
*/
void addFailureLinks()
{
std::queue<Node *> q;
q.push(root);
while (!q.empty())
{
Node *temp = q.front();
for (Node *child : temp->children)
{
q.push(child);
}
/* A failure link with just one less charater is the optimum and will
* never change.
*/
if (temp->failure->depth < temp->depth - 1)
{
for (Node *failchild : temp->parent->failure->children)
{
if (failchild->c == temp->c)
{
temp->failure = failchild;
}
}
}
/* Process the failure links for possible additional matches */
Node *out = temp->failure;
while (out != root)
{
if (out->id != -1)
{
break;
}
out = out->failure;
}
temp->output = out;
q.pop();
}
}
/**
* @brief findChild Searches for a child Node with given character or adds one.
* @param current The pointer to the current Node.
* @param character The character that is searched.
* @param addWord Flag sign to decide whether a new Node should be added.
* @return The pointer to the matching Node (possibly after failure links),
* root or the newly created one.
*/
Node *findChild(Node *current, const char &character) const
{
for (Node *child : current->children)
{
if (child->c == character)
{
return child;
}
}
return traverseFail(current, character);
}
/**
* @brief traverseFail Traverse the failure links during a search.
* @param current The original Node.
* @param character The character that is beeing searched.
* @return The pointer to the matching Node after a failure link or root->
*/
Node *traverseFail(Node *current, const char &character) const
{
Node *temp = current->failure;
while (temp != root)
{
for (Node *failchild : temp->children)
{
if (failchild->c == character)
{
return failchild;
}
}
temp = temp->failure;
}
for (Node *rootchild : root->children)
{
if (rootchild->c == character)
{
return rootchild;
}
}
return root;
}
}; // class keyword_trie
} // namespace miscco
#endif // MISCCO_KEYWORDTRIE_HPP