tesseract  4.00.00dev
combine_lang_model.cpp
Go to the documentation of this file.
1 // Copyright 2017 Google Inc. All Rights Reserved.
2 // Author: rays@google.com (Ray Smith)
3 // Purpose: Program to generate a traineddata file that can be used to train an
4 // LSTM-based neural network model from a unicharset and an optional
5 // set of wordlists. Eliminates the need to run
6 // set_unicharset_properties, wordlist2dawg, some non-existent binary
7 // to generate the recoder, and finally combine_tessdata.
8 
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 #include "commandlineflags.h"
19 #include "lang_model_helpers.h"
20 #include "tprintf.h"
22 
23 STRING_PARAM_FLAG(input_unicharset, "",
24  "Unicharset to complete and use in encoding");
25 STRING_PARAM_FLAG(script_dir, "",
26  "Directory name for input script unicharsets");
27 STRING_PARAM_FLAG(words, "",
28  "File listing words to use for the system dictionary");
29 STRING_PARAM_FLAG(puncs, "", "File listing punctuation patterns");
30 STRING_PARAM_FLAG(numbers, "", "File listing number patterns");
31 STRING_PARAM_FLAG(output_dir, "", "Root directory for output files");
32 STRING_PARAM_FLAG(version_str, "", "Version string to add to traineddata file");
33 STRING_PARAM_FLAG(lang, "", "Name of language being processed");
34 BOOL_PARAM_FLAG(lang_is_rtl, false,
35  "True if lang being processed is written right-to-left");
36 BOOL_PARAM_FLAG(pass_through_recoder, false,
37  "If true, the recoder is a simple pass-through of the"
38  " unicharset. Otherwise, potentially a compression of it");
39 
40 int main(int argc, char** argv) {
41  tesseract::ParseCommandLineFlags(argv[0], &argc, &argv, true);
42 
43  // Check validity of input flags.
44  if (FLAGS_input_unicharset.empty() || FLAGS_script_dir.empty() ||
45  FLAGS_output_dir.empty() || FLAGS_lang.empty()) {
46  tprintf("Usage: %s --input_unicharset filename --script_dir dirname\n",
47  argv[0]);
48  tprintf(" --output_dir rootdir --lang lang [--lang_is_rtl]\n");
49  tprintf(" [--words file --puncs file --numbers file]\n");
50  tprintf("Sets properties on the input unicharset file, and writes:\n");
51  tprintf("rootdir/lang/lang.charset_size=ddd.txt\n");
52  tprintf("rootdir/lang/lang.traineddata\n");
53  tprintf("rootdir/lang/lang.unicharset\n");
54  tprintf("If the 3 word lists are provided, the dawgs are also added to");
55  tprintf(" the traineddata file.\n");
56  tprintf("The output unicharset and charset_size files are just for human");
57  tprintf(" readability.\n");
58  exit(1);
59  }
60  GenericVector<STRING> words, puncs, numbers;
61  // If these reads fail, we get a warning message and an empty list of words.
62  tesseract::ReadFile(FLAGS_words.c_str(), nullptr).split('\n', &words);
63  tesseract::ReadFile(FLAGS_puncs.c_str(), nullptr).split('\n', &puncs);
64  tesseract::ReadFile(FLAGS_numbers.c_str(), nullptr).split('\n', &numbers);
65  // Load the input unicharset
66  UNICHARSET unicharset;
67  if (!unicharset.load_from_file(FLAGS_input_unicharset.c_str(), false)) {
68  tprintf("Failed to load unicharset from %s\n",
69  FLAGS_input_unicharset.c_str());
70  return 1;
71  }
72  tprintf("Loaded unicharset of size %d from file %s\n", unicharset.size(),
73  FLAGS_input_unicharset.c_str());
74 
75  // Set unichar properties
76  tprintf("Setting unichar properties\n");
77  tesseract::SetupBasicProperties(/*report_errors*/ true,
78  /*decompose (NFD)*/ false, &unicharset);
79  tprintf("Setting script properties\n");
80  tesseract::SetScriptProperties(FLAGS_script_dir.c_str(), &unicharset);
81  // Combine everything into a traineddata file.
83  unicharset, FLAGS_script_dir.c_str(), FLAGS_version_str.c_str(),
84  FLAGS_output_dir.c_str(), FLAGS_lang.c_str(), FLAGS_pass_through_recoder,
85  words, puncs, numbers, FLAGS_lang_is_rtl, /*reader*/ nullptr,
86  /*writer*/ nullptr);
87 }
int CombineLangModel(const UNICHARSET &unicharset, const string &script_dir, const string &version_str, const string &output_dir, const string &lang, bool pass_through_recoder, const GenericVector< STRING > &words, const GenericVector< STRING > &puncs, const GenericVector< STRING > &numbers, bool lang_is_rtl, FileReader reader, FileWriter writer)
void SetScriptProperties(const string &script_dir, UNICHARSET *unicharset)
void SetupBasicProperties(bool report_errors, bool decompose, UNICHARSET *unicharset)
STRING_PARAM_FLAG(input_unicharset, "", "Unicharset to complete and use in encoding")
#define tprintf(...)
Definition: tprintf.h:31
BOOL_PARAM_FLAG(lang_is_rtl, false, "True if lang being processed is written right-to-left")
int size() const
Definition: unicharset.h:338
void ParseCommandLineFlags(const char *usage, int *argc, char ***argv, const bool remove_flags)
bool load_from_file(const char *const filename, bool skip_fragments)
Definition: unicharset.h:387
STRING ReadFile(const string &filename, FileReader reader)
int main(int argc, char **argv)