Tesseract
3.02
|
00001 /********************************************************************** 00002 * File: tesseract_cube_combiner.h 00003 * Description: Declaration of the Tesseract & Cube results combiner Class 00004 * Author: Ahmad Abdulkader 00005 * Created: 2008 00006 * 00007 * (C) Copyright 2008, Google Inc. 00008 ** Licensed under the Apache License, Version 2.0 (the "License"); 00009 ** you may not use this file except in compliance with the License. 00010 ** You may obtain a copy of the License at 00011 ** http://www.apache.org/licenses/LICENSE-2.0 00012 ** Unless required by applicable law or agreed to in writing, software 00013 ** distributed under the License is distributed on an "AS IS" BASIS, 00014 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00015 ** See the License for the specific language governing permissions and 00016 ** limitations under the License. 00017 * 00018 **********************************************************************/ 00019 00020 // The TesseractCubeCombiner class provides the functionality of combining 00021 // the recognition results of Tesseract and Cube at the word level 00022 00023 #ifndef TESSERACT_CCMAIN_TESSERACT_CUBE_COMBINER_H 00024 #define TESSERACT_CCMAIN_TESSERACT_CUBE_COMBINER_H 00025 00026 #include <string> 00027 #include <vector> 00028 #include "pageres.h" 00029 00030 #ifdef _WIN32 00031 #include <windows.h> 00032 using namespace std; 00033 #endif 00034 00035 #ifdef USE_STD_NAMESPACE 00036 using std::string; 00037 using std::vector; 00038 #endif 00039 00040 namespace tesseract { 00041 00042 class CubeObject; 00043 class NeuralNet; 00044 class CubeRecoContext; 00045 class WordAltList; 00046 00047 class TesseractCubeCombiner { 00048 public: 00049 explicit TesseractCubeCombiner(CubeRecoContext *cube_cntxt); 00050 virtual ~TesseractCubeCombiner(); 00051 00052 // There are 2 public methods for combining the results of tesseract 00053 // and cube. Both return the probability that the Tesseract result is 00054 // correct. The difference between the two interfaces is in how the 00055 // passed-in CubeObject is used. 00056 00057 // The CubeObject parameter is used for 2 purposes: 1) to retrieve 00058 // cube's alt list, and 2) to compute cube's word cost for the 00059 // tesseract result. Both uses may modify the state of the 00060 // CubeObject (including the BeamSearch state) with a call to 00061 // RecognizeWord(). 00062 float CombineResults(WERD_RES *tess_res, CubeObject *cube_obj); 00063 00064 // The alt_list parameter is expected to have been extracted from the 00065 // CubeObject that recognized the word to be combined. The cube_obj 00066 // parameter passed in is a separate instance to be used only by 00067 // the combiner. 00068 float CombineResults(WERD_RES *tess_res, CubeObject *cube_obj, 00069 WordAltList *alt_list); 00070 00071 // Public method for computing the combiner features. The agreement 00072 // output parameter will be true if both answers are identical, 00073 // false otherwise. Modifies the cube_alt_list, so no assumptions 00074 // should be made about its state upon return. 00075 bool ComputeCombinerFeatures(const string &tess_res, 00076 int tess_confidence, 00077 CubeObject *cube_obj, 00078 WordAltList *cube_alt_list, 00079 vector<double> *features, 00080 bool *agreement); 00081 00082 // Is the word valid according to Tesseract's language model 00083 bool ValidWord(const string &str); 00084 00085 // Loads the combiner neural network from file, using cube_cntxt_ 00086 // to find path. 00087 bool LoadCombinerNet(); 00088 private: 00089 // Normalize a UTF-8 string. Converts the UTF-8 string to UTF32 and optionally 00090 // strips punc and/or normalizes case and then converts back 00091 string NormalizeString(const string &str, bool remove_punc, bool norm_case); 00092 00093 // Compares 2 strings after optionally normalizing them and or stripping 00094 // punctuation 00095 int CompareStrings(const string &str1, const string &str2, bool ignore_punc, 00096 bool norm_case); 00097 00098 NeuralNet *combiner_net_; // pointer to the combiner NeuralNet object 00099 CubeRecoContext *cube_cntxt_; // used for language ID and data paths 00100 }; 00101 } 00102 00103 #endif // TESSERACT_CCMAIN_TESSERACT_CUBE_COMBINER_H