tesseract  4.0.0-1-g2a2b
wordlist2dawg.cpp File Reference
#include "classify.h"
#include "commontraining.h"
#include "dawg.h"
#include "dict.h"
#include "emalloc.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 0;
39  } else if (!(argc == 4 || (argc == 5 && strcmp(argv[1], "-t") == 0) ||
40  (argc == 6 && strcmp(argv[1], "-r") == 0))) {
41  printf("Usage: %s -v | --version |\n"
42  " %s [-t | -r [reverse policy] ] word_list_file"
43  " dawg_file unicharset_file\n", argv[0], argv[0]);
44  return 1;
45  }
46  tesseract::Classify *classify = new tesseract::Classify();
47  int argv_index = 0;
48  if (argc == 5) ++argv_index;
49  tesseract::Trie::RTLReversePolicy reverse_policy =
51  if (argc == 6) {
52  ++argv_index;
53  int tmp_int;
54  sscanf(argv[++argv_index], "%d", &tmp_int);
55  reverse_policy = static_cast<tesseract::Trie::RTLReversePolicy>(tmp_int);
56  tprintf("Set reverse_policy to %s\n",
58  }
59  if (argc == 7) argv_index += 3;
60  const char* wordlist_filename = argv[++argv_index];
61  const char* dawg_filename = argv[++argv_index];
62  const char* unicharset_file = argv[++argv_index];
63  tprintf("Loading unicharset from '%s'\n", unicharset_file);
64  if (!classify->getDict().getUnicharset().load_from_file(unicharset_file)) {
65  tprintf("Failed to load unicharset from '%s'\n", unicharset_file);
66  delete classify;
67  return 1;
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  unicharset.size(), 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,
77  reverse_policy)) {
78  tprintf("Failed to add word list from '%s'\n", wordlist_filename);
79  exit(1);
80  }
81  tprintf("Reducing Trie to SquishedDawg\n");
82  tesseract::SquishedDawg *dawg = trie.trie_to_dawg();
83  if (dawg != nullptr && dawg->NumEdges() > 0) {
84  tprintf("Writing squished DAWG to '%s'\n", dawg_filename);
85  dawg->write_squished_dawg(dawg_filename);
86  } else {
87  tprintf("Dawg is empty, skip producing the output file\n");
88  }
89  delete dawg;
90  } else if (argc == 5) {
91  tprintf("Loading dawg DAWG from '%s'\n", dawg_filename);
93  dawg_filename,
94  // these 3 arguments are not used in this case
96  classify->getDict().dawg_debug_level);
97  tprintf("Checking word list from '%s'\n", wordlist_filename);
98  words.check_for_words(wordlist_filename, unicharset, true);
99  } else { // should never get here
100  tprintf("Invalid command-line options\n");
101  exit(1);
102  }
103  delete classify;
104  return 0;
105 }
int size() const
Definition: unicharset.h:336
static const char * get_reverse_policy_name(RTLReversePolicy reverse_policy)
Definition: trie.cpp:57
bool write_squished_dawg(TFile *file)
Writes the squished/reduced Dawg to a file.
Definition: dawg.cpp:374
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
int dawg_debug_level
Definition: dict.h:605
RTLReversePolicy
Definition: trie.h:63
const UNICHARSET & getUnicharset() const
Definition: dict.h:98
static const char * Version()
Definition: baseapi.cpp:223
virtual Dict & getDict()
Definition: classify.h:107
bool load_from_file(const char *const filename, bool skip_fragments)
Definition: unicharset.h:383