Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/thirdparty/tesseract/src/training/wordlist2dawg.cpp @ 2:b50eed0cc0ef upstream
ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4.
The directory name has changed: no version number in the expanded directory now.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:43:07 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 1:1d09e1dec1d9 | 2:b50eed0cc0ef |
|---|---|
| 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 // | |
| 17 /////////////////////////////////////////////////////////////////////// | |
| 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 | |
| 31 using namespace tesseract; | |
| 32 | |
| 33 int main(int argc, char **argv) { | |
| 34 tesseract::CheckSharedLibraryVersion(); | |
| 35 | |
| 36 if (argc > 1 && (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version"))) { | |
| 37 printf("%s\n", tesseract::TessBaseAPI::Version()); | |
| 38 return EXIT_SUCCESS; | |
| 39 } else if (!(argc == 4 || (argc == 5 && strcmp(argv[1], "-t") == 0) || | |
| 40 (argc == 6 && strcmp(argv[1], "-r") == 0))) { | |
| 41 printf( | |
| 42 "Usage: %s -v | --version |\n" | |
| 43 " %s [-t | -r [reverse policy] ] word_list_file" | |
| 44 " dawg_file unicharset_file\n", | |
| 45 argv[0], argv[0]); | |
| 46 return EXIT_FAILURE; | |
| 47 } | |
| 48 tesseract::Classify classify; | |
| 49 int argv_index = 0; | |
| 50 if (argc == 5) { | |
| 51 ++argv_index; | |
| 52 } | |
| 53 tesseract::Trie::RTLReversePolicy reverse_policy = tesseract::Trie::RRP_DO_NO_REVERSE; | |
| 54 if (argc == 6) { | |
| 55 ++argv_index; | |
| 56 int tmp_int; | |
| 57 sscanf(argv[++argv_index], "%d", &tmp_int); | |
| 58 reverse_policy = static_cast<tesseract::Trie::RTLReversePolicy>(tmp_int); | |
| 59 tprintf("Set reverse_policy to %s\n", tesseract::Trie::get_reverse_policy_name(reverse_policy)); | |
| 60 } | |
| 61 const char *wordlist_filename = argv[++argv_index]; | |
| 62 const char *dawg_filename = argv[++argv_index]; | |
| 63 const char *unicharset_file = argv[++argv_index]; | |
| 64 tprintf("Loading unicharset from '%s'\n", unicharset_file); | |
| 65 if (!classify.getDict().getUnicharset().load_from_file(unicharset_file)) { | |
| 66 tprintf("Failed to load unicharset from '%s'\n", unicharset_file); | |
| 67 return EXIT_FAILURE; | |
| 68 } | |
| 69 const UNICHARSET &unicharset = classify.getDict().getUnicharset(); | |
| 70 if (argc == 4 || argc == 6) { | |
| 71 tesseract::Trie trie( | |
| 72 // the first 3 arguments are not used in this case | |
| 73 tesseract::DAWG_TYPE_WORD, "", SYSTEM_DAWG_PERM, unicharset.size(), | |
| 74 classify.getDict().dawg_debug_level); | |
| 75 tprintf("Reading word list from '%s'\n", wordlist_filename); | |
| 76 if (!trie.read_and_add_word_list(wordlist_filename, unicharset, reverse_policy)) { | |
| 77 tprintf("Failed to add word list from '%s'\n", wordlist_filename); | |
| 78 return EXIT_FAILURE; | |
| 79 } | |
| 80 tprintf("Reducing Trie to SquishedDawg\n"); | |
| 81 std::unique_ptr<tesseract::SquishedDawg> dawg(trie.trie_to_dawg()); | |
| 82 if (dawg && dawg->NumEdges() > 0) { | |
| 83 tprintf("Writing squished DAWG to '%s'\n", dawg_filename); | |
| 84 dawg->write_squished_dawg(dawg_filename); | |
| 85 } else { | |
| 86 tprintf("Dawg is empty, skip producing the output file\n"); | |
| 87 } | |
| 88 } else if (argc == 5) { | |
| 89 tprintf("Loading dawg DAWG from '%s'\n", dawg_filename); | |
| 90 tesseract::SquishedDawg words(dawg_filename, | |
| 91 // these 3 arguments are not used in this case | |
| 92 tesseract::DAWG_TYPE_WORD, "", SYSTEM_DAWG_PERM, | |
| 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 return EXIT_FAILURE; | |
| 99 } | |
| 100 return EXIT_SUCCESS; | |
| 101 } |
