tesseract 3.04.01

training/wordlist2dawg.cpp

Go to the documentation of this file.
00001 
00002 // File:        wordlist2dawg.cpp
00003 // Description: Program to generate a DAWG from a word list file
00004 // Author:      Thomas Kielbus
00005 // Created:     Thu May 10 18:11:42 PDT 2007
00006 //
00007 // (C) Copyright 2006, Google Inc.
00008 // Licensed under the Apache License, Version 2.0 (the "License");
00009 // you may not use this file except in compliance with the License.
00010 // You may obtain a copy of the License at
00011 // http://www.apache.org/licenses/LICENSE-2.0
00012 // Unless required by applicable law or agreed to in writing, software
00013 // distributed under the License is distributed on an "AS IS" BASIS,
00014 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 // See the License for the specific language governing permissions and
00016 // limitations under the License.
00017 //
00019 
00020 // Given a file that contains a list of words (one word per line) this program
00021 // generates the corresponding squished DAWG file.
00022 
00023 #include <stdio.h>
00024 
00025 #include "classify.h"
00026 #include "dawg.h"
00027 #include "dict.h"
00028 #include "emalloc.h"
00029 #include "freelist.h"
00030 #include "helpers.h"
00031 #include "serialis.h"
00032 #include "trie.h"
00033 #include "unicharset.h"
00034 
00035 int main(int argc, char** argv) {
00036   if (!(argc == 4 || (argc == 5 && strcmp(argv[1], "-t") == 0) ||
00037       (argc == 6 && strcmp(argv[1], "-r") == 0))) {
00038     printf("Usage: %s [-t | -r [reverse policy] ] word_list_file"
00039            " dawg_file unicharset_file\n", argv[0]);
00040     return 1;
00041   }
00042   tesseract::Classify *classify = new tesseract::Classify();
00043   int argv_index = 0;
00044   if (argc == 5) ++argv_index;
00045   tesseract::Trie::RTLReversePolicy reverse_policy =
00046       tesseract::Trie::RRP_DO_NO_REVERSE;
00047   if (argc == 6) {
00048     ++argv_index;
00049     int tmp_int;
00050     sscanf(argv[++argv_index], "%d", &tmp_int);
00051     reverse_policy = static_cast<tesseract::Trie::RTLReversePolicy>(tmp_int);
00052     tprintf("Set reverse_policy to %s\n",
00053             tesseract::Trie::get_reverse_policy_name(reverse_policy));
00054   }
00055   if (argc == 7) argv_index += 3;
00056   const char* wordlist_filename = argv[++argv_index];
00057   const char* dawg_filename = argv[++argv_index];
00058   const char* unicharset_file = argv[++argv_index];
00059   tprintf("Loading unicharset from '%s'\n", unicharset_file);
00060   if (!classify->getDict().getUnicharset().load_from_file(unicharset_file)) {
00061     tprintf("Failed to load unicharset from '%s'\n", unicharset_file);
00062     delete classify;
00063     return 1;
00064   }
00065   const UNICHARSET &unicharset = classify->getDict().getUnicharset();
00066   if (argc == 4 || argc == 6) {
00067     tesseract::Trie trie(
00068         // the first 3 arguments are not used in this case
00069         tesseract::DAWG_TYPE_WORD, "", SYSTEM_DAWG_PERM,
00070         unicharset.size(), classify->getDict().dawg_debug_level);
00071     tprintf("Reading word list from '%s'\n", wordlist_filename);
00072     if (!trie.read_and_add_word_list(wordlist_filename, unicharset,
00073                                      reverse_policy)) {
00074       tprintf("Failed to add word list from '%s'\n", wordlist_filename);
00075       exit(1);
00076     }
00077     tprintf("Reducing Trie to SquishedDawg\n");
00078     tesseract::SquishedDawg *dawg = trie.trie_to_dawg();
00079     if (dawg != NULL && dawg->NumEdges() > 0) {
00080       tprintf("Writing squished DAWG to '%s'\n", dawg_filename);
00081       dawg->write_squished_dawg(dawg_filename);
00082     } else {
00083       tprintf("Dawg is empty, skip producing the output file\n");
00084     }
00085     delete dawg;
00086   } else if (argc == 5) {
00087     tprintf("Loading dawg DAWG from '%s'\n", dawg_filename);
00088     tesseract::SquishedDawg words(
00089         dawg_filename,
00090         // these 3 arguments are not used in this case
00091         tesseract::DAWG_TYPE_WORD, "", SYSTEM_DAWG_PERM,
00092         classify->getDict().dawg_debug_level);
00093     tprintf("Checking word list from '%s'\n", wordlist_filename);
00094     words.check_for_words(wordlist_filename, unicharset, true);
00095   } else {  // should never get here
00096     tprintf("Invalid command-line options\n");
00097     exit(1);
00098   }
00099   delete classify;
00100   return 0;
00101 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines