/********************************************************************** * save_best_state * * Save this state away to be compared later. **********************************************************************/ void save_best_state(CHUNKS_RECORD *chunks_record) { STATE state; SEARCH_STATE chunk_groups; int num_joints; if (save_priorities) { num_joints = chunks_record->ratings->dimension() - 1; state.part1 = 0xffffffff; state.part2 = 0xffffffff; chunk_groups = bin_to_chunks (&state, num_joints); display_segmentation (chunks_record->chunks, chunk_groups); memfree(chunk_groups); cprintf ("Enter the correct segmentation > "); fflush(stdout); state.part1 = 0; scanf ("%x", &state.part2); chunk_groups = bin_to_chunks (&state, num_joints); display_segmentation (chunks_record->chunks, chunk_groups); memfree(chunk_groups); window_wait(segm_window); /* == 'n') */ if (known_best_state) free_state(known_best_state); known_best_state = new_state (&state); } }
BLOB_CHOICE_LIST *Wordrec::classify_piece(TBLOB *pieces, const DENORM& denorm, SEAMS seams, inT16 start, inT16 end, BlamerBundle *blamer_bundle) { BLOB_CHOICE_LIST *choices; TBLOB *blob; inT16 x; join_pieces(pieces, seams, start, end); for (blob = pieces, x = 0; x < start; x++) { blob = blob->next; } choices = classify_blob(blob, denorm, "pieces:", White, blamer_bundle); break_pieces(blob, seams, start, end); #ifndef GRAPHICS_DISABLED if (wordrec_display_segmentations > 2) { STATE current_state; SEARCH_STATE chunk_groups; set_n_ones (¤t_state, array_count(seams)); chunk_groups = bin_to_chunks(¤t_state, array_count(seams)); display_segmentation(pieces, chunk_groups); window_wait(segm_window); memfree(chunk_groups); } #endif return (choices); }
BLOB_CHOICE_LIST *Wordrec::classify_piece(TBLOB *pieces, SEAMS seams, inT16 start, inT16 end) { STATE current_state; BLOB_CHOICE_LIST *choices; TBLOB *pblob; TBLOB *blob; TBLOB *nblob; inT16 x; SEARCH_STATE chunk_groups; set_n_ones (¤t_state, array_count (seams)); join_pieces(pieces, seams, start, end); for (blob = pieces, pblob = NULL, x = 0; x < start; x++) { pblob = blob; blob = blob->next; } for (nblob = blob->next; x < end; x++) nblob = nblob->next; choices = classify_blob (pblob, blob, nblob, NULL, "pieces:", White); break_pieces(blob, seams, start, end); #ifndef GRAPHICS_DISABLED if (wordrec_display_segmentations > 2) { chunk_groups = bin_to_chunks (¤t_state, array_count (seams)); display_segmentation(pieces, chunk_groups); window_wait(segm_window); memfree(chunk_groups); } #endif return (choices); }
/** * @name evaluate_state * * Evaluate the segmentation that is represented by this state in the * best first search. Add this state to the "states_seen" list. */ inT16 Wordrec::evaluate_state(CHUNKS_RECORD *chunks_record, SEARCH_RECORD *the_search, DANGERR *fixpt) { BLOB_CHOICE_LIST_VECTOR *char_choices; SEARCH_STATE chunk_groups; float rating_limit = the_search->best_choice->rating(); inT16 keep_going = TRUE; PIECES_STATE widths; the_search->num_states++; chunk_groups = bin_to_chunks(the_search->this_state, the_search->num_joints); bin_to_pieces (the_search->this_state, the_search->num_joints, widths); getDict().LogNewSegmentation(widths); char_choices = evaluate_chunks(chunks_record, chunk_groups); wordseg_rating_adjust_factor = -1.0f; if (char_choices != NULL && char_choices->length() > 0) { // Compute the segmentation cost and include the cost in word rating. // TODO(dsl): We should change the SEARCH_RECORD to store this cost // from state evaluation and avoid recomputing it here. prioritize_state(chunks_record, the_search); wordseg_rating_adjust_factor = the_search->segcost_bias; getDict().permute_characters(*char_choices, rating_limit, the_search->best_choice, the_search->raw_choice); bool replaced = false; if (getDict().AcceptableChoice(char_choices, the_search->best_choice, *(the_search->raw_choice), fixpt, ASSOCIATOR_CALLER, &replaced)) { keep_going = FALSE; } } wordseg_rating_adjust_factor = -1.0f; #ifndef GRAPHICS_DISABLED if (wordrec_display_segmentations) { display_segmentation (chunks_record->chunks, chunk_groups); if (wordrec_display_segmentations > 1) window_wait(segm_window); } #endif if (rating_limit != the_search->best_choice->rating()) { the_search->before_best = the_search->num_states; the_search->best_state->part1 = the_search->this_state->part1; the_search->best_state->part2 = the_search->this_state->part2; replace_char_widths(chunks_record, chunk_groups); } else if (char_choices != NULL) fixpt->index = -1; if (char_choices != NULL) delete char_choices; memfree(chunk_groups); return (keep_going); }
/********************************************************************** * classify_piece * * Create a larger piece from a collection of smaller ones. Classify * it and return the results. Take the large piece apart to leave * the collection of small pieces un modified. **********************************************************************/ CHOICES classify_piece(TBLOB *pieces, SEAMS seams, INT16 start, INT16 end, INT32 fx, STATE *this_state, STATE *best_state, INT32 pass, INT32 blob_index) { STATE current_state; CHOICES choices; TBLOB *pblob; TBLOB *blob; TBLOB *nblob; INT16 x; SEARCH_STATE chunk_groups; set_n_ones (¤t_state, array_count (seams)); join_pieces(pieces, seams, start, end); for (blob = pieces, pblob = NULL, x = 0; x < start; x++) { pblob = blob; blob = blob->next; } for (nblob = blob->next; x < end; x++) nblob = nblob->next; choices = classify_blob (pblob, blob, nblob, NULL, fx, "pieces:", White, this_state, best_state, pass, blob_index); break_pieces(blob, seams, start, end); #ifndef GRAPHICS_DISABLED if (display_segmentations > 2) { chunk_groups = bin_to_chunks (¤t_state, array_count (seams)); display_segmentation(pieces, chunk_groups); window_wait(segm_window); memfree(chunk_groups); } #endif return (choices); }
/** * @name evaluate_state * * Evaluate the segmentation that is represented by this state in the * best first search. Add this state to the "states_seen" list. */ inT16 Wordrec::evaluate_state(CHUNKS_RECORD *chunks_record, SEARCH_RECORD *the_search, DANGERR *fixpt, BlamerBundle *blamer_bundle) { BLOB_CHOICE_LIST_VECTOR *char_choices; SEARCH_STATE chunk_groups; float rating_limit = the_search->best_choice->rating(); bool keep_going = true; PIECES_STATE widths; the_search->num_states++; chunk_groups = bin_to_chunks(the_search->this_state, the_search->num_joints); bin_to_pieces (the_search->this_state, the_search->num_joints, widths); if (wordrec_debug_level > 1) { log_state("Evaluating state", the_search->num_joints, the_search->this_state); } getDict().LogNewSegmentation(widths); char_choices = evaluate_chunks(chunks_record, chunk_groups, blamer_bundle); getDict().SetWordsegRatingAdjustFactor(-1.0f); bool updated_best_choice = false; if (char_choices != NULL && char_choices->length() > 0) { // Compute the segmentation cost and include the cost in word rating. // TODO(dsl): We should change the SEARCH_RECORD to store this cost // from state evaluation and avoid recomputing it here. prioritize_state(chunks_record, the_search); getDict().SetWordsegRatingAdjustFactor(the_search->segcost_bias); updated_best_choice = getDict().permute_characters(*char_choices, the_search->best_choice, the_search->raw_choice); bool replaced = false; if (updated_best_choice) { if (getDict().AcceptableChoice(char_choices, the_search->best_choice, NULL, ASSOCIATOR_CALLER, &replaced)) { keep_going = false; } CopyCharChoices(*char_choices, the_search->best_char_choices); } } getDict().SetWordsegRatingAdjustFactor(-1.0f); #ifndef GRAPHICS_DISABLED if (wordrec_display_segmentations) { display_segmentation (chunks_record->chunks, chunk_groups); if (wordrec_display_segmentations > 1) window_wait(segm_window); } #endif if (rating_limit != the_search->best_choice->rating()) { ASSERT_HOST(updated_best_choice); the_search->before_best = the_search->num_states; the_search->best_state->part1 = the_search->this_state->part1; the_search->best_state->part2 = the_search->this_state->part2; replace_char_widths(chunks_record, chunk_groups); } else { ASSERT_HOST(!updated_best_choice); if (char_choices != NULL) fixpt->clear(); } if (char_choices != NULL) delete char_choices; memfree(chunk_groups); return (keep_going); }