All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
dawg2wordlist.cpp
Go to the documentation of this file.
1 // File: dawg2wordlist.cpp
3 // Description: Program to create a word list from a DAWG and unicharset.
4 // Author: David Eger
5 // Created: Thu 22 Dec 2011
6 //
7 // (C) Copyright 2011, 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 #include "dawg.h"
21 #include "host.h"
22 #include "tesscallback.h"
23 #include "trie.h"
24 #include "unicharset.h"
25 
26 const int kDictDebugLevel = 1;
27 
29  const char *filename) {
30  const int kDictDebugLevel = 1;
31  FILE *dawg_file = fopen(filename, "rb");
32  if (dawg_file == NULL) {
33  tprintf("Could not open %s for reading.\n", filename);
34  return NULL;
35  }
36  tprintf("Loading word list from %s\n", filename);
39  kDictDebugLevel);
40  tprintf("Word list loaded.\n");
41  fclose(dawg_file);
42  return retval;
43 }
44 
46  public:
47  WordOutputter(FILE *file) : file_(file) {}
48  void output_word(const char *word) { fprintf(file_, "%s\n", word); }
49  private:
50  FILE *file_;
51 };
52 
53 // returns 0 if successful.
54 int WriteDawgAsWordlist(const UNICHARSET &unicharset,
55  const tesseract::Dawg *dawg,
56  const char *outfile_name) {
57  FILE *out = fopen(outfile_name, "wb");
58  if (out == NULL) {
59  tprintf("Could not open %s for writing.\n", outfile_name);
60  return 1;
61  }
62  WordOutputter outputter(out);
63  TessCallback1<const char *> *print_word_cb =
65  dawg->iterate_words(unicharset, print_word_cb);
66  delete print_word_cb;
67  return fclose(out);
68 }
69 
70 int main(int argc, char *argv[]) {
71  if (argc != 4) {
72  tprintf("Print all the words in a given dawg.\n");
73  tprintf("Usage: %s <unicharset> <dawgfile> <wordlistfile>\n",
74  argv[0]);
75  return 1;
76  }
77  const char *unicharset_file = argv[1];
78  const char *dawg_file = argv[2];
79  const char *wordlist_file = argv[3];
80  UNICHARSET unicharset;
81  if (!unicharset.load_from_file(unicharset_file)) {
82  tprintf("Error loading unicharset from %s.\n", unicharset_file);
83  return 1;
84  }
85  tesseract::Dawg *dict = LoadSquishedDawg(unicharset, dawg_file);
86  if (dict == NULL) {
87  tprintf("Error loading dictionary from %s.\n", dawg_file);
88  return 1;
89  }
90  int retval = WriteDawgAsWordlist(unicharset, dict, wordlist_file);
91  delete dict;
92  return retval;
93 }
int main(int argc, char *argv[])
int WriteDawgAsWordlist(const UNICHARSET &unicharset, const tesseract::Dawg *dawg, const char *outfile_name)
#define tprintf(...)
Definition: tprintf.h:31
tesseract::Dawg * LoadSquishedDawg(const UNICHARSET &unicharset, const char *filename)
void output_word(const char *word)
bool load_from_file(const char *const filename, bool skip_fragments)
Definition: unicharset.h:346
void iterate_words(const UNICHARSET &unicharset, TessCallback1< const WERD_CHOICE * > *cb) const
Definition: dawg.cpp:106
_ConstTessMemberResultCallback_0_0< false, R, T1 >::base * NewPermanentTessCallback(const T1 *obj, R(T2::*member)() const)
Definition: tesscallback.h:116
WordOutputter(FILE *file)
#define NULL
Definition: host.h:144
const int kDictDebugLevel