liblevenshtein 4.0.0
A library for generating Finite State Transducers based on Levenshtein Automata.
Loading...
Searching...
No Matches
command_line.cpp
Go to the documentation of this file.
1#include <cstring>
2#include <filesystem>
3#include <iostream>
4#include <sstream>
5
7
8#include "command_line.h"
9
10namespace fs = std::filesystem;
11
12namespace ll = liblevenshtein;
13
15
16CommandLine::CommandLine(int argc, char **argv) : argc(argc), argv(argv) {}
17
18auto CommandLine::dictionary_path() const -> const fs::path & {
19 return _dictionary_path;
20}
21
22auto CommandLine::serialization_path() const -> const fs::path & {
24}
25
26auto CommandLine::algorithm() const -> const ll::Algorithm & {
27 return _algorithm;
28}
29
30auto CommandLine::max_distance() const -> std::size_t {
31 return _max_distance;
32}
33
34auto CommandLine::next_path(fs::path &path, int &i) -> bool {
35 if (i + 1 < argc) {
36 path = argv[i + 1];
37 ++ i;
38 return true;
39 }
40 std::cerr << "Not enough command-line args!" << std::endl;
41 _return_code = ReturnCode::RETURN_END_OF_OPTS;
42 return false;
43}
44
46 for (int i = 1; i < argc; ++i) {
47 const char *arg = argv[i];
48 if (!strcmp(arg, "-h") || !strcmp(arg, "--help")) {
49 _return_code = ReturnCode::RETURN_SUCCESS;
50 print_help();
51 return false;
52 } else if (!strcmp(arg, "--dictionary-path")) {
53 if (!next_path(_dictionary_path, i)) {
54 return false;
55 }
56 if (!fs::exists(_dictionary_path)) {
57 std::cerr << "--dictionary-path=<PATH> must exist!" << std::endl;
58 std::cerr << "--dictionary-path=" << _dictionary_path << std::endl;
60 return false;
61 }
62 } else if (!strcmp(arg, "--serialization-path")) {
63 if (!next_path(_serialization_path, i)) {
64 return false;
65 }
66 } else if (!strcmp(arg, "--standard")) {
67 _algorithm = ll::Algorithm::STANDARD;
68 } else if (!strcmp(arg, "--transposition")) {
69 _algorithm = ll::Algorithm::TRANSPOSITION;
70 } else if (!strcmp(arg, "--merge-and-split")) {
71 _algorithm = ll::Algorithm::MERGE_AND_SPLIT;
72 } else if (!strcmp(arg, "--max-distance")) {
73 if (i + 1 == argc) {
74 std::cerr << "Not enough command-line args!" << std::endl;
75 _return_code = ReturnCode::RETURN_END_OF_OPTS;
76 return false;
77 }
78 std::stringstream stream(argv[i + 1]);
79 stream >> _max_distance;
80 ++i;
81 } else {
82 std::cerr << "Invalid argument: " << arg << std::endl;
83 _return_code = ReturnCode::RETURN_INVALID_ARG;
84 print_help();
85 return false;
86 }
87 }
88
89 if (!fs::exists(_dictionary_path) && !fs::exists(_serialization_path)) {
90 std::cerr << "Either --dictionary-path=<PATH> or "
91 "--serialization-path=<PATH> must exist"
92 << std::endl;
93 std::cerr << "--dictionary-path=" << _dictionary_path << std::endl;
94 std::cerr << "--serialization-path=" << _serialization_path << std::endl;
96 return false;
97 }
98
99 print_config();
100 return true;
101}
102
104 return _return_code;
105}
106
108 std::cerr << "liblevenshtein-demo :: Interactively queries a dictionary using the" << std::endl;
109 std::cerr << "configuration specified at the comand-line. Either a valid --dictionary-path or" << std::endl;
110 std::cerr << "a --serialization-path must be provided." << std::endl;
111 std::cerr << "--------------------------------------------------------------------------------" << std::endl;
112 std::cerr << " -h|--help Prints this help text." << std::endl;
113 std::cerr << " --dictionary-path <PATH> Path to the dictionary text file (one term per" << std::endl;
114 std::cerr << " line)." << std::endl;
115 std::cerr << " --serialization-path <PATH> Path to the file containing the serialized" << std::endl;
116 std::cerr << " dictionary." << std::endl;
117 std::cerr << " --standard [Optional] Use the standard Levenshtein distance " << std::endl;
118 std::cerr << " (substitutions, insertions, and deletions)." << std::endl;
119 std::cerr << " --transposition [Optional] Use the standard Levenshtein distance" << std::endl;
120 std::cerr << " extended with transposition." << std::endl;
121 std::cerr << " --merge-and-split [Optional] Use the standard Levenshtein distance" << std::endl;
122 std::cerr << " extended with merge and split." << std::endl;
123 std::cerr << " --max-distance <VALUE> Maximum edit distance from the query term for " << std::endl;
124 std::cerr << " spelling candidates (default: 2)." << std::endl;
125 std::cerr << std::endl;
126 std::cerr << "Examples:" << std::endl;
127 std::cerr << std::endl;
128 std::cerr << " ./liblevenshtein-demo --dictionary-path ../programming-languages.txt \\" << std::endl;
129 std::cerr << " --serialization-path programming-languages.pb" << std::endl;
130}
131
133 std::cout << "Using configuration:" << std::endl;
134 std::cout << " dictionary-path: " << _dictionary_path << std::endl;
135 std::cout << " serialization-path: " << _serialization_path << std::endl;
136 std::cout << " algorithm: ";
137 switch (_algorithm) {
138 case ll::Algorithm::STANDARD:
139 std::cout << "standard";
140 break;
141 case ll::Algorithm::TRANSPOSITION:
142 std::cout << "transposition";
143 break;
144 case ll::Algorithm::MERGE_AND_SPLIT:
145 std::cout << "merge-and-split";
146 break;
147 }
148 std::cout << std::endl;
149 std::cout << " max-distance: " << _max_distance << std::endl;
150}
151
152} // namespace liblevenshtein::demo
auto max_distance() const -> std::size_t
auto return_code() const -> ReturnCode
auto next_path(fs::path &path, int &i) -> bool
CommandLine(int argc, char **argv)
auto dictionary_path() const -> const fs::path &
auto algorithm() const -> const ll::Algorithm &
auto serialization_path() const -> const fs::path &
Various utilities regarding Levenshtein transducers.
Definition namespaces.dox:9
Algorithm
Enumerates the available Levenshtein distance algorithms.
Definition algorithm.h:9
STL namespace.