tesseract v5.3.3.20231005
dawg2wordlist.cpp
Go to the documentation of this file.
1
2// 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 "trie.h"
22#include "unicharset.h"
23
24#include "serialis.h"
25
26using namespace tesseract;
27
28static std::unique_ptr<tesseract::Dawg> LoadSquishedDawg(const UNICHARSET &unicharset, const char *filename) {
29 const int kDictDebugLevel = 1;
30 tesseract::TFile dawg_file;
31 if (!dawg_file.Open(filename, nullptr)) {
32 tprintf("Could not open %s for reading.\n", filename);
33 return nullptr;
34 }
35 tprintf("Loading word list from %s\n", filename);
36 auto retval = std::make_unique<tesseract::SquishedDawg>(tesseract::DAWG_TYPE_WORD, "eng",
37 SYSTEM_DAWG_PERM, kDictDebugLevel);
38 if (!retval->Load(&dawg_file)) {
39 tprintf("Could not read %s\n", filename);
40 return nullptr;
41 }
42 tprintf("Word list loaded.\n");
43 return retval;
44}
45
47public:
48 WordOutputter(FILE *file) : file_(file) {}
49 void output_word(const char *word) {
50 fprintf(file_, "%s\n", word);
51 }
52
53private:
54 FILE *file_;
55};
56
57// returns 0 if successful.
58static int WriteDawgAsWordlist(const UNICHARSET &unicharset, 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 EXIT_FAILURE;
64 }
65 WordOutputter outputter(out);
66 using namespace std::placeholders; // for _1
67 dawg->iterate_words(unicharset, std::bind(&WordOutputter::output_word, &outputter, _1));
68 return fclose(out);
69}
70
71int 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(
80 "Usage: %s -v | --version | %s <unicharset> <dawgfile> "
81 "<wordlistfile>\n",
82 argv[0], argv[0]);
83 return EXIT_FAILURE;
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 EXIT_FAILURE;
92 }
93 auto dict = LoadSquishedDawg(unicharset, dawg_file);
94 if (dict == nullptr) {
95 tprintf("Error loading dictionary from %s.\n", dawg_file);
96 return EXIT_FAILURE;
97 }
98 int retval = WriteDawgAsWordlist(unicharset, dict.get(), wordlist_file);
99 return retval;
100}
int main(int argc, char *argv[])
@ DAWG_TYPE_WORD
Definition: dawg.h:66
void tprintf(const char *format,...)
Definition: tprintf.cpp:41
@ SYSTEM_DAWG_PERM
Definition: ratngs.h:244
static const char * Version()
Definition: baseapi.cpp:241
bool Open(const char *filename, FileReader reader)
Definition: serialis.cpp:140
bool load_from_file(const char *const filename, bool skip_fragments)
Definition: unicharset.h:391
void iterate_words(const UNICHARSET &unicharset, std::function< void(const WERD_CHOICE *)> cb) const
Definition: dawg.cpp:106
void output_word(const char *word)
WordOutputter(FILE *file)