liblevenshtein 4.0.0
A library for generating Finite State Transducers based on Levenshtein Automata.
Loading...
Searching...
No Matches
liblevenshtein::distance::StandardDistance Class Reference

Computes the standard Levenshtein distance, which includes the elementary operations of insertion, deletion, and substitution. More...

#include <standard_distance.h>

Inheritance diagram for liblevenshtein::distance::StandardDistance:
Collaboration diagram for liblevenshtein::distance::StandardDistance:

Public Member Functions

auto between (std::string v, std::string w) -> std::size_t override
 Measures the edit distance between two terms.
 
auto operator() (const std::string &v, const std::string &w) -> std::size_t
 Measures the edit distance between two terms.
 

Protected Member Functions

auto get (const SymmetricPair &key, std::size_t &distance) -> bool
 Collects the memoized distance between the pair of terms represented by the SymmetricPair if the distance has been previously determined.
 
auto set (const SymmetricPair &key, const std::size_t &distance) -> std::size_t
 Memoizes the distance between the SymmetricPair of terms for future reference.
 

Static Protected Member Functions

static auto f (const std::string &u, std::size_t t) -> std::string
 Returns the suffix of \(u\) from position \(t\).
 

Private Attributes

std::unordered_map< SymmetricPair, std::size_t > memo
 Memoized distances between pairs of terms.
 
std::shared_mutex mutex
 Coordinates memoization among threads.
 

Detailed Description

Computes the standard Levenshtein distance, which includes the elementary operations of insertion, deletion, and substitution.

Definition at line 14 of file standard_distance.h.

Member Function Documentation

◆ between()

auto liblevenshtein::distance::StandardDistance::between ( std::string v,
std::string w ) -> std::size_t
overridevirtual

Measures the edit distance between two terms.

Parameters
vFirst term to compare.
wSecond term to compare.
Returns
Edit distance between v and w.

Implements liblevenshtein::distance::Distance.

Definition at line 6 of file standard_distance.cpp.

6 {
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}
auto get(const SymmetricPair &key, std::size_t &distance) -> bool
Collects the memoized distance between the pair of terms represented by the SymmetricPair if the dist...
auto set(const SymmetricPair &key, const std::size_t &distance) -> std::size_t
Memoizes the distance between the SymmetricPair of terms for future reference.
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
auto distance(State *state, std::size_t query_length) -> std::size_t
Infers the Levenshtein distance from the given State and query length.

References liblevenshtein::distance(), and query().

Here is the call graph for this function:

◆ f()

auto liblevenshtein::distance::MemoizedDistance::f ( const std::string & u,
std::size_t t ) -> std::string
staticprotectedinherited

Returns the suffix of \(u\) from position \(t\).

If \(t \ge \left|u\right|\), then the empty string is returned.

Parameters
uTerm whose suffix is to be returned.
tBeginning position in u for the substring.
Returns
The substring of u beginning at position t if \(t < \left|u\right|\), or the empty string otherwise.

Definition at line 26 of file memoized_distance.cpp.

26 {
27 if (t < u.length()) {
28 return u.substr(1 + t);
29 }
30 return "";
31}

References query().

Here is the call graph for this function:

◆ get()

auto liblevenshtein::distance::MemoizedDistance::get ( const SymmetricPair & key,
std::size_t & distance ) -> bool
protectedinherited

Collects the memoized distance between the pair of terms represented by the SymmetricPair if the distance has been previously determined.

Otherwise, the distance is not collected and false is returned.

Parameters
keySymmetricPair of terms whose distance may have been memoized.
distanceOutgoing reference containing the memoized distance between the pair of terms, if the distance has been previously determined.
Returns
Whether the distance between the pair of terms has been previously determined.

Definition at line 9 of file memoized_distance.cpp.

9 {
10 std::shared_lock reader(mutex);
11 auto iter = memo.find(key);
12 if (iter != memo.end()) {
13 distance = iter->second;
14 return true;
15 }
16 return false;
17}
std::unordered_map< SymmetricPair, std::size_t > memo
Memoized distances between pairs of terms.
std::shared_mutex mutex
Coordinates memoization among threads.

References liblevenshtein::distance(), and query().

Here is the call graph for this function:

◆ operator()()

auto liblevenshtein::distance::Distance::operator() ( const std::string & v,
const std::string & w ) -> std::size_t
inherited

Measures the edit distance between two terms.

This is equivalent to calling between(v, w).

Parameters
vFirst term to compare.
wSecond term to compare.
Returns
Edit distance between v and w.

Definition at line 5 of file distance.cpp.

6 {
7 return between(v, w);
8}
virtual auto between(std::string v, std::string w) -> std::size_t=0
Measures the edit distance between two terms.

References query().

Here is the call graph for this function:

◆ set()

auto liblevenshtein::distance::MemoizedDistance::set ( const SymmetricPair & key,
const std::size_t & distance ) -> std::size_t
protectedinherited

Memoizes the distance between the SymmetricPair of terms for future reference.

Parameters
keySymmetricPair of terms whose distance is to be memoized.
distanceDistance between the SymmetricPair of terms to memoize.
Returns
The distance once it has been memoized.

Definition at line 19 of file memoized_distance.cpp.

20 {
21 std::unique_lock writer(mutex);
22 memo[key] = distance;
23 return distance;
24}

References liblevenshtein::distance(), and query().

Here is the call graph for this function:

Member Data Documentation

◆ memo

std::unordered_map<SymmetricPair, std::size_t> liblevenshtein::distance::MemoizedDistance::memo
privateinherited

Memoized distances between pairs of terms.

Definition at line 57 of file memoized_distance.h.

◆ mutex

std::shared_mutex liblevenshtein::distance::MemoizedDistance::mutex
mutableprivateinherited

Coordinates memoization among threads.

Definition at line 60 of file memoized_distance.h.


The documentation for this class was generated from the following files: