Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/thirdparty/tesseract/src/training/dawg2wordlist.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: 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 // | |
| 17 /////////////////////////////////////////////////////////////////////// | |
| 18 | |
| 19 #include "commontraining.h" // CheckSharedLibraryVersion | |
| 20 #include "dawg.h" | |
| 21 #include "trie.h" | |
| 22 #include "unicharset.h" | |
| 23 | |
| 24 #include "serialis.h" | |
| 25 | |
| 26 using namespace tesseract; | |
| 27 | |
| 28 static 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 | |
| 46 class WordOutputter { | |
| 47 public: | |
| 48 WordOutputter(FILE *file) : file_(file) {} | |
| 49 void output_word(const char *word) { | |
| 50 fprintf(file_, "%s\n", word); | |
| 51 } | |
| 52 | |
| 53 private: | |
| 54 FILE *file_; | |
| 55 }; | |
| 56 | |
| 57 // returns 0 if successful. | |
| 58 static 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 | |
| 71 int 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 } |
