tesseract  4.00.00dev
tessdatamanager.h
Go to the documentation of this file.
1 // File: tessdatamanager.h
3 // Description: Functions to handle loading/combining tesseract data files.
4 // Author: Daria Antonova
5 // Created: Wed Jun 03 11:26:43 PST 2009
6 //
7 // (C) Copyright 2009, Google Inc.
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 //
19 
20 #ifndef TESSERACT_CCUTIL_TESSDATAMANAGER_H_
21 #define TESSERACT_CCUTIL_TESSDATAMANAGER_H_
22 
23 #include <stdio.h>
24 
25 #include "host.h"
26 #include "strngs.h"
27 #include "tprintf.h"
28 
29 static const char kTrainedDataSuffix[] = "traineddata";
30 
31 // When adding new tessdata types and file suffixes, please make sure to
32 // update TessdataType enum, kTessdataFileSuffixes and kTessdataFileIsText.
33 static const char kLangConfigFileSuffix[] = "config";
34 static const char kUnicharsetFileSuffix[] = "unicharset";
35 static const char kAmbigsFileSuffix[] = "unicharambigs";
36 static const char kBuiltInTemplatesFileSuffix[] = "inttemp";
37 static const char kBuiltInCutoffsFileSuffix[] = "pffmtable";
38 static const char kNormProtoFileSuffix[] = "normproto";
39 static const char kPuncDawgFileSuffix[] = "punc-dawg";
40 static const char kSystemDawgFileSuffix[] = "word-dawg";
41 static const char kNumberDawgFileSuffix[] = "number-dawg";
42 static const char kFreqDawgFileSuffix[] = "freq-dawg";
43 static const char kFixedLengthDawgsFileSuffix[] = "fixed-length-dawgs";
44 static const char kCubeUnicharsetFileSuffix[] = "cube-unicharset";
45 static const char kCubeSystemDawgFileSuffix[] = "cube-word-dawg";
46 static const char kShapeTableFileSuffix[] = "shapetable";
47 static const char kBigramDawgFileSuffix[] = "bigram-dawg";
48 static const char kUnambigDawgFileSuffix[] = "unambig-dawg";
49 static const char kParamsModelFileSuffix[] = "params-model";
50 static const char kLSTMModelFileSuffix[] = "lstm";
51 static const char kLSTMPuncDawgFileSuffix[] = "lstm-punc-dawg";
52 static const char kLSTMSystemDawgFileSuffix[] = "lstm-word-dawg";
53 static const char kLSTMNumberDawgFileSuffix[] = "lstm-number-dawg";
54 
55 namespace tesseract {
56 
68  TESSDATA_FIXED_LENGTH_DAWGS, // 10 // deprecated
69  TESSDATA_CUBE_UNICHARSET, // 11 // deprecated
70  TESSDATA_CUBE_SYSTEM_DAWG, // 12 // deprecated
79 
81 };
82 
87 static const char *const kTessdataFileSuffixes[] = {
88  kLangConfigFileSuffix, // 0
89  kUnicharsetFileSuffix, // 1
90  kAmbigsFileSuffix, // 2
91  kBuiltInTemplatesFileSuffix, // 3
92  kBuiltInCutoffsFileSuffix, // 4
93  kNormProtoFileSuffix, // 5
94  kPuncDawgFileSuffix, // 6
95  kSystemDawgFileSuffix, // 7
96  kNumberDawgFileSuffix, // 8
97  kFreqDawgFileSuffix, // 9
98  kFixedLengthDawgsFileSuffix, // 10 // deprecated
99  kCubeUnicharsetFileSuffix, // 11 // deprecated
100  kCubeSystemDawgFileSuffix, // 12 // deprecated
101  kShapeTableFileSuffix, // 13
102  kBigramDawgFileSuffix, // 14
103  kUnambigDawgFileSuffix, // 15
104  kParamsModelFileSuffix, // 16
105  kLSTMModelFileSuffix, // 17
106  kLSTMPuncDawgFileSuffix, // 18
107  kLSTMSystemDawgFileSuffix, // 19
108  kLSTMNumberDawgFileSuffix, // 20
109 };
110 
118 static const int kMaxNumTessdataEntries = 1000;
119 
120 
122  public:
123  TessdataManager() : reader_(nullptr), is_loaded_(false), swap_(false) {}
124  explicit TessdataManager(FileReader reader)
125  : reader_(reader), is_loaded_(false), swap_(false) {}
127 
128  bool swap() const { return swap_; }
129  bool is_loaded() const { return is_loaded_; }
130 
135  bool Init(const char *data_file_name);
136  // Loads from the given memory buffer as if a file, remembering name as some
137  // arbitrary source id for caching.
138  bool LoadMemBuffer(const char *name, const char *data, int size);
139 
140  // Saves to the given filename.
141  bool SaveFile(const STRING &filename, FileWriter writer) const;
142  // Serializes to the given vector.
143  void Serialize(GenericVector<char> *data) const;
144  // Resets to the initial state, keeping the reader.
145  void Clear();
146 
147  // Prints a directory of contents.
148  void Directory() const;
149 
150  // Opens the given TFile pointer to the given component type.
151  // Returns false in case of failure.
152  bool GetComponent(TessdataType type, TFile *fp);
153 
154  // Returns true if the base Tesseract components are present.
155  bool IsBaseAvailable() const {
156  return !entries_[TESSDATA_UNICHARSET].empty() &&
157  !entries_[TESSDATA_INTTEMP].empty();
158  }
159 
160  // Returns true if the LSTM components are present.
161  bool IsLSTMAvailable() const { return !entries_[TESSDATA_LSTM].empty(); }
162 
163  // Return the name of the underlying data file.
164  const STRING &GetDataFileName() const { return data_file_name_; }
165 
171  bool CombineDataFiles(const char *language_data_path_prefix,
172  const char *output_filename);
173 
179  bool OverwriteComponents(const char *new_traineddata_filename,
180  char **component_filenames,
181  int num_new_components);
182 
193  bool ExtractToFile(const char *filename);
194 
201  static bool TessdataTypeFromFileSuffix(const char *suffix,
202  TessdataType *type);
203 
208  static bool TessdataTypeFromFileName(const char *filename,
209  TessdataType *type);
210 
211  private:
212  // Name of file it came from.
213  STRING data_file_name_;
214  // Function to load the file when we need it.
215  FileReader reader_;
216  // True if the file has been loaded.
217  bool is_loaded_;
218  // True if the bytes need swapping.
219  bool swap_;
220  // Contents of each element of the traineddata file.
222 };
223 
224 } // namespace tesseract
225 
226 #endif // TESSERACT_CCUTIL_TESSDATAMANAGER_H_
bool OverwriteComponents(const char *new_traineddata_filename, char **component_filenames, int num_new_components)
static bool TessdataTypeFromFileName(const char *filename, TessdataType *type)
TessdataManager(FileReader reader)
bool(* FileReader)(const STRING &filename, GenericVector< char > *data)
bool LoadMemBuffer(const char *name, const char *data, int size)
voidpf void uLong size
Definition: ioapi.h:39
bool empty() const
Definition: genericvector.h:90
bool ExtractToFile(const char *filename)
bool SaveFile(const STRING &filename, FileWriter writer) const
Definition: strngs.h:45
bool GetComponent(TessdataType type, TFile *fp)
void Serialize(GenericVector< char > *data) const
bool(* FileWriter)(const GenericVector< char > &data, const STRING &filename)
static bool TessdataTypeFromFileSuffix(const char *suffix, TessdataType *type)
const char * filename
Definition: ioapi.h:38
const STRING & GetDataFileName() const
bool CombineDataFiles(const char *language_data_path_prefix, const char *output_filename)
bool Init(const char *data_file_name)