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