liblevenshtein 4.0.0
A library for generating Finite State Transducers based on Levenshtein Automata.
Loading...
Searching...
No Matches
lazy_query.h
Go to the documentation of this file.
1#ifndef LIBLEVENSHTEIN_TRANSDUCER_LAZY_QUERY_H
2#define LIBLEVENSHTEIN_TRANSDUCER_LAZY_QUERY_H
3
4#include <cstddef>
5#include <functional>
6#include <queue>
7#include <string>
8#include <utility>
9#include <vector>
10
14
15namespace liblevenshtein {
16
19using TransitionFn = std::function<State *(State *, std::vector<bool> &)>;
20
23using DistanceFn = std::function<std::size_t(State *, std::size_t)>;
24
30template <class Result> class LazyQuery {
31public:
32
48 LazyQuery(const std::string &term, std::size_t max_distance,
51
55 LazyQuery() = default;
56
60 ~LazyQuery();
61
67 auto operator++() -> LazyQuery &;
68
74 auto operator*() const -> const Result &;
75
83
85
90
94 std::queue<std::pair<char, DawgNode *>> _edges;
95
98
101
104
107
109 std::string _term;
110
113
117
121
123 std::size_t _a;
124
126 std::size_t _k;
127
129 std::size_t _i;
130
134 void advance();
135
143 void update_candidate(std::string &term, std::size_t distance);
144
156 auto build_intersection(char label, DawgNode *node, State *state,
158
171 auto characteristic_vector(char x, std::string &term, std::size_t k,
172 std::size_t i) -> std::vector<bool>;
173};
174
181public:
182
198 LazyIterator(const std::string &term, std::size_t max_distance,
201
208 auto begin() -> LazyQuery<Result>;
209
215 auto end() -> LazyQuery<Result>;
216
217private:
218
220 std::string _term;
221
223 std::size_t _max_distance;
224
228
232
236};
237
238} // namespace liblevenshtein
239
240
241#endif // LIBLEVENSHTEIN_TRANSDUCER_LAZY_QUERY_H
Represents a position within one or more terms of a DAWG dictionary.
Definition dawg_node.h:20
Represents an Intersection between a dictionary automaton and Levenshtein automaton,...
Lazily traverses the intersection between the dictionary automaton and Levenshtein automaton,...
Definition lazy_query.h:180
TransitionFn transition
Maps one Levenshtein State to another given the state and a characteristic vector.
Definition lazy_query.h:231
std::size_t _max_distance
Maximum edit distance to consider when matching spelling candidates.
Definition lazy_query.h:223
Intersection * _intersection
Initial Intersection between the dictionary root node and initial Levenshtein State.
Definition lazy_query.h:227
std::string _term
Query term whose spelling candidates are to be matched.
Definition lazy_query.h:220
DistanceFn min_distance
Infers the Levenshtein distance between the query term and a spelling candidate.
Definition lazy_query.h:235
Lazily traverses the intersection between the dictionary automaton and Levenshtein automaton,...
Definition lazy_query.h:30
Result _candidate
Presently matched spelling candidate.
Definition lazy_query.h:106
void advance()
Advances to the next spelling candidate.
std::size_t _a
Intermediate value used to compare edit distances without overflowing.
Definition lazy_query.h:123
TransitionFn transition
Maps one Levenshtein State to another given the State and a characteristic vector.
Definition lazy_query.h:116
std::size_t _k
Intermediate value used to compare edit distances without overflowing.
Definition lazy_query.h:126
std::queue< Intersection * > _pending
Intermediate Intersection instances representing paths along the intersection between the dictionary ...
Definition lazy_query.h:89
Intersection * _intersection
Current whose respective spelling candidates are being considered.
Definition lazy_query.h:103
auto characteristic_vector(char x, std::string &term, std::size_t k, std::size_t i) -> std::vector< bool >
Flags all instances of the given character in the query term within a window of consecutive character...
std::size_t _max_distance
Maximum edit distance to consider when matching spelling candidates.
Definition lazy_query.h:112
std::string _term
Query term whose spelling candidates are to be matched.
Definition lazy_query.h:109
auto operator++() -> LazyQuery &
Advances to the next spelling candidate.
LazyQuery()=default
Constructs a LazyQuery that represents the end of the spelling candidates.
auto build_intersection(char label, DawgNode *node, State *state, Intersection *parent) -> Intersection *
Factory function that constructs and records a new Intersection instance.
std::queue< std::pair< char, DawgNode * > > _edges
Outgoing dictionary node edges that are pending traversal along the intersection between the dictiona...
Definition lazy_query.h:94
std::vector< Intersection * > _intersections
Tracks Intersection allocations so they may be freed.
Definition lazy_query.h:97
DistanceFn min_distance
Infers the Levenshtein distance between the query term and a spelling candidate.
Definition lazy_query.h:120
auto operator*() const -> const Result &
Returns the current spelling candidate.
bool _is_complete
Whether all the spelling candidates have been matched.
Definition lazy_query.h:100
void update_candidate(std::string &term, std::size_t distance)
Sets the current spelling candidate according to the desired Result type.
~LazyQuery()
Frees any owned members.
std::size_t _i
Intermediate value used to compare edit distances without overflowing.
Definition lazy_query.h:129
Consists of a closure of Position nodes within the Levenshtein automaton.
Definition state.h:23
void query(ll::Dawg *dawg, const std::string &query_term, std::size_t max_distance)
Definition main.cpp:25
Various utilities regarding Levenshtein transducers.
Definition namespaces.dox:9
std::function< std::size_t(State *, std::size_t)> DistanceFn
Infers the Levenshtein distance from a final Levenshtein State and the length of the query term.
Definition lazy_query.h:23
std::function< State *(State *, std::vector< bool > &)> TransitionFn
Returns the Levenshtein State mapped-to by another State and characteristic vector.
Definition lazy_query.h:19
STL namespace.