liblevenshtein 4.0.0
A library for generating Finite State Transducers based on Levenshtein Automata.
Loading...
Searching...
No Matches
standard_distance.cpp
Go to the documentation of this file.
3
5
6auto StandardDistance::between(std::string v, std::string w) -> std::size_t {
7 const SymmetricPair key(v, w);
8
9 std::size_t distance;
10 if (get(key, distance)) {
11 return distance;
12 }
13
14 if (v.empty()) {
15 return set(key, w.length());
16 }
17
18 if (w.empty()) {
19 return set(key, v.length());
20 }
21
22 char a = v[0]; std::string s = v.substr(1);
23 char b = w[0]; std::string t = w.substr(1);
24
25 // Discard identical characters
26 while (a == b && !(s.empty() || t.empty())) {
27 a = s[0]; v = s; s = v.substr(1);
28 b = t[0]; w = t; t = w.substr(1);
29 }
30
31 // s.length() = 0 || t.length() == 0
32 if (a == b) {
33 if (s.empty()) {
34 return set(key, t.length());
35 }
36 return set(key, s.length());
37 }
38
39 distance = between(s, w);
40 if (0 == distance) {
41 return set(key, 1);
42 }
43
44 std::size_t min_distance = distance;
45
46 distance = between(v, t);
47 if (0 == distance) {
48 return set(key, 1);
49 }
50
51 if (distance < min_distance) {
52 min_distance = distance;
53 }
54
55 distance = between(s, t);
56 if (0 == distance) {
57 return set(key, 1);
58 }
59
60 if (distance < min_distance) {
61 min_distance = distance;
62 }
63
64 return set(key, 1 + min_distance);
65}
66
67} // 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.