wordrec/heuristic.cpp

Go to the documentation of this file.
00001 
00020 /*----------------------------------------------------------------------
00021               I n c l u d e s
00022 ----------------------------------------------------------------------*/
00023 #include "heuristic.h"
00024 #include "baseline.h"
00025 #include "metrics.h"
00026 #include "freelist.h"
00027 #include <math.h>
00028 
00029 /*----------------------------------------------------------------------
00030               M a c r o s
00031 ----------------------------------------------------------------------*/
00032 #define MAX_SQUAT       2.0      
00033 #define BAD_RATING   1000.0      
00035 /*----------------------------------------------------------------------
00036               F u n c t i o n s
00037 ----------------------------------------------------------------------*/
00038 /* ================== */
00043 FLOAT32 prioritize_state(CHUNKS_RECORD *chunks_record,
00044                          SEARCH_RECORD *the_search,
00045                          STATE *old_state) {
00046   FLOAT32 width_pri;
00047   FLOAT32 match_pri;
00048 
00049   match_pri = rating_priority (chunks_record, the_search->this_state,
00050     old_state, the_search->num_joints);
00051 
00052   width_pri = width_priority (chunks_record, the_search->this_state,
00053     the_search->num_joints) * 1000.0;
00054 
00055   record_priorities(the_search, old_state, match_pri, width_pri); 
00056 
00057   return (width_pri + match_pri);
00058 }
00059 
00060 
00061 /* ================== */
00069 FLOAT32 rating_priority(CHUNKS_RECORD *chunks_record,
00070                         STATE *state,
00071                         STATE *old_state,
00072                         int num_joints) {
00073   PIECES_STATE blob_chunks;
00074   INT16 x;
00075   INT16 y;
00076   CHOICES this_choice;
00077   INT16 first_chunk = 0;
00078   INT16 last_chunk;
00079   INT16 ratings = 0;
00080   INT16 weights = 0;
00081 
00082   bin_to_pieces(state, num_joints, blob_chunks); 
00083 
00084   for (x = 0; blob_chunks[x]; x++) {
00085                                  // Iterate each blob
00086     last_chunk = first_chunk + blob_chunks[x] - 1;
00087 
00088     this_choice = matrix_get (chunks_record->ratings,
00089       first_chunk, last_chunk);
00090 
00091     if (this_choice == NIL)
00092       return (BAD_RATING);
00093 
00094     if (this_choice != NOT_CLASSIFIED) {
00095 
00096       ratings += (INT16) best_probability (this_choice);
00097       for (y = first_chunk; y <= last_chunk; y++) {
00098         weights += (INT16) (chunks_record->weights[y]);
00099       }
00100     }
00101     first_chunk += blob_chunks[x];
00102   }
00103   if (weights <= 0)
00104     weights = 1;
00105   return ((FLOAT32) ratings / weights);
00106 }
00107 
00108 
00109 /* ================== */
00114 WIDTH_RECORD *state_char_widths(WIDTH_RECORD *chunk_widths,
00115                                 STATE *state,
00116                                 int num_joints,
00117                                 SEARCH_STATE *search_state) {
00118   WIDTH_RECORD *width_record;
00119   int num_blobs;
00120   int x;
00121   int y;
00122   int i;
00123   SEARCH_STATE new_chunks;
00124 
00125   new_chunks = bin_to_chunks (state, num_joints);
00126 
00127   num_blobs = new_chunks[0] + 1;
00128   width_record = (WIDTH_RECORD *) memalloc (sizeof (int) * num_blobs * 2);
00129   width_record->num_chars = num_blobs;
00130 
00131   x = 0;
00132   for (i = 1; i <= new_chunks[0] + 1; i++) {
00133     if (i > new_chunks[0])
00134       y = num_joints;
00135     else
00136       y = x + new_chunks[i];
00137 
00138     width_record->widths[2 * i - 2] = chunks_width (chunk_widths, x, y);
00139 
00140     if (i <= new_chunks[0])
00141       width_record->widths[2 * i - 1] = chunks_gap (chunk_widths, y);
00142 
00143     x = y + 1;
00144   }
00145 
00146   *search_state = new_chunks;
00147   return (width_record);
00148 }
00149 
00150 
00151 /* ================== */
00156 FLOAT32 width_priority(CHUNKS_RECORD *chunks_record,
00157                        STATE *state,
00158                        int num_joints) {
00159   SEARCH_STATE new_chunks;
00160   FLOAT32 result = 0.0;
00161   WIDTH_RECORD *width_record;
00162   FLOAT32 squat;
00163   int x;
00164 
00165   width_record = state_char_widths (chunks_record->chunk_widths,
00166     state, num_joints, &new_chunks);
00167   for (x = 0; x < width_record->num_chars; x++) {
00168 
00169     squat = width_record->widths[2 * x];
00170     if (!baseline_enable) {
00171       squat /= chunks_record->row->lineheight;
00172     }
00173     else {
00174       squat /= BASELINE_SCALE;
00175     }
00176 
00177     if (squat > MAX_SQUAT)
00178       result += squat - MAX_SQUAT;
00179 
00180   }
00181 
00182   memfree(new_chunks); 
00183   free_widths(width_record); 
00184 
00185   return (result);
00186 }

Generated on Wed Feb 28 19:49:13 2007 for Tesseract by  doxygen 1.5.1