tesseract  4.0.0-1-g2a2b
matrix.cpp
Go to the documentation of this file.
1 /* -*-C-*-
2  ********************************************************************************
3  *
4  * File: matrix.cpp (Formerly matrix.c)
5  * Description: Ratings matrix code. (Used by associator)
6  * Author: Mark Seaman, OCR Technology
7  * Created: Wed May 16 13:18:47 1990
8  * Modified: Wed Mar 20 09:44:47 1991 (Mark Seaman) marks@hpgrlt
9  * Language: C
10  * Package: N/A
11  * Status: Experimental (Do Not Distribute)
12  *
13  * (c) Copyright 1990, Hewlett-Packard Company.
14  ** Licensed under the Apache License, Version 2.0 (the "License");
15  ** you may not use this file except in compliance with the License.
16  ** You may obtain a copy of the License at
17  ** http://www.apache.org/licenses/LICENSE-2.0
18  ** Unless required by applicable law or agreed to in writing, software
19  ** distributed under the License is distributed on an "AS IS" BASIS,
20  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  ** See the License for the specific language governing permissions and
22  ** limitations under the License.
23  *
24  *********************************************************************************/
25 /*----------------------------------------------------------------------
26  I n c l u d e s
27 ----------------------------------------------------------------------*/
28 #include "matrix.h"
29 
30 #include "callcpp.h"
31 #include "ratngs.h"
32 #include "tprintf.h"
33 #include "unicharset.h"
34 
35 // Destructor.
36 // It is defined here, so the compiler can create a single vtable
37 // instead of weak vtables in every compilation unit.
38 MATRIX::~MATRIX() = default;
39 
40 // Returns true if there are any real classification results.
41 bool MATRIX::Classified(int col, int row, int wildcard_id) const {
42  if (get(col, row) == NOT_CLASSIFIED) return false;
43  BLOB_CHOICE_IT b_it(get(col, row));
44  for (b_it.mark_cycle_pt(); !b_it.cycled_list(); b_it.forward()) {
45  BLOB_CHOICE* choice = b_it.data();
46  if (choice->IsClassified())
47  return true;
48  }
49  return false;
50 }
51 
52 // Expands the existing matrix in-place to make the band wider, without
53 // losing any existing data.
54 void MATRIX::IncreaseBandSize(int bandwidth) {
56 }
57 
58 // Returns a bigger MATRIX with a new column and row in the matrix in order
59 // to split the blob at the given (ind,ind) diagonal location.
60 // Entries are relocated to the new MATRIX using the transformation defined
61 // by MATRIX_COORD::MapForSplit.
62 // Transfers the pointer data to the new MATRIX and deletes *this.
64  int dim = dimension();
65  int band_width = bandwidth();
66  // Check to see if bandwidth needs expanding.
67  for (int col = ind; col >= 0 && col > ind - band_width; --col) {
68  if (array_[col * band_width + band_width - 1] != empty_) {
69  ++band_width;
70  break;
71  }
72  }
73  MATRIX* result = new MATRIX(dim + 1, band_width);
74 
75  for (int col = 0; col < dim; ++col) {
76  for (int row = col; row < dim && row < col + bandwidth(); ++row) {
77  MATRIX_COORD coord(col, row);
78  coord.MapForSplit(ind);
79  BLOB_CHOICE_LIST* choices = get(col, row);
80  if (choices != nullptr) {
81  // Correct matrix location on each choice.
82  BLOB_CHOICE_IT bc_it(choices);
83  for (bc_it.mark_cycle_pt(); !bc_it.cycled_list(); bc_it.forward()) {
84  BLOB_CHOICE* choice = bc_it.data();
85  choice->set_matrix_cell(coord.col, coord.row);
86  }
87  ASSERT_HOST(coord.Valid(*result));
88  result->put(coord.col, coord.row, choices);
89  }
90  }
91  }
92  delete this;
93  return result;
94 }
95 
96 // Makes and returns a deep copy of *this, including all the BLOB_CHOICEs
97 // on the lists, but not any LanguageModelState that may be attached to the
98 // BLOB_CHOICEs.
100  int dim = dimension();
101  int band_width = bandwidth();
102  MATRIX* result = new MATRIX(dim, band_width);
103  for (int col = 0; col < dim; ++col) {
104  for (int row = col; row < dim && row < col + band_width; ++row) {
105  BLOB_CHOICE_LIST* choices = get(col, row);
106  if (choices != nullptr) {
107  BLOB_CHOICE_LIST* copy_choices = new BLOB_CHOICE_LIST;
108  copy_choices->deep_copy(choices, &BLOB_CHOICE::deep_copy);
109  result->put(col, row, copy_choices);
110  }
111  }
112  }
113  return result;
114 }
115 
116 // Print the best guesses out of the match rating matrix.
117 void MATRIX::print(const UNICHARSET &unicharset) const {
118  tprintf("Ratings Matrix (top 3 choices)\n");
119  int dim = dimension();
120  int band_width = bandwidth();
121  int row, col;
122  for (col = 0; col < dim; ++col) {
123  for (row = col; row < dim && row < col + band_width; ++row) {
124  BLOB_CHOICE_LIST *rating = this->get(col, row);
125  if (rating == NOT_CLASSIFIED) continue;
126  BLOB_CHOICE_IT b_it(rating);
127  tprintf("col=%d row=%d ", col, row);
128  for (b_it.mark_cycle_pt(); !b_it.cycled_list(); b_it.forward()) {
129  tprintf("%s rat=%g cert=%g " ,
130  unicharset.id_to_unichar(b_it.data()->unichar_id()),
131  b_it.data()->rating(), b_it.data()->certainty());
132  }
133  tprintf("\n");
134  }
135  tprintf("\n");
136  }
137  tprintf("\n");
138  for (col = 0; col < dim; ++col) tprintf("\t%d", col);
139  tprintf("\n");
140  for (row = 0; row < dim; ++row) {
141  for (col = 0; col <= row; ++col) {
142  if (col == 0) tprintf("%d\t", row);
143  if (row >= col + band_width) {
144  tprintf(" \t");
145  continue;
146  }
147  BLOB_CHOICE_LIST *rating = this->get(col, row);
148  if (rating != NOT_CLASSIFIED) {
149  BLOB_CHOICE_IT b_it(rating);
150  int counter = 0;
151  for (b_it.mark_cycle_pt(); !b_it.cycled_list(); b_it.forward()) {
152  tprintf("%s ",
153  unicharset.id_to_unichar(b_it.data()->unichar_id()));
154  ++counter;
155  if (counter == 3) break;
156  }
157  tprintf("\t");
158  } else {
159  tprintf(" \t");
160  }
161  }
162  tprintf("\n");
163  }
164 }
void print(const UNICHARSET &unicharset) const
Definition: matrix.cpp:117
void MapForSplit(int ind)
Definition: matrix.h:626
MATRIX * ConsumeAndMakeBigger(int ind)
Definition: matrix.cpp:63
void set_matrix_cell(int col, int row)
Definition: ratngs.h:157
bool Valid(const MATRIX &m) const
Definition: matrix.h:615
static BLOB_CHOICE * deep_copy(const BLOB_CHOICE *src)
Definition: ratngs.h:170
#define NOT_CLASSIFIED
Definition: matrix.h:44
void IncreaseBandSize(int bandwidth)
Definition: matrix.cpp:54
virtual ~MATRIX()
BLOB_CHOICE_LIST * * array_
Definition: matrix.h:505
MATRIX(int dimension, int bandwidth)
Definition: matrix.h:577
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
void put(ICOORD pos, const T &thing)
Definition: matrix.h:220
bool IsClassified() const
Definition: ratngs.h:139
MATRIX * DeepCopy() const
Definition: matrix.cpp:99
bool Classified(int col, int row, int wildcard_id) const
Definition: matrix.cpp:41
const char * id_to_unichar(UNICHAR_ID id) const
Definition: unicharset.cpp:290
void ResizeWithCopy(int size1, int size2)
Definition: matrix.h:112
Definition: matrix.h:575
#define ASSERT_HOST(x)
Definition: errcode.h:84