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