tesseract  5.0.0-alpha-619-ge9db
context.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * File: context.cpp (Formerly context.c)
4  * Description: Context checking functions
5  * Author: Mark Seaman, OCR Technology
6  *
7  * (c) Copyright 1990, Hewlett-Packard Company.
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  *
18  *****************************************************************************/
19 
20 #include "dict.h"
21 #include "unicharset.h"
22 
23 namespace tesseract {
24 
25 static const int kMinAbsoluteGarbageWordLength = 10;
26 static const float kMinAbsoluteGarbageAlphanumFrac = 0.5f;
27 
28 const int case_state_table[6][4] = {
29  {/* 0. Beginning of word */
30  /* P U L D */
31  /* -1. Error on case */
32  0, 1, 5, 4},
33  {/* 1. After initial capital */
34  0, 3, 2, 4},
35  {/* 2. After lower case */
36  0, -1, 2, -1},
37  {/* 3. After upper case */
38  0, 3, -1, 4},
39  {/* 4. After a digit */
40  0, -1, -1, 4},
41  {/* 5. After initial lower case */
42  5, -1, 2, -1},
43 };
44 
45 int Dict::case_ok(const WERD_CHOICE &word) const {
46  int state = 0;
47  int x;
48  const UNICHARSET* unicharset = word.unicharset();
49  for (x = 0; x < word.length(); ++x) {
50  UNICHAR_ID ch_id = word.unichar_id(x);
51  if (unicharset->get_isupper(ch_id))
52  state = case_state_table[state][1];
53  else if (unicharset->get_islower(ch_id))
54  state = case_state_table[state][2];
55  else if (unicharset->get_isdigit(ch_id))
56  state = case_state_table[state][3];
57  else
58  state = case_state_table[state][0];
59  if (state == -1) return false;
60  }
61  return state != 5; // single lower is bad
62 }
63 
64 bool Dict::absolute_garbage(const WERD_CHOICE &word,
65  const UNICHARSET &unicharset) {
66  if (word.length() < kMinAbsoluteGarbageWordLength) return false;
67  int num_alphanum = 0;
68  for (int x = 0; x < word.length(); ++x) {
69  num_alphanum += (unicharset.get_isalpha(word.unichar_id(x)) ||
70  unicharset.get_isdigit(word.unichar_id(x)));
71  }
72  return (static_cast<float>(num_alphanum) /
73  static_cast<float>(word.length()) < kMinAbsoluteGarbageAlphanumFrac);
74 }
75 
76 } // namespace tesseract
tesseract::case_state_table
const int case_state_table[6][4]
Definition: context.cpp:44
UNICHARSET::get_islower
bool get_islower(UNICHAR_ID unichar_id) const
Definition: unicharset.h:488
dict.h
WERD_CHOICE::unichar_id
UNICHAR_ID unichar_id(int index) const
Definition: ratngs.h:303
UNICHARSET::get_isdigit
bool get_isdigit(UNICHAR_ID unichar_id) const
Definition: unicharset.h:502
WERD_CHOICE
Definition: ratngs.h:261
UNICHARSET::get_isalpha
bool get_isalpha(UNICHAR_ID unichar_id) const
Definition: unicharset.h:481
tesseract::Dict::absolute_garbage
bool absolute_garbage(const WERD_CHOICE &word, const UNICHARSET &unicharset)
Definition: context.cpp:80
WERD_CHOICE::unicharset
const UNICHARSET * unicharset() const
Definition: ratngs.h:288
unicharset.h
UNICHARSET
Definition: unicharset.h:145
tesseract
Definition: baseapi.h:65
UNICHAR_ID
int UNICHAR_ID
Definition: unichar.h:36
UNICHARSET::get_isupper
bool get_isupper(UNICHAR_ID unichar_id) const
Definition: unicharset.h:495
WERD_CHOICE::length
int length() const
Definition: ratngs.h:291
tesseract::Dict::case_ok
int case_ok(const WERD_CHOICE &word) const
Check a string to see if it matches a set of lexical rules.
Definition: context.cpp:61