tesseract  4.1.0
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

int main ( int  argc,
char **  argv 
)

Definition at line 31 of file wordlist2dawg.cpp.

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