cprover
Toggle main menu visibility
Loading...
Searching...
No Matches
edit_distance.cpp
Go to the documentation of this file.
1
5
6
#include "
edit_distance.h
"
7
8
levenshtein_automatont::levenshtein_automatont
(
9
const
std::string &
string
,
10
std::size_t allowed_errors)
11
{
12
const
std::size_t layer_offset =
string
.size() + 1;
13
for
(std::size_t i = 0; i <= allowed_errors; ++i)
14
{
15
final_states
.push_back(
string
.size() + layer_offset * i);
16
}
17
for
(std::size_t string_index = 0; string_index <
string
.size();
18
++string_index)
19
{
20
for
(std::size_t error_layer = 0; error_layer <= allowed_errors;
21
++error_layer)
22
{
23
// position string_index matches
24
nfa
.add_transition(
25
error_layer * layer_offset + string_index,
26
string
[string_index],
27
error_layer * layer_offset + string_index + 1);
28
if
(error_layer < allowed_errors)
29
{
30
// insertion, swap or deletion
31
nfa
.add_arbitrary_transition(
32
error_layer * layer_offset + string_index,
33
(error_layer + 1) * layer_offset + string_index);
34
nfa
.add_epsilon_transition(
35
error_layer * layer_offset + string_index,
36
(error_layer + 1) * layer_offset + string_index + 1);
37
nfa
.add_arbitrary_transition(
38
error_layer * layer_offset + string_index,
39
(error_layer + 1) * layer_offset + string_index + 1);
40
}
41
}
42
}
43
for
(std::size_t error_layer = 0; error_layer < allowed_errors; ++error_layer)
44
{
45
// arbitrary transitions between error layers
46
nfa
.add_arbitrary_transition(
47
error_layer * layer_offset +
string
.size(),
48
(error_layer + 1) * layer_offset +
string
.size());
49
}
50
}
51
52
bool
levenshtein_automatont::matches
(
const
std::string &
string
)
const
53
{
54
return
get_edit_distance
(
string
).has_value();
55
}
56
57
std::optional<std::size_t>
58
levenshtein_automatont::get_edit_distance
(
const
std::string &
string
)
const
59
{
60
auto
current =
nfa
.initial_state(0);
61
for
(
const
auto
c :
string
)
62
{
63
current =
nfa
.next_state(current, c);
64
}
65
for
(std::size_t distance = 0; distance <
final_states
.size(); ++distance)
66
{
67
if
(current.contains(
final_states
[distance]))
68
{
69
return
distance;
70
}
71
}
72
return
{};
73
}
edit_distance.h
levenshtein_automatont::levenshtein_automatont
levenshtein_automatont(const std::string &string, std::size_t allowed_errors=2)
Definition
edit_distance.cpp:8
levenshtein_automatont::get_edit_distance
std::optional< std::size_t > get_edit_distance(const std::string &string) const
Definition
edit_distance.cpp:58
levenshtein_automatont::matches
bool matches(const std::string &string) const
Definition
edit_distance.cpp:52
levenshtein_automatont::final_states
std::vector< state_labelt > final_states
Definition
edit_distance.h:29
levenshtein_automatont::nfa
nfat< char > nfa
Definition
edit_distance.h:27
util
edit_distance.cpp
Generated by
1.17.0