liblevenshtein 4.0.0
A library for generating Finite State Transducers based on Levenshtein Automata.
Loading...
Searching...
No Matches
transposition_distance.cpp
Go to the documentation of this file.
3
5
6auto TranspositionDistance::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]; std::string x = v.substr(1);
24 char b = w[0]; std::string y = w.substr(1);
25
26 // Discard identical characters
27 while (a == b && !(x.empty() || y.empty())) {
28 a = x[0]; v = x; x = v.substr(1);
29 b = y[0]; w = y; y = w.substr(1);
30 }
31
32 // x.empty() || y.empty()
33 if (a == b) {
34 if (x.empty()) {
35 return set(key, y.length());
36 }
37 return set(key, x.length());
38 }
39
40 distance = between(x, w);
41 if (0 == distance) {
42 return set(key, 1);
43 }
44
45 std::size_t min_distance = distance;
46
47 distance = between(v, y);
48 if (0 == distance) {
49 return set(key, 1);
50 }
51
52 if (distance < min_distance) {
53 min_distance = distance;
54 }
55
56 distance = between(x, y);
57 if (0 == distance) {
58 return set(key, 1);
59 }
60
61 if (distance < min_distance) {
62 min_distance = distance;
63 }
64
65 if (!(x.empty() || y.empty())) {
66 char const a1 = x[0];
67 char const b1 = y[0];
68 if (a == b1 && a1 == b) {
69 distance = between(f(v, 1), f(w, 1));
70
71 if (0 == distance) {
72 return set(key, 1);
73 }
74
75 if (distance < min_distance) {
76 min_distance = distance;
77 }
78 }
79 }
80
81 return set(key, 1 + min_distance);
82}
83
84} // namespace liblevenshtein::distance
Represents a pair of terms sorted, lexicographically, in ascending order.
auto between(std::string v, std::string w) -> std::size_t override
Measures the edit distance between two terms.
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.