tesseract  5.0.0-alpha-619-ge9db
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 <tesseract/helpers.h>
27 #include <tesseract/serialis.h>
28 #include "trie.h"
29 #include "unicharset.h"
30 
31 int main(int argc, char** argv) {
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  const char* wordlist_filename = argv[++argv_index];
58  const char* dawg_filename = argv[++argv_index];
59  const char* unicharset_file = argv[++argv_index];
60  tprintf("Loading unicharset from '%s'\n", unicharset_file);
61  if (!classify->getDict().getUnicharset().load_from_file(unicharset_file)) {
62  tprintf("Failed to load unicharset from '%s'\n", unicharset_file);
63  delete classify;
64  return 1;
65  }
66  const UNICHARSET &unicharset = classify->getDict().getUnicharset();
67  if (argc == 4 || argc == 6) {
68  tesseract::Trie trie(
69  // the first 3 arguments are not used in this case
71  unicharset.size(), classify->getDict().dawg_debug_level);
72  tprintf("Reading word list from '%s'\n", wordlist_filename);
73  if (!trie.read_and_add_word_list(wordlist_filename, unicharset,
74  reverse_policy)) {
75  tprintf("Failed to add word list from '%s'\n", wordlist_filename);
76  exit(1);
77  }
78  tprintf("Reducing Trie to SquishedDawg\n");
79  tesseract::SquishedDawg *dawg = trie.trie_to_dawg();
80  if (dawg != nullptr && dawg->NumEdges() > 0) {
81  tprintf("Writing squished DAWG to '%s'\n", dawg_filename);
82  dawg->write_squished_dawg(dawg_filename);
83  } else {
84  tprintf("Dawg is empty, skip producing the output file\n");
85  }
86  delete dawg;
87  } else if (argc == 5) {
88  tprintf("Loading dawg DAWG from '%s'\n", dawg_filename);
90  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  delete classify;
101  return 0;
102 }
UNICHARSET::load_from_file
bool load_from_file(const char *const filename, bool skip_fragments)
Definition: unicharset.h:378
main
int main(int argc, char **argv)
Definition: wordlist2dawg.cpp:31
tesseract::SquishedDawg
Definition: dawg.h:405
commontraining.h
dict.h
SYSTEM_DAWG_PERM
Definition: ratngs.h:239
tesseract::Trie
Definition: trie.h:54
tesseract::SquishedDawg::NumEdges
int NumEdges()
Definition: dawg.h:437
tesseract::Classify
Definition: classify.h:103
tesseract::DAWG_TYPE_WORD
Definition: dawg.h:68
tesseract::Classify::getDict
virtual Dict & getDict()
Definition: classify.h:107
unicharset.h
dawg.h
tesseract::Trie::RRP_DO_NO_REVERSE
Definition: trie.h:57
UNICHARSET
Definition: unicharset.h:145
tesseract::Dict::dawg_debug_level
int dawg_debug_level
Definition: dict.h:622
tesseract::TessBaseAPI::Version
static const char * Version()
Definition: baseapi.cpp:233
helpers.h
tesseract::Trie::RTLReversePolicy
RTLReversePolicy
Definition: trie.h:56
tesseract::Trie::get_reverse_policy_name
static const char * get_reverse_policy_name(RTLReversePolicy reverse_policy)
Definition: trie.cpp:66
tprintf
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:34
tesseract::Dict::getUnicharset
const UNICHARSET & getUnicharset() const
Definition: dict.h:101
serialis.h
tesseract::SquishedDawg::write_squished_dawg
bool write_squished_dawg(TFile *file)
Writes the squished/reduced Dawg to a file.
Definition: dawg.cpp:382
classify.h
UNICHARSET::size
int size() const
Definition: unicharset.h:341
trie.h