tesseract  4.00.00dev
wordlist2dawg.cpp File Reference
#include <stdio.h>
#include "classify.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 
)

This program reads in a text file consisting of feature samples from a training page in the following format:

   FontName UTF8-char-str xmin ymin xmax ymax page-number
    NumberOfFeatureTypes(N)
      FeatureTypeName1 NumberOfFeatures(M)
         Feature1
         ...
         FeatureM
      FeatureTypeName2 NumberOfFeatures(M)
         Feature1
         ...
         FeatureM
      ...
      FeatureTypeNameN NumberOfFeatures(M)
         Feature1
         ...
         FeatureM
   FontName CharName ...

The result of this program is a binary inttemp file used by the OCR engine.

Parameters
argcnumber of command line arguments
argvarray of command line arguments
Returns
none
Note
Exceptions: none
History: Fri Aug 18 08:56:17 1989, DSJ, Created.
History: Mon May 18 1998, Christy Russson, Revistion started.

Definition at line 34 of file wordlist2dawg.cpp.

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