liblevenshtein 4.0.0
A library for generating Finite State Transducers based on Levenshtein Automata.
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1#include <algorithm>
2#include <cstddef>
3#include <filesystem>
4#include <fstream>
5#include <iostream>
6#include <map>
7#include <string>
8#include <utility>
9#include <vector>
10
11#include <google/protobuf/stubs/common.h>
12
18
19#include "command_line.h"
20
21namespace ll = liblevenshtein;
22namespace demo = liblevenshtein::demo;
23
24template <ll::Algorithm Type>
25void query(ll::Dawg *dawg, const std::string &query_term, std::size_t max_distance) {
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}
49
50std::string &prompt(std::string &query_term) {
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}
57
58auto main(int argc, char *argv[]) -> int {
59 // Verify that the version of the library that we linked against is
60 // compatible with the version of the headers we compiled against.
61 GOOGLE_PROTOBUF_VERIFY_VERSION;
62
63 demo::CommandLine cli(argc, argv);
64 if (!cli.parse_opts()) {
65 return static_cast<int>(cli.return_code());
66 }
67
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
auto root() const -> DawgNode *
Returns a pointer to the root node of this dictionary from which all its terms may be determined.
Definition dawg.cpp:51
Constructs a Levenshtein Transducer that, when given a dictionary automaton, query term,...
Definition transducer.h:26
auto return_code() const -> ReturnCode
auto dictionary_path() const -> const fs::path &
auto algorithm() const -> const ll::Algorithm &
auto serialization_path() const -> const fs::path &
auto main(int argc, char *argv[]) -> int
Definition main.cpp:58
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
Various utilities regarding Levenshtein transducers.
Definition namespaces.dox:9
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.