tesseract 4.1.1
Loading...
Searching...
No Matches
wordlist2dawg.cpp
Go to the documentation of this file.
1
2// File: wordlist2dawg.cpp
3// Description: Program to generate a DAWG from a word list file
4// Author: Thomas Kielbus
5//
6// (C) Copyright 2006, 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// Given a file that contains a list of words (one word per line) this program
20// generates the corresponding squished DAWG file.
21
22#include "classify.h"
23#include "commontraining.h" // CheckSharedLibraryVersion
24#include "dawg.h"
25#include "dict.h"
26#include "helpers.h"
27#include "serialis.h"
28#include "trie.h"
29#include "unicharset.h"
30
31int main(int argc, char** argv) {
32 tesseract::CheckSharedLibraryVersion();
33
34 if (argc > 1 && (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version"))) {
35 printf("%s\n", tesseract::TessBaseAPI::Version());
36 return 0;
37 } else if (!(argc == 4 || (argc == 5 && strcmp(argv[1], "-t") == 0) ||
38 (argc == 6 && strcmp(argv[1], "-r") == 0))) {
39 printf("Usage: %s -v | --version |\n"
40 " %s [-t | -r [reverse policy] ] word_list_file"
41 " dawg_file unicharset_file\n", argv[0], argv[0]);
42 return 1;
43 }
45 int argv_index = 0;
46 if (argc == 5) ++argv_index;
49 if (argc == 6) {
50 ++argv_index;
51 int tmp_int;
52 sscanf(argv[++argv_index], "%d", &tmp_int);
53 reverse_policy = static_cast<tesseract::Trie::RTLReversePolicy>(tmp_int);
54 tprintf("Set reverse_policy to %s\n",
56 }
57 const char* wordlist_filename = argv[++argv_index];
58 const char* dawg_filename = argv[++argv_index];
59 const char* unicharset_file = argv[++argv_index];
60 tprintf("Loading unicharset from '%s'\n", unicharset_file);
61 if (!classify->getDict().getUnicharset().load_from_file(unicharset_file)) {
62 tprintf("Failed to load unicharset from '%s'\n", unicharset_file);
63 delete classify;
64 return 1;
65 }
66 const UNICHARSET &unicharset = classify->getDict().getUnicharset();
67 if (argc == 4 || argc == 6) {
68 tesseract::Trie trie(
69 // the first 3 arguments are not used in this case
71 unicharset.size(), classify->getDict().dawg_debug_level);
72 tprintf("Reading word list from '%s'\n", wordlist_filename);
73 if (!trie.read_and_add_word_list(wordlist_filename, unicharset,
74 reverse_policy)) {
75 tprintf("Failed to add word list from '%s'\n", wordlist_filename);
76 exit(1);
77 }
78 tprintf("Reducing Trie to SquishedDawg\n");
80 if (dawg != nullptr && dawg->NumEdges() > 0) {
81 tprintf("Writing squished DAWG to '%s'\n", dawg_filename);
82 dawg->write_squished_dawg(dawg_filename);
83 } else {
84 tprintf("Dawg is empty, skip producing the output file\n");
85 }
86 delete dawg;
87 } else if (argc == 5) {
88 tprintf("Loading dawg DAWG from '%s'\n", dawg_filename);
90 dawg_filename,
91 // these 3 arguments are not used in this case
93 classify->getDict().dawg_debug_level);
94 tprintf("Checking word list from '%s'\n", wordlist_filename);
95 words.check_for_words(wordlist_filename, unicharset, true);
96 } else { // should never get here
97 tprintf("Invalid command-line options\n");
98 exit(1);
99 }
100 delete classify;
101 return 0;
102}
@ SYSTEM_DAWG_PERM
Definition: ratngs.h:241
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:35
int main(int argc, char **argv)
@ DAWG_TYPE_WORD
Definition: dawg.h:70
int size() const
Definition: unicharset.h:341
bool load_from_file(const char *const filename, bool skip_fragments)
Definition: unicharset.h:388
virtual Dict & getDict()
Definition: classify.h:107
int check_for_words(const char *filename, const UNICHARSET &unicharset, bool enable_wildcard) const
Definition: dawg.cpp:69
bool write_squished_dawg(TFile *file)
Writes the squished/reduced Dawg to a file.
Definition: dawg.cpp:368
int dawg_debug_level
Definition: dict.h:622
const UNICHARSET & getUnicharset() const
Definition: dict.h:101
bool read_and_add_word_list(const char *filename, const UNICHARSET &unicharset, Trie::RTLReversePolicy reverse)
Definition: trie.cpp:281
SquishedDawg * trie_to_dawg()
Definition: trie.cpp:511
RTLReversePolicy
Definition: trie.h:58
@ RRP_DO_NO_REVERSE
Definition: trie.h:59
static const char * get_reverse_policy_name(RTLReversePolicy reverse_policy)
Definition: trie.cpp:52