Tesseract 3.02

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 // 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 "tesscallback.h"
23 #include "trie.h"
24 #include "unicharset.h"
25
26 const int kDictDebugLevel = 1;
27
28 tesseract::Dawg *LoadSquishedDawg(const UNICHARSET &unicharset,
29 const char *filename) {
30 const int kDictDebugLevel = 1;
31 FILE *dawg_file = fopen(filename, "rb");
32 if (dawg_file == NULL) {
33 tprintf("Could not open %s for reading.\n", filename);
34 return NULL;
35 }
36 tprintf("Loading word list from %s\n", filename);
37 tesseract::Dawg *retval = new tesseract::SquishedDawg(
38 dawg_file, tesseract::DAWG_TYPE_WORD, "eng", SYSTEM_DAWG_PERM,
39 kDictDebugLevel);
40 tprintf("Word list loaded.\n");
41 fclose(dawg_file);
42 return retval;
43 }
44
45 class WordOutputter {
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 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 == NULL) {
59 tprintf("Could not open %s for writing.\n", outfile_name);
60 return 1;
61 }
62 WordOutputter outputter(out);
63 TessCallback1<const char *> *print_word_cb =
64 NewPermanentTessCallback(&outputter, &WordOutputter::output_word);
65 dawg->iterate_words(unicharset, print_word_cb);
66 delete print_word_cb;
67 return fclose(out);
68 }
69
70 int main(int argc, char *argv[]) {
71 if (argc != 4) {
72 tprintf("Print all the words in a given dawg.\n");
73 tprintf("Usage: %s <unicharset> <dawgfile> <wordlistfile>\n",
74 argv[0]);
75 return 1;
76 }
77 const char *unicharset_file = argv[1];
78 const char *dawg_file = argv[2];
79 const char *wordlist_file = argv[3];
80 UNICHARSET unicharset;
81 if (!unicharset.load_from_file(unicharset_file)) {
82 tprintf("Error loading unicharset from %s.\n", unicharset_file);
83 return 1;
84 }
85 tesseract::Dawg *dict = LoadSquishedDawg(unicharset, dawg_file);
86 if (dict == NULL) {
87 tprintf("Error loading dictionary from %s.\n", dawg_file);
88 return 1;
89 }
90 int retval = WriteDawgAsWordlist(unicharset, dict, wordlist_file);
91 delete dict;
92 return retval;
93 }