tesseract  5.0.0
wordlist2dawg.cpp
Go to the documentation of this file.
1 // File: wordlist2dawg.cpp
3 // Description: Program to generate a DAWG from a word list file
4 // Author: Thomas Kielbus
5 //
6 // (C) Copyright 2006, Google Inc.
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
18 
19 // Given a file that contains a list of words (one word per line) this program
20 // generates the corresponding squished DAWG file.
21 
22 #include "classify.h"
23 #include "commontraining.h" // CheckSharedLibraryVersion
24 #include "dawg.h"
25 #include "dict.h"
26 #include "helpers.h"
27 #include "serialis.h"
28 #include "trie.h"
29 #include "unicharset.h"
30 
31 using namespace tesseract;
32 
33 int main(int argc, char **argv) {
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(
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 1;
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 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  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  exit(1);
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  exit(1);
99  }
100  return 0;
101 }
int main(int argc, char **argv)
@ DAWG_TYPE_WORD
Definition: dawg.h:66
void tprintf(const char *format,...)
Definition: tprintf.cpp:41
@ SYSTEM_DAWG_PERM
Definition: ratngs.h:240
static const char * Version()
Definition: baseapi.cpp:238
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
int check_for_words(const char *filename, const UNICHARSET &unicharset, bool enable_wildcard) const
Definition: dawg.cpp:68
const UNICHARSET & getUnicharset() const
Definition: dict.h:104
bool read_and_add_word_list(const char *filename, const UNICHARSET &unicharset, Trie::RTLReversePolicy reverse)
Definition: trie.cpp:273
SquishedDawg * trie_to_dawg()
Definition: trie.cpp:508
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