tesseract  4.0.0-1-g2a2b
dawg_cache.cpp
Go to the documentation of this file.
1 // File: dawg_cache.cpp
3 // Description: A class that knows about loading and caching dawgs.
4 // Author: David Eger
5 // Created: Fri Jan 27 12:08:00 PST 2012
6 //
7 // (C) Copyright 2012, 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 #include "dawg_cache.h"
21 
22 #include "dawg.h"
23 #include "object_cache.h"
24 #include "strngs.h"
25 #include "tessdatamanager.h"
26 
27 namespace tesseract {
28 
29 struct DawgLoader {
30  DawgLoader(const STRING &lang, TessdataType tessdata_dawg_type,
31  int dawg_debug_level, TessdataManager *data_file)
32  : lang_(lang),
33  data_file_(data_file),
34  tessdata_dawg_type_(tessdata_dawg_type),
35  dawg_debug_level_(dawg_debug_level) {}
36 
37  Dawg *Load();
38 
43 };
44 
46  TessdataType tessdata_dawg_type,
47  int debug_level, TessdataManager *data_file) {
48  STRING data_id = data_file->GetDataFileName();
49  data_id += kTessdataFileSuffixes[tessdata_dawg_type];
50  DawgLoader loader(lang, tessdata_dawg_type, debug_level, data_file);
51  return dawgs_.Get(data_id, NewTessCallback(&loader, &DawgLoader::Load));
52 }
53 
55  TFile fp;
56  if (!data_file_->GetComponent(tessdata_dawg_type_, &fp)) return nullptr;
57  DawgType dawg_type;
58  PermuterType perm_type;
59  switch (tessdata_dawg_type_) {
60  case TESSDATA_PUNC_DAWG:
62  dawg_type = DAWG_TYPE_PUNCTUATION;
63  perm_type = PUNC_PERM;
64  break;
67  dawg_type = DAWG_TYPE_WORD;
68  perm_type = SYSTEM_DAWG_PERM;
69  break;
72  dawg_type = DAWG_TYPE_NUMBER;
73  perm_type = NUMBER_PERM;
74  break;
76  dawg_type = DAWG_TYPE_WORD; // doesn't actually matter
77  perm_type = COMPOUND_PERM; // doesn't actually matter
78  break;
80  dawg_type = DAWG_TYPE_WORD;
81  perm_type = SYSTEM_DAWG_PERM;
82  break;
83  case TESSDATA_FREQ_DAWG:
84  dawg_type = DAWG_TYPE_WORD;
85  perm_type = FREQ_DAWG_PERM;
86  break;
87  default:
88  return nullptr;
89  }
90  SquishedDawg *retval =
91  new SquishedDawg(dawg_type, lang_, perm_type, dawg_debug_level_);
92  if (retval->Load(&fp)) return retval;
93  delete retval;
94  return nullptr;
95 }
96 
97 } // namespace tesseract
bool Load(TFile *fp)
Definition: dawg.h:439
bool GetComponent(TessdataType type, TFile *fp)
TessdataManager * data_file_
Definition: dawg_cache.cpp:40
Dawg * GetSquishedDawg(const STRING &lang, TessdataType tessdata_dawg_type, int debug_level, TessdataManager *data_file)
Definition: dawg_cache.cpp:45
const STRING & GetDataFileName() const
TessdataType tessdata_dawg_type_
Definition: dawg_cache.cpp:41
Definition: strngs.h:45
DawgLoader(const STRING &lang, TessdataType tessdata_dawg_type, int dawg_debug_level, TessdataManager *data_file)
Definition: dawg_cache.cpp:30
DawgType
Definition: dawg.h:72
_ConstTessMemberResultCallback_0_0< true, R, T1 >::base * NewTessCallback(const T1 *obj, R(T2::*member)() const)
Definition: tesscallback.h:106
PermuterType
Definition: ratngs.h:242