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

#include <command_line.h>

Collaboration diagram for liblevenshtein::demo::CommandLine:

Public Member Functions

 CommandLine (int argc, char **argv)
 
 ~CommandLine ()=default
 
auto dictionary_path () const -> const fs::path &
 
auto serialization_path () const -> const fs::path &
 
auto algorithm () const -> const ll::Algorithm &
 
auto max_distance () const -> std::size_t
 
auto parse_opts () -> bool
 
auto return_code () const -> ReturnCode
 
void print_help () const
 
void print_config () const
 

Private Member Functions

auto next_path (fs::path &path, int &i) -> bool
 

Private Attributes

ReturnCode _return_code = ReturnCode::RETURN_DEFAULT
 
fs::path _dictionary_path = "../resources/programming-languages.txt"
 
fs::path _serialization_path = "programming-languages.pb"
 
ll::Algorithm _algorithm = ll::Algorithm::TRANSPOSITION
 
std::size_t _max_distance = 2
 
int argc
 
char ** argv
 

Detailed Description

Definition at line 24 of file command_line.h.

Constructor & Destructor Documentation

◆ CommandLine()

liblevenshtein::demo::CommandLine::CommandLine ( int argc,
char ** argv )

◆ ~CommandLine()

liblevenshtein::demo::CommandLine::~CommandLine ( )
default

Member Function Documentation

◆ algorithm()

auto liblevenshtein::demo::CommandLine::algorithm ( ) const -> const ll::Algorithm &

Definition at line 26 of file command_line.cpp.

26 {
27 return _algorithm;
28}

References _algorithm.

Referenced by main().

Here is the caller graph for this function:

◆ dictionary_path()

auto liblevenshtein::demo::CommandLine::dictionary_path ( ) const -> const fs::path &

Definition at line 18 of file command_line.cpp.

18 {
19 return _dictionary_path;
20}

References _dictionary_path.

Referenced by main().

Here is the caller graph for this function:

◆ max_distance()

auto liblevenshtein::demo::CommandLine::max_distance ( ) const -> std::size_t

Definition at line 30 of file command_line.cpp.

30 {
31 return _max_distance;
32}

References _max_distance.

◆ next_path()

auto liblevenshtein::demo::CommandLine::next_path ( fs::path & path,
int & i ) -> bool
private

Definition at line 34 of file command_line.cpp.

34 {
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;
42 return false;
43}
void query(ll::Dawg *dawg, const std::string &query_term, std::size_t max_distance)
Definition main.cpp:25

References liblevenshtein::demo::RETURN_END_OF_OPTS.

◆ parse_opts()

auto liblevenshtein::demo::CommandLine::parse_opts ( ) -> bool

Definition at line 45 of file command_line.cpp.

45 {
46 for (int i = 1; i < argc; ++i) {
47 const char *arg = argv[i];
48 if (!strcmp(arg, "-h") || !strcmp(arg, "--help")) {
50 print_help();
51 return false;
52 } else if (!strcmp(arg, "--dictionary-path")) {
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")) {
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;
76 return false;
77 }
78 std::stringstream stream(argv[i + 1]);
80 ++i;
81 } else {
82 std::cerr << "Invalid argument: " << arg << std::endl;
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
100 return true;
101}
auto next_path(fs::path &path, int &i) -> bool

References liblevenshtein::demo::RETURN_END_OF_OPTS, liblevenshtein::demo::RETURN_FILE_NOT_EXISTS, liblevenshtein::demo::RETURN_INVALID_ARG, and liblevenshtein::demo::RETURN_SUCCESS.

Referenced by main().

Here is the caller graph for this function:

◆ print_config()

void liblevenshtein::demo::CommandLine::print_config ( ) const

Definition at line 132 of file command_line.cpp.

132 {
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}

References _algorithm, _dictionary_path, _max_distance, and _serialization_path.

◆ print_help()

void liblevenshtein::demo::CommandLine::print_help ( ) const

Definition at line 107 of file command_line.cpp.

107 {
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}

◆ return_code()

auto liblevenshtein::demo::CommandLine::return_code ( ) const -> ReturnCode

Definition at line 103 of file command_line.cpp.

103 {
104 return _return_code;
105}

References _return_code.

Referenced by main().

Here is the caller graph for this function:

◆ serialization_path()

auto liblevenshtein::demo::CommandLine::serialization_path ( ) const -> const fs::path &

Definition at line 22 of file command_line.cpp.

22 {
24}

References _serialization_path.

Referenced by main().

Here is the caller graph for this function:

Member Data Documentation

◆ _algorithm

ll::Algorithm liblevenshtein::demo::CommandLine::_algorithm = ll::Algorithm::TRANSPOSITION
private

Definition at line 47 of file command_line.h.

Referenced by algorithm(), and print_config().

◆ _dictionary_path

fs::path liblevenshtein::demo::CommandLine::_dictionary_path = "../resources/programming-languages.txt"
private

Definition at line 45 of file command_line.h.

Referenced by dictionary_path(), and print_config().

◆ _max_distance

std::size_t liblevenshtein::demo::CommandLine::_max_distance = 2
private

Definition at line 48 of file command_line.h.

Referenced by max_distance(), and print_config().

◆ _return_code

ReturnCode liblevenshtein::demo::CommandLine::_return_code = ReturnCode::RETURN_DEFAULT
private

Definition at line 44 of file command_line.h.

Referenced by return_code().

◆ _serialization_path

fs::path liblevenshtein::demo::CommandLine::_serialization_path = "programming-languages.pb"
private

Definition at line 46 of file command_line.h.

Referenced by print_config(), and serialization_path().

◆ argc

int liblevenshtein::demo::CommandLine::argc
private

Definition at line 50 of file command_line.h.

◆ argv

char** liblevenshtein::demo::CommandLine::argv
private

Definition at line 51 of file command_line.h.


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