/********************************************************************** * rating_priority * * Assign a segmentation priority based on the ratings of the blobs * (in that segmentation) that have been classified. The average * "goodness" (i.e. rating / weight) for each blob is used to indicate * the segmentation priority. This is the original. **********************************************************************/ FLOAT32 Wordrec::rating_priority(CHUNKS_RECORD *chunks_record, STATE *state, int num_joints) { BLOB_CHOICE_LIST *blob_choices; BLOB_CHOICE_IT blob_choice_it; inT16 first_chunk = 0; inT16 last_chunk; inT16 ratings = 0; inT16 weights = 0; PIECES_STATE blob_chunks; bin_to_pieces(state, num_joints, blob_chunks); for (int x = 0; blob_chunks[x]; x++) { last_chunk = first_chunk + blob_chunks[x]; blob_choices = chunks_record->ratings->get(first_chunk, last_chunk - 1); if (blob_choices != NOT_CLASSIFIED && blob_choices->length() > 0) { blob_choice_it.set_to_list(blob_choices); ratings += (inT16) blob_choice_it.data()->rating(); for (int y = first_chunk; y < last_chunk; y++) { weights += (inT16) (chunks_record->weights[y]); } } first_chunk = last_chunk; } if (weights <= 0) weights = 1; FLOAT32 rating_cost = static_cast<FLOAT32>(ratings) / static_cast<FLOAT32>(weights); if (segment_adjust_debug > 2) tprintf("rating_cost: r%f / w%f = %f\n", ratings, weights, rating_cost); return rating_cost; }
/** * @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); }
/** * @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); }