tesseract  4.0.0-1-g2a2b
unicharmap.cpp
Go to the documentation of this file.
1 // File: unicharmap.cpp
3 // Description: Unicode character/ligature to integer id class.
4 // Author: Thomas Kielbus
5 // Created: Wed Jun 28 17:05:01 PDT 2006
6 //
7 // (C) Copyright 2006, 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 <cassert>
21 #include "unichar.h"
22 #include "host.h"
23 #include "unicharmap.h"
24 
26 nodes(nullptr) {
27 }
28 
30  delete[] nodes;
31 }
32 
33 // Search the given unichar representation in the tree, using length characters
34 // from it maximum. Each character in the string is interpreted as an index in
35 // an array of nodes.
36 UNICHAR_ID UNICHARMAP::unichar_to_id(const char* const unichar_repr,
37  int length) const {
38  UNICHARMAP_NODE* current_nodes = nodes;
39 
40  assert(*unichar_repr != '\0');
41  assert(length > 0 && length <= UNICHAR_LEN);
42 
43  int index = 0;
44  if (index >= length || unichar_repr[index] == '\0') return INVALID_UNICHAR_ID;
45  do {
46  if (index + 1 >= length || unichar_repr[index + 1] == '\0')
47  return current_nodes[static_cast<unsigned char>(unichar_repr[index])].id;
48  current_nodes =
49  current_nodes[static_cast<unsigned char>(unichar_repr[index])].children;
50  ++index;
51  } while (true);
52 }
53 
54 // Search the given unichar representation in the tree, creating the possibly
55 // missing nodes. Once the right place has been found, insert the given id and
56 // update the inserted flag to keep track of the insert. Each character in the
57 // string is interpreted as an index in an array of nodes.
58 void UNICHARMAP::insert(const char* const unichar_repr, UNICHAR_ID id) {
59  const char* current_char = unichar_repr;
60  if (*current_char == '\0') return;
61  UNICHARMAP_NODE** current_nodes_pointer = &nodes;
62  do {
63  if (*current_nodes_pointer == nullptr)
64  *current_nodes_pointer = new UNICHARMAP_NODE[256];
65  if (current_char[1] == '\0') {
66  (*current_nodes_pointer)
67  [static_cast<unsigned char>(*current_char)].id = id;
68  return;
69  }
70  current_nodes_pointer =
71  &((*current_nodes_pointer)
72  [static_cast<unsigned char>(*current_char)].children);
73  ++current_char;
74  } while (true);
75 }
76 
77 // Search the given unichar representation in the tree, using length characters
78 // from it maximum. Each character in the string is interpreted as an index in
79 // an array of nodes. Stop once the tree does not have anymore nodes or once we
80 // found the right unichar_repr.
81 bool UNICHARMAP::contains(const char* const unichar_repr,
82  int length) const {
83  if (unichar_repr == nullptr || *unichar_repr == '\0') return false;
84  if (length <= 0 || length > UNICHAR_LEN) return false;
85  int index = 0;
86  if (unichar_repr[index] == '\0') return false;
87  UNICHARMAP_NODE* current_nodes = nodes;
88 
89  while (current_nodes != nullptr && index + 1 < length &&
90  unichar_repr[index + 1] != '\0') {
91  current_nodes =
92  current_nodes[static_cast<unsigned char>(unichar_repr[index])].children;
93  ++index;
94  }
95  return current_nodes != nullptr &&
96  (index + 1 >= length || unichar_repr[index + 1] == '\0') &&
97  current_nodes[static_cast<unsigned char>(unichar_repr[index])].id >= 0;
98 }
99 
100 // Return the minimum number of characters that must be used from this string
101 // to obtain a match in the UNICHARMAP.
102 int UNICHARMAP::minmatch(const char* const unichar_repr) const {
103  const char* current_char = unichar_repr;
104  if (*current_char == '\0') return 0;
105  UNICHARMAP_NODE* current_nodes = nodes;
106 
107  while (current_nodes != nullptr && *current_char != '\0') {
108  if (current_nodes[static_cast<unsigned char>(*current_char)].id >= 0)
109  return current_char + 1 - unichar_repr;
110  current_nodes =
111  current_nodes[static_cast<unsigned char>(*current_char)].children;
112  ++current_char;
113  }
114  return 0;
115 }
116 
118  delete[] nodes;
119  nodes = nullptr;
120 }
121 
122 UNICHARMAP::UNICHARMAP_NODE::UNICHARMAP_NODE() :
123 children(nullptr),
124 id(-1) {
125 }
126 
127 // Recursively delete the children
128 UNICHARMAP::UNICHARMAP_NODE::~UNICHARMAP_NODE() {
129  delete[] children;
130 }
int UNICHAR_ID
Definition: unichar.h:35
UNICHAR_ID unichar_to_id(const char *const unichar_repr, int length) const
Definition: unicharmap.cpp:36
bool contains(const char *const unichar_repr, int length) const
Definition: unicharmap.cpp:81
#define UNICHAR_LEN
Definition: unichar.h:31
void clear()
Definition: unicharmap.cpp:117
void insert(const char *const unichar_repr, UNICHAR_ID id)
Definition: unicharmap.cpp:58
int minmatch(const char *const unichar_repr) const
Definition: unicharmap.cpp:102