tesseract  5.0.0-alpha-619-ge9db
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 <tesseract/serialis.h>
22 #include "trie.h"
23 #include "unicharset.h"
24 
25 static tesseract::Dawg *LoadSquishedDawg(const UNICHARSET &unicharset,
26  const char *filename) {
27  const int kDictDebugLevel = 1;
28  tesseract::TFile dawg_file;
29  if (!dawg_file.Open(filename, nullptr)) {
30  tprintf("Could not open %s for reading.\n", filename);
31  return nullptr;
32  }
33  tprintf("Loading word list from %s\n", filename);
35  tesseract::DAWG_TYPE_WORD, "eng", SYSTEM_DAWG_PERM, kDictDebugLevel);
36  if (!retval->Load(&dawg_file)) {
37  tprintf("Could not read %s\n", filename);
38  delete retval;
39  return nullptr;
40  }
41  tprintf("Word list loaded.\n");
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 static 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 == nullptr) {
59  tprintf("Could not open %s for writing.\n", outfile_name);
60  return 1;
61  }
62  WordOutputter outputter(out);
63  using namespace std::placeholders; // for _1
64  dawg->iterate_words(unicharset,
65  std::bind(&WordOutputter::output_word, &outputter, _1));
66  return fclose(out);
67 }
68 
69 int main(int argc, char *argv[]) {
70  tesseract::CheckSharedLibraryVersion();
71 
72  if (argc > 1 && (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version"))) {
73  printf("%s\n", tesseract::TessBaseAPI::Version());
74  return 0;
75  } else if (argc != 4) {
76  tprintf("Print all the words in a given dawg.\n");
77  tprintf("Usage: %s -v | --version | %s <unicharset> <dawgfile> <wordlistfile>\n",
78  argv[0], 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 }
UNICHARSET::load_from_file
bool load_from_file(const char *const filename, bool skip_fragments)
Definition: unicharset.h:378
tesseract::SquishedDawg::Load
bool Load(TFile *fp)
Definition: dawg.h:431
tesseract::SquishedDawg
Definition: dawg.h:405
commontraining.h
SYSTEM_DAWG_PERM
Definition: ratngs.h:239
tesseract::DAWG_TYPE_WORD
Definition: dawg.h:68
tesseract::TFile::Open
bool Open(const STRING &filename, FileReader reader)
Definition: serialis.cpp:210
WordOutputter
Definition: dawg2wordlist.cpp:45
unicharset.h
dawg.h
file
Definition: include_gunit.h:22
main
int main(int argc, char *argv[])
Definition: dawg2wordlist.cpp:69
tesseract::TFile
Definition: serialis.h:75
UNICHARSET
Definition: unicharset.h:145
tesseract::TessBaseAPI::Version
static const char * Version()
Definition: baseapi.cpp:233
tesseract::Dawg::iterate_words
void iterate_words(const UNICHARSET &unicharset, std::function< void(const WERD_CHOICE *)> cb) const
Definition: dawg.cpp:118
tesseract::Dawg
Definition: dawg.h:113
WordOutputter::WordOutputter
WordOutputter(FILE *file)
Definition: dawg2wordlist.cpp:47
WordOutputter::output_word
void output_word(const char *word)
Definition: dawg2wordlist.cpp:48
tprintf
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:34
serialis.h
trie.h