tesseract  4.00.00dev
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 "serialis.h"
23 #include "tesscallback.h"
24 #include "trie.h"
25 #include "unicharset.h"
26 
27 const int kDictDebugLevel = 1;
28 
30  const char *filename) {
31  const int kDictDebugLevel = 1;
32  tesseract::TFile dawg_file;
33  if (!dawg_file.Open(filename, nullptr)) {
34  tprintf("Could not open %s for reading.\n", filename);
35  return nullptr;
36  }
37  tprintf("Loading word list from %s\n", filename);
39  tesseract::DAWG_TYPE_WORD, "eng", SYSTEM_DAWG_PERM, kDictDebugLevel);
40  if (!retval->Load(&dawg_file)) {
41  tprintf("Could not read %s\n", filename);
42  delete retval;
43  return nullptr;
44  }
45  tprintf("Word list loaded.\n");
46  return retval;
47 }
48 
50  public:
51  WordOutputter(FILE *file) : file_(file) {}
52  void output_word(const char *word) { fprintf(file_, "%s\n", word); }
53  private:
54  FILE *file_;
55 };
56 
57 // returns 0 if successful.
58 int WriteDawgAsWordlist(const UNICHARSET &unicharset,
59  const tesseract::Dawg *dawg,
60  const char *outfile_name) {
61  FILE *out = fopen(outfile_name, "wb");
62  if (out == nullptr) {
63  tprintf("Could not open %s for writing.\n", outfile_name);
64  return 1;
65  }
66  WordOutputter outputter(out);
67  TessCallback1<const char *> *print_word_cb =
69  dawg->iterate_words(unicharset, print_word_cb);
70  delete print_word_cb;
71  return fclose(out);
72 }
73 
74 int main(int argc, char *argv[]) {
75  if (argc != 4) {
76  tprintf("Print all the words in a given dawg.\n");
77  tprintf("Usage: %s <unicharset> <dawgfile> <wordlistfile>\n",
78  argv[0]);
79  return 1;
80  }
81  const char *unicharset_file = argv[1];
82  const char *dawg_file = argv[2];
83  const char *wordlist_file = argv[3];
84  UNICHARSET unicharset;
85  if (!unicharset.load_from_file(unicharset_file)) {
86  tprintf("Error loading unicharset from %s.\n", unicharset_file);
87  return 1;
88  }
89  tesseract::Dawg *dict = LoadSquishedDawg(unicharset, dawg_file);
90  if (dict == nullptr) {
91  tprintf("Error loading dictionary from %s.\n", dawg_file);
92  return 1;
93  }
94  int retval = WriteDawgAsWordlist(unicharset, dict, wordlist_file);
95  delete dict;
96  return retval;
97 }
_ConstTessMemberResultCallback_0_0< false, R, T1 >::base * NewPermanentTessCallback(const T1 *obj, R(T2::*member)() const)
Definition: tesscallback.h:116
void output_word(const char *word)
tesseract::Dawg * LoadSquishedDawg(const UNICHARSET &unicharset, const char *filename)
int WriteDawgAsWordlist(const UNICHARSET &unicharset, const tesseract::Dawg *dawg, const char *outfile_name)
void iterate_words(const UNICHARSET &unicharset, TessCallback1< const WERD_CHOICE *> *cb) const
Definition: dawg.cpp:105
const int kDictDebugLevel
#define tprintf(...)
Definition: tprintf.h:31
int main(int argc, char *argv[])
bool Load(TFile *fp)
Definition: dawg.h:438
WordOutputter(FILE *file)
bool load_from_file(const char *const filename, bool skip_fragments)
Definition: unicharset.h:348
const char * filename
Definition: ioapi.h:38
bool Open(const STRING &filename, FileReader reader)
Definition: serialis.cpp:38