tesseract 4.1.1
Loading...
Searching...
No Matches
dawg_cache.cpp
Go to the documentation of this file.
1
2// 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
27namespace tesseract {
28
29struct 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_) {
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;
84 dawg_type = DAWG_TYPE_WORD;
85 perm_type = FREQ_DAWG_PERM;
86 break;
87 default:
88 return nullptr;
89 }
90 auto *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
PermuterType
Definition: ratngs.h:232
@ FREQ_DAWG_PERM
Definition: ratngs.h:244
@ SYSTEM_DAWG_PERM
Definition: ratngs.h:241
@ NUMBER_PERM
Definition: ratngs.h:239
@ PUNC_PERM
Definition: ratngs.h:234
@ COMPOUND_PERM
Definition: ratngs.h:245
_TessMemberResultCallback_0_0< true, R, T1 >::base * NewTessCallback(T1 *obj, R(T2::*member)())
Definition: tesscallback.h:91
DawgType
Definition: dawg.h:68
@ DAWG_TYPE_NUMBER
Definition: dawg.h:71
@ DAWG_TYPE_WORD
Definition: dawg.h:70
@ DAWG_TYPE_PUNCTUATION
Definition: dawg.h:69
@ TESSDATA_UNAMBIG_DAWG
@ TESSDATA_LSTM_SYSTEM_DAWG
@ TESSDATA_NUMBER_DAWG
@ TESSDATA_LSTM_PUNC_DAWG
@ TESSDATA_BIGRAM_DAWG
@ TESSDATA_LSTM_NUMBER_DAWG
@ TESSDATA_SYSTEM_DAWG
Definition: strngs.h:45
bool GetComponent(TessdataType type, TFile *fp)
const STRING & GetDataFileName() const
DawgLoader(const STRING &lang, TessdataType tessdata_dawg_type, int dawg_debug_level, TessdataManager *data_file)
Definition: dawg_cache.cpp:30
TessdataType tessdata_dawg_type_
Definition: dawg_cache.cpp:41
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