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
315
316
317
318
319
320
321
322
323
324
325
326
327
|
#include "base/levenshtein_dfa.hpp"
#include "base/assert.hpp"
#include "base/stl_helpers.hpp"
#include <algorithm>
#include <queue>
#include <set>
#include <sstream>
#include <vector>
namespace strings
{
namespace
{
inline size_t AbsDiff(size_t a, size_t b) { return a > b ? a - b : b - a; }
class TransitionTable
{
public:
TransitionTable(UniString const & s) : m_s(s), m_size(s.size()) {}
void Move(LevenshteinDFA::State const & s, size_t prefixCharsToKeep, UniChar c,
LevenshteinDFA::State & t)
{
t.Clear();
for (auto const & p : s.m_positions)
GetMoves(p, prefixCharsToKeep, c, t);
t.Normalize();
}
private:
void GetMoves(LevenshteinDFA::Position const & p, size_t prefixCharsToKeep, UniChar c,
LevenshteinDFA::State & t)
{
auto & ps = t.m_positions;
if (p.IsTransposed())
{
if (p.m_offset + 2 <= m_size && m_s[p.m_offset] == c)
ps.emplace_back(p.m_offset + 2, p.m_errorsLeft, false /* transposed */);
return;
}
ASSERT(p.IsStandard(), ());
if (p.m_offset < m_size && m_s[p.m_offset] == c)
{
ps.emplace_back(p.m_offset + 1, p.m_errorsLeft, false /* transposed */);
return;
}
if (p.m_errorsLeft == 0)
return;
if (p.m_offset < prefixCharsToKeep)
return;
ps.emplace_back(p.m_offset, p.m_errorsLeft - 1, false /* transposed */);
if (p.m_offset == m_size)
return;
ps.emplace_back(p.m_offset + 1, p.m_errorsLeft - 1, false /* transposed */);
size_t i;
if (FindRelevant(p, c, i))
{
ASSERT_GREATER(i, 0, (i));
ASSERT_LESS_OR_EQUAL(p.m_offset + i + 1, m_size, ());
ps.emplace_back(p.m_offset + i + 1, p.m_errorsLeft - i, false /* transposed */);
if (i == 1)
ps.emplace_back(p.m_offset, p.m_errorsLeft - 1, true /* transposed */);
}
}
bool FindRelevant(LevenshteinDFA::Position const & p, UniChar c, size_t & i) const
{
size_t const limit =
std::min(m_size - p.m_offset, static_cast<size_t>(p.m_errorsLeft) + 1);
for (i = 0; i < limit; ++i)
{
if (m_s[p.m_offset + i] == c)
return true;
}
return false;
}
UniString const & m_s;
size_t const m_size;
};
} // namespace
// LevenshteinDFA ----------------------------------------------------------------------------------
// static
size_t const LevenshteinDFA::kStartingState = 0;
size_t const LevenshteinDFA::kRejectingState = 1;
// LevenshteinDFA::Position ------------------------------------------------------------------------
LevenshteinDFA::Position::Position(size_t offset, uint8_t errorsLeft, bool transposed)
: m_offset(offset), m_errorsLeft(errorsLeft), m_transposed(transposed)
{
}
bool LevenshteinDFA::Position::SubsumedBy(Position const & rhs) const
{
if (m_errorsLeft >= rhs.m_errorsLeft)
return false;
if (IsStandard() && rhs.IsStandard())
return AbsDiff(m_offset, rhs.m_offset) <= rhs.m_errorsLeft - m_errorsLeft;
if (IsStandard() && rhs.IsTransposed())
return m_offset == rhs.m_offset && m_errorsLeft == 0;
if (IsTransposed() && rhs.IsStandard())
return AbsDiff(m_offset + 1, rhs.m_offset) <= rhs.m_errorsLeft - m_errorsLeft;
ASSERT(IsTransposed(), ());
ASSERT(rhs.IsTransposed(), ());
return m_offset == rhs.m_offset;
}
bool LevenshteinDFA::Position::operator<(Position const & rhs) const
{
if (m_offset != rhs.m_offset)
return m_offset < rhs.m_offset;
if (m_errorsLeft != rhs.m_errorsLeft)
return m_errorsLeft < rhs.m_errorsLeft;
return m_transposed < rhs.m_transposed;
}
bool LevenshteinDFA::Position::operator==(Position const & rhs) const
{
return m_offset == rhs.m_offset && m_errorsLeft == rhs.m_errorsLeft &&
m_transposed == rhs.m_transposed;
}
// LevenshteinDFA::State ---------------------------------------------------------------------------
void LevenshteinDFA::State::Normalize()
{
size_t j = m_positions.size();
for (size_t i = 0; i < j; ++i)
{
auto const & cur = m_positions[i];
auto it = find_if(m_positions.begin(), m_positions.begin() + j,
[&](Position const & rhs) { return cur.SubsumedBy(rhs); });
if (it != m_positions.begin() + j)
{
ASSERT_GREATER(j, 0, ());
--j;
std::swap(m_positions[i], m_positions[j]);
}
}
m_positions.erase(m_positions.begin() + j, m_positions.end());
my::SortUnique(m_positions);
}
// LevenshteinDFA ----------------------------------------------------------------------------------
// static
LevenshteinDFA::LevenshteinDFA(UniString const & s, size_t prefixCharsToKeep, uint8_t maxErrors)
: m_size(s.size()), m_maxErrors(maxErrors)
{
m_alphabet.assign(s.begin(), s.end());
my::SortUnique(m_alphabet);
UniChar missed = 0;
for (size_t i = 0; i < m_alphabet.size() && missed >= m_alphabet[i]; ++i)
{
if (missed == m_alphabet[i])
++missed;
}
m_alphabet.push_back(missed);
std::queue<State> states;
std::map<State, size_t> visited;
auto pushState = [&states, &visited, this](State const & state, size_t id)
{
ASSERT_EQUAL(id, m_transitions.size(), ());
ASSERT_EQUAL(visited.count(state), 0, (state, id));
states.emplace(state);
visited[state] = id;
m_transitions.emplace_back(m_alphabet.size());
m_accepting.push_back(false);
};
pushState(MakeStart(), kStartingState);
pushState(MakeRejecting(), kRejectingState);
TransitionTable table(s);
while (!states.empty())
{
auto const curr = states.front();
states.pop();
ASSERT(IsValid(curr), (curr));
ASSERT_GREATER(visited.count(curr), 0, (curr));
auto const id = visited[curr];
ASSERT_LESS(id, m_transitions.size(), ());
if (IsAccepting(curr))
m_accepting[id] = true;
for (size_t i = 0; i < m_alphabet.size(); ++i)
{
State next;
table.Move(curr, prefixCharsToKeep, m_alphabet[i], next);
size_t nid;
auto const it = visited.find(next);
if (it == visited.end())
{
nid = visited.size();
pushState(next, nid);
}
else
{
nid = it->second;
}
m_transitions[id][i] = nid;
}
}
}
LevenshteinDFA::LevenshteinDFA(std::string const & s, size_t prefixCharsToKeep, uint8_t maxErrors)
: LevenshteinDFA(MakeUniString(s), prefixCharsToKeep, maxErrors)
{
}
LevenshteinDFA::LevenshteinDFA(UniString const & s, uint8_t maxErrors)
: LevenshteinDFA(s, 0 /* prefixCharsToKeep */, maxErrors)
{
}
LevenshteinDFA::LevenshteinDFA(std::string const & s, uint8_t maxErrors)
: LevenshteinDFA(s, 0 /* prefixCharsToKeep */, maxErrors)
{
}
LevenshteinDFA::State LevenshteinDFA::MakeStart()
{
State state;
state.m_positions.emplace_back(0 /* offset */, m_maxErrors /* errorsLeft */,
false /* transposed */);
return state;
}
LevenshteinDFA::State LevenshteinDFA::MakeRejecting()
{
return State();
}
bool LevenshteinDFA::IsValid(Position const & p) const
{
return p.m_offset <= m_size && p.m_errorsLeft <= m_maxErrors;
}
bool LevenshteinDFA::IsValid(State const & s) const
{
for (auto const & p : s.m_positions)
{
if (!IsValid(p))
return false;
}
return true;
}
bool LevenshteinDFA::IsAccepting(Position const & p) const
{
return p.IsStandard() && m_size - p.m_offset <= p.m_errorsLeft;
}
bool LevenshteinDFA::IsAccepting(State const & s) const
{
for (auto const & p : s.m_positions)
{
if (IsAccepting(p))
return true;
}
return false;
}
size_t LevenshteinDFA::Move(size_t s, UniChar c) const
{
ASSERT_GREATER(m_alphabet.size(), 0, ());
ASSERT(is_sorted(m_alphabet.begin(), m_alphabet.end() - 1), ());
size_t i;
auto const it = lower_bound(m_alphabet.begin(), m_alphabet.end() - 1, c);
if (it == m_alphabet.end() - 1 || *it != c)
i = m_alphabet.size() - 1;
else
i = distance(m_alphabet.begin(), it);
return m_transitions[s][i];
}
std::string DebugPrint(LevenshteinDFA::Position const & p)
{
std::ostringstream os;
os << "Position [" << p.m_offset << ", " << static_cast<uint32_t>(p.m_errorsLeft) << ", "
<< p.m_transposed << "]";
return os.str();
}
std::string DebugPrint(LevenshteinDFA::State const & s)
{
std::ostringstream os;
os << "State [";
for (size_t i = 0; i < s.m_positions.size(); ++i)
{
os << DebugPrint(s.m_positions[i]);
if (i + 1 != s.m_positions.size())
os << ", ";
}
return os.str();
}
} // namespace strings
|