All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 // Created: Thu May 10 18:11:42 PDT 2007
6 //
7 // (C) Copyright 2006, Google Inc.
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 //
19 
20 // Given a file that contains a list of words (one word per line) this program
21 // generates the corresponding squished DAWG file.
22 
23 #include <stdio.h>
24 
25 #include "classify.h"
26 #include "dawg.h"
27 #include "dict.h"
28 #include "emalloc.h"
29 #include "freelist.h"
30 #include "helpers.h"
31 #include "serialis.h"
32 #include "trie.h"
33 #include "unicharset.h"
34 
35 int main(int argc, char** argv) {
36  if (!(argc == 4 || (argc == 5 && strcmp(argv[1], "-t") == 0) ||
37  (argc == 6 && strcmp(argv[1], "-r") == 0))) {
38  printf("Usage: %s [-t | -r [reverse policy] ] word_list_file"
39  " dawg_file unicharset_file\n", argv[0]);
40  return 1;
41  }
42  tesseract::Classify *classify = new tesseract::Classify();
43  int argv_index = 0;
44  if (argc == 5) ++argv_index;
45  tesseract::Trie::RTLReversePolicy reverse_policy =
47  if (argc == 6) {
48  ++argv_index;
49  int tmp_int;
50  sscanf(argv[++argv_index], "%d", &tmp_int);
51  reverse_policy = static_cast<tesseract::Trie::RTLReversePolicy>(tmp_int);
52  tprintf("Set reverse_policy to %s\n",
54  }
55  if (argc == 7) argv_index += 3;
56  const char* wordlist_filename = argv[++argv_index];
57  const char* dawg_filename = argv[++argv_index];
58  const char* unicharset_file = argv[++argv_index];
59  tprintf("Loading unicharset from '%s'\n", unicharset_file);
60  if (!classify->getDict().getUnicharset().load_from_file(unicharset_file)) {
61  tprintf("Failed to load unicharset from '%s'\n", unicharset_file);
62  delete classify;
63  return 1;
64  }
65  const UNICHARSET &unicharset = classify->getDict().getUnicharset();
66  if (argc == 4 || argc == 6) {
67  tesseract::Trie trie(
68  // the first 3 arguments are not used in this case
70  unicharset.size(), classify->getDict().dawg_debug_level);
71  tprintf("Reading word list from '%s'\n", wordlist_filename);
72  if (!trie.read_and_add_word_list(wordlist_filename, unicharset,
73  reverse_policy)) {
74  tprintf("Failed to add word list from '%s'\n", wordlist_filename);
75  exit(1);
76  }
77  tprintf("Reducing Trie to SquishedDawg\n");
78  tesseract::SquishedDawg *dawg = trie.trie_to_dawg();
79  if (dawg != NULL && dawg->NumEdges() > 0) {
80  tprintf("Writing squished DAWG to '%s'\n", dawg_filename);
81  dawg->write_squished_dawg(dawg_filename);
82  } else {
83  tprintf("Dawg is empty, skip producing the output file\n");
84  }
85  delete dawg;
86  } else if (argc == 5) {
87  tprintf("Loading dawg DAWG from '%s'\n", dawg_filename);
89  dawg_filename,
90  // these 3 arguments are not used in this case
92  classify->getDict().dawg_debug_level);
93  tprintf("Checking word list from '%s'\n", wordlist_filename);
94  words.check_for_words(wordlist_filename, unicharset, true);
95  } else { // should never get here
96  tprintf("Invalid command-line options\n");
97  exit(1);
98  }
99  delete classify;
100  return 0;
101 }
#define tprintf(...)
Definition: tprintf.h:31
bool load_from_file(const char *const filename, bool skip_fragments)
Definition: unicharset.h:346
void write_squished_dawg(FILE *file)
Writes the squished/reduced Dawg to a file.
Definition: dawg.cpp:388
static const char * get_reverse_policy_name(RTLReversePolicy reverse_policy)
Definition: trie.cpp:61
Dict & getDict()
Definition: classify.h:65
int main(int argc, char **argv)
int dawg_debug_level
Definition: dict.h:595
const UNICHARSET & getUnicharset() const
Definition: dict.h:96
RTLReversePolicy
Definition: trie.h:64
#define NULL
Definition: host.h:144
int size() const
Definition: unicharset.h:297