liblevenshtein 4.0.0
A library for generating Finite State Transducers based on Levenshtein Automata.
Loading...
Searching...
No Matches
merge_and_split_distance.cpp
Go to the documentation of this file.
3
5
6auto MergeAndSplitDistance::between(std::string v, std::string w)
7 -> std::size_t {
8 const SymmetricPair key(v, w);
9
10 std::size_t distance;
11 if (get(key, distance)) {
12 return distance;
13 }
14
15 if (v.empty()) {
16 return set(key, w.length());
17 }
18
19 if (w.empty()) {
20 return set(key, v.length());
21 }
22
23 char a = v[0];
24 std::string x = v.substr(1);
25 char b = w[0];
26 std::string y = w.substr(1);
27
28 // Discard identical characters
29 while (a == b && !(x.empty() || y.empty())) {
30 a = x[0];
31 v = x;
32 x = v.substr(1);
33 b = y[0];
34 w = y;
35 y = w.substr(1);
36 }
37
38 // x.empty() || y.empty()
39 if (a == b) {
40 if (x.empty()) {
41 return set(key, y.length());
42 }
43 return set(key, x.length());
44 }
45
46 distance = between(x, w);
47 if (0 == distance) {
48 return set(key, 1);
49 }
50
51 std::size_t min_distance = distance;
52
53 distance = between(v, y);
54 if (0 == distance) {
55 return set(key, 1);
56 }
57
58 if (distance < min_distance) {
59 min_distance = distance;
60 }
61
62 distance = between(x, y);
63 if (0 == distance) {
64 return set(key, 1);
65 }
66
67 if (distance < min_distance) {
68 min_distance = distance;
69 }
70
71 if (w.length() > 1) {
72 distance = between(x, f(w, 1));
73
74 if (0 == distance) {
75 return set(key, 1);
76 }
77
78 if (distance < min_distance) {
79 min_distance = distance;
80 }
81 }
82
83 if (v.length() > 1) {
84 distance = between(f(v, 1), y);
85
86 if (0 == distance) {
87 return set(key, 1);
88 }
89
90 if (distance < min_distance) {
91 min_distance = distance;
92 }
93 }
94
95 return set(key, 1 + min_distance);
96}
97
98} // namespace liblevenshtein::distance
auto between(std::string v, std::string w) -> std::size_t override
Measures the edit distance between two terms.
Represents a pair of terms sorted, lexicographically, in ascending order.
void query(ll::Dawg *dawg, const std::string &query_term, std::size_t max_distance)
Definition main.cpp:25
Memoized, recursive distance metrics typically used to evaluate the correctness of Levenshtein automa...
auto distance(State *state, std::size_t query_length) -> std::size_t
Infers the Levenshtein distance from the given State and query length.