tesseract v5.3.3.20231005
wordlist2dawg.cpp File Reference
#include "classify.h"
#include "commontraining.h"
#include "dawg.h"
#include "dict.h"
#include "helpers.h"
#include "serialis.h"
#include "trie.h"
#include "unicharset.h"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 33 of file wordlist2dawg.cpp.

33 {
34 tesseract::CheckSharedLibraryVersion();
35
36 if (argc > 1 && (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version"))) {
37 printf("%s\n", tesseract::TessBaseAPI::Version());
38 return EXIT_SUCCESS;
39 } else if (!(argc == 4 || (argc == 5 && strcmp(argv[1], "-t") == 0) ||
40 (argc == 6 && strcmp(argv[1], "-r") == 0))) {
41 printf(
42 "Usage: %s -v | --version |\n"
43 " %s [-t | -r [reverse policy] ] word_list_file"
44 " dawg_file unicharset_file\n",
45 argv[0], argv[0]);
46 return EXIT_FAILURE;
47 }
48 tesseract::Classify classify;
49 int argv_index = 0;
50 if (argc == 5) {
51 ++argv_index;
52 }
54 if (argc == 6) {
55 ++argv_index;
56 int tmp_int;
57 sscanf(argv[++argv_index], "%d", &tmp_int);
58 reverse_policy = static_cast<tesseract::Trie::RTLReversePolicy>(tmp_int);
59 tprintf("Set reverse_policy to %s\n", tesseract::Trie::get_reverse_policy_name(reverse_policy));
60 }
61 const char *wordlist_filename = argv[++argv_index];
62 const char *dawg_filename = argv[++argv_index];
63 const char *unicharset_file = argv[++argv_index];
64 tprintf("Loading unicharset from '%s'\n", unicharset_file);
65 if (!classify.getDict().getUnicharset().load_from_file(unicharset_file)) {
66 tprintf("Failed to load unicharset from '%s'\n", unicharset_file);
67 return EXIT_FAILURE;
68 }
69 const UNICHARSET &unicharset = classify.getDict().getUnicharset();
70 if (argc == 4 || argc == 6) {
71 tesseract::Trie trie(
72 // the first 3 arguments are not used in this case
74 classify.getDict().dawg_debug_level);
75 tprintf("Reading word list from '%s'\n", wordlist_filename);
76 if (!trie.read_and_add_word_list(wordlist_filename, unicharset, reverse_policy)) {
77 tprintf("Failed to add word list from '%s'\n", wordlist_filename);
78 return EXIT_FAILURE;
79 }
80 tprintf("Reducing Trie to SquishedDawg\n");
81 std::unique_ptr<tesseract::SquishedDawg> dawg(trie.trie_to_dawg());
82 if (dawg && dawg->NumEdges() > 0) {
83 tprintf("Writing squished DAWG to '%s'\n", dawg_filename);
84 dawg->write_squished_dawg(dawg_filename);
85 } else {
86 tprintf("Dawg is empty, skip producing the output file\n");
87 }
88 } else if (argc == 5) {
89 tprintf("Loading dawg DAWG from '%s'\n", dawg_filename);
90 tesseract::SquishedDawg words(dawg_filename,
91 // these 3 arguments are not used in this case
93 classify.getDict().dawg_debug_level);
94 tprintf("Checking word list from '%s'\n", wordlist_filename);
95 words.check_for_words(wordlist_filename, unicharset, true);
96 } else { // should never get here
97 tprintf("Invalid command-line options\n");
98 return EXIT_FAILURE;
99 }
100 return EXIT_SUCCESS;
101}
@ DAWG_TYPE_WORD
Definition: dawg.h:66
void tprintf(const char *format,...)
Definition: tprintf.cpp:41
@ SYSTEM_DAWG_PERM
Definition: ratngs.h:244
static const char * Version()
Definition: baseapi.cpp:241
bool load_from_file(const char *const filename, bool skip_fragments)
Definition: unicharset.h:391
size_t size() const
Definition: unicharset.h:355
virtual Dict & getDict()
Definition: classify.h:98
const UNICHARSET & getUnicharset() const
Definition: dict.h:104
RTLReversePolicy
Definition: trie.h:55
@ RRP_DO_NO_REVERSE
Definition: trie.h:56
static const char * get_reverse_policy_name(RTLReversePolicy reverse_policy)
Definition: trie.cpp:45