liblevenshtein 4.0.0
A library for generating Finite State Transducers based on Levenshtein Automata.
Loading...
Searching...
No Matches
main.cpp File Reference
#include <algorithm>
#include <cstddef>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>
#include <google/protobuf/stubs/common.h>
#include <liblevenshtein/collection/dawg.h>
#include <liblevenshtein/collection/sorted_dawg.h>
#include <liblevenshtein/serialization/serializer.h>
#include <liblevenshtein/transducer/algorithm.h>
#include <liblevenshtein/transducer/transducer.h>
#include "command_line.h"
Include dependency graph for main.cpp:

Go to the source code of this file.

Functions

template<ll::Algorithm Type>
void query (ll::Dawg *dawg, const std::string &query_term, std::size_t max_distance)
 
std::string & prompt (std::string &query_term)
 
auto main (int argc, char *argv[]) -> int
 

Function Documentation

◆ main()

auto main ( int argc,
char * argv[] ) -> int

Definition at line 58 of file main.cpp.

58 {
59 // Verify that the version of the library that we linked against is
60 // compatible with the version of the headers we compiled against.
62
63 demo::CommandLine cli(argc, argv);
64 if (!cli.parse_opts()) {
65 return static_cast<int>(cli.return_code());
66 }
67
68 ll::Dawg *dawg = ll::deserialize_protobuf(cli.serialization_path());
69
70 if (dawg == nullptr) {
71 std::vector<std::string> terms;
72
73 // populate with your spelling candidates
74 std::ifstream dictionary_file(cli.dictionary_path());
75 if (dictionary_file.is_open()) {
76 std::string term;
77 while (std::getline(dictionary_file, term)) {
78 terms.push_back(term);
79 }
80 dictionary_file.close();
81 }
82
83 std::sort(terms.begin(), terms.end()); // must be sorted for now
84
85 // NOTE: If (dawg == nullptr) then the construction of the dictionary
86 // failed, probably because terms wasn't sorted lexicographically in
87 // ascending order.
88 dawg = ll::sorted_dawg(terms.begin(), terms.end());
89 }
90
91 std::string query_term;
92
93 while (prompt(query_term) != "") {
94 switch (cli.algorithm()) {
95 case ll::Algorithm::STANDARD:
97 break;
98 case ll::Algorithm::TRANSPOSITION:
100 break;
101 case ll::Algorithm::MERGE_AND_SPLIT:
103 cli.max_distance());
104 break;
105 }
106 }
107
108 std::cout << std::endl;
109 std::cout << "Exiting ..." << std::endl;
110
111 // save the dictionary for reuse
112 serialize_protobuf(dawg, cli.serialization_path());
113
114 delete dawg;
115
116 // Optional: Delete all global objects allocated by libprotobuf.
117 google::protobuf::ShutdownProtobufLibrary();
118
119 return 0;
120}
A Directed Acyclic Word Graph (DAWG) maps sequences of characters to form words; the collection of wo...
Definition dawg.h:23
std::string & prompt(std::string &query_term)
Definition main.cpp:50
void query(ll::Dawg *dawg, const std::string &query_term, std::size_t max_distance)
Definition main.cpp:25
auto sorted_dawg(IterType iter, IterType end) -> SortedDawg *
Factory function that initializes a new SortedDawg using the terms yielded from an iterator.
auto deserialize_protobuf(const fs::path &path) -> Dawg *
Deserializes the protobuf containing a Dawg at the given path or returns nullptr if none exists.
auto serialize_protobuf(Dawg *dawg, const fs::path &path) -> bool
Serializes the given Dawg to protobuf at the given path.

References liblevenshtein::demo::CommandLine::algorithm(), liblevenshtein::deserialize_protobuf(), liblevenshtein::demo::CommandLine::dictionary_path(), liblevenshtein::demo::CommandLine::parse_opts(), prompt(), query(), liblevenshtein::demo::CommandLine::return_code(), liblevenshtein::demo::CommandLine::serialization_path(), and liblevenshtein::sorted_dawg().

Here is the call graph for this function:

◆ prompt()

std::string & prompt ( std::string & query_term)

Definition at line 50 of file main.cpp.

50 {
51 std::cout << std::endl;
52 std::cout << "Enter a query term (or nothing to exit)." << std::endl;
53 std::cout << "query> ";
54 std::getline(std::cin, query_term);
55 return query_term;
56}

Referenced by main().

Here is the caller graph for this function:

◆ query()

template<ll::Algorithm Type>
void query ( ll::Dawg * dawg,
const std::string & query_term,
std::size_t max_distance )

NOTE: If you had initialized the transducer as

ll::Transducer<Type, std::string> transduce(dawg->root());

, you would iterate over the results as follows:

for (const std::string &term : transduce(query_term, max_distance)) { // do something with term, which is guaranteed to require no more // than max_distance operations to transform it into the query_term. }

Definition at line 25 of file main.cpp.

25 {
27
28 // NOTE: transduce(query_term, max_distance) returns an iterator over
29 // ll:Candidate instances. ll:Candidate is an alias for
30 // std::pair<std::string, std::size_t>.
31 for (const auto &[term, distance] : transduce(query_term, max_distance)) {
32 std::cout << "d(\"" << query_term << "\", \"" << term << "\") = "
33 << distance << std::endl;
34 }
35
48}
Constructs a Levenshtein Transducer that, when given a dictionary automaton, query term,...
Definition transducer.h:26
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::Dawg::root().

Referenced by liblevenshtein::SortedDawg::add(), liblevenshtein::State::add(), liblevenshtein::DawgNode::add_edge(), liblevenshtein::DawgIterator::advance(), liblevenshtein::LazyQuery< Result >::advance(), liblevenshtein::Dawg::all_nodes(), liblevenshtein::LazyIterator< Result >::begin(), liblevenshtein::distance::MergeAndSplitDistance::between(), liblevenshtein::distance::StandardDistance::between(), liblevenshtein::distance::TranspositionDistance::between(), liblevenshtein::LazyQuery< Result >::build_intersection(), liblevenshtein::LazyQuery< Result >::characteristic_vector(), liblevenshtein::collect_edges(), liblevenshtein::collect_nodes(), liblevenshtein::compare< Algorithm::MERGE_AND_SPLIT >(), liblevenshtein::compare< Algorithm::STANDARD >(), liblevenshtein::compare< Algorithm::TRANSPOSITION >(), liblevenshtein::Dawg::contains(), liblevenshtein::SortedDawg::Dawg(), liblevenshtein::DawgIterator::DawgIterator(), liblevenshtein::deserialize_protobuf(), liblevenshtein::deserialize_protobuf(), liblevenshtein::deserialize_protobuf(), liblevenshtein::deserialize_protobuf(), liblevenshtein::distance< Algorithm::MERGE_AND_SPLIT >(), liblevenshtein::distance< Algorithm::STANDARD >(), liblevenshtein::distance< Algorithm::TRANSPOSITION >(), liblevenshtein::LazyIterator< Result >::end(), liblevenshtein::distance::MemoizedDistance::f(), liblevenshtein::State::find_middle(), liblevenshtein::DawgNode::for_each_edge(), liblevenshtein::from_protobuf(), liblevenshtein::distance::MemoizedDistance::get(), liblevenshtein::index_of(), liblevenshtein::StateIterator::insert(), liblevenshtein::State::insert_after(), liblevenshtein::insert_after(), liblevenshtein::LazyQuery< Result >::LazyQuery(), main(), liblevenshtein::State::merge(), liblevenshtein::merge< Algorithm::MERGE_AND_SPLIT >(), liblevenshtein::merge< Algorithm::STANDARD >(), liblevenshtein::merge< Algorithm::TRANSPOSITION >(), liblevenshtein::State::merge_sort(), liblevenshtein::SortedDawg::minimize(), liblevenshtein::SortedDawg::minimized_node(), std::hash< liblevenshtein::Dawg >::operator()(), std::hash< liblevenshtein::DawgNode >::operator()(), liblevenshtein::Transducer< Type, Result >::operator()(), liblevenshtein::distance::Distance::operator()(), liblevenshtein::StateTransition::operator()(), liblevenshtein::UnsubsumeFn::operator()(), liblevenshtein::operator<<(), liblevenshtein::Dawg::operator==(), liblevenshtein::DawgIterator::operator==(), liblevenshtein::DawgNode::operator==(), liblevenshtein::StateIterator::operator==(), liblevenshtein::distance::SymmetricPair::operator==(), liblevenshtein::Transition::operator==(), liblevenshtein::position_transition< Algorithm::MERGE_AND_SPLIT >(), liblevenshtein::position_transition< Algorithm::STANDARD >(), liblevenshtein::position_transition< Algorithm::TRANSPOSITION >(), liblevenshtein::State::remove(), liblevenshtein::serialize_protobuf(), liblevenshtein::serialize_protobuf(), liblevenshtein::serialize_protobuf(), liblevenshtein::serialize_protobuf(), liblevenshtein::distance::MemoizedDistance::set(), liblevenshtein::sorted_dawg(), liblevenshtein::State::State(), liblevenshtein::State::State(), liblevenshtein::Prefix::str(), liblevenshtein::Intersection::str(), liblevenshtein::subsumes< Algorithm::MERGE_AND_SPLIT >(), liblevenshtein::subsumes< Algorithm::STANDARD >(), liblevenshtein::subsumes< Algorithm::TRANSPOSITION >(), liblevenshtein::distance::SymmetricPair::SymmetricPair(), liblevenshtein::to_protobuf(), liblevenshtein::Transducer< Type, Result >::Transducer(), liblevenshtein::DawgNode::transition(), liblevenshtein::LazyQuery< Result >::update_candidate(), liblevenshtein::DawgIterator::~DawgIterator(), and liblevenshtein::LazyQuery< Result >::~LazyQuery().

Here is the call graph for this function:
Here is the caller graph for this function: