Performs a beam search in the specified search using the specified language model; returns an alternate list of possible words as a result.
| 95 | // Performs a beam search in the specified search using the specified |
| 96 | // language model; returns an alternate list of possible words as a result. |
| 97 | WordAltList * BeamSearch::Search(SearchObject *srch_obj, LangModel *lang_mod) { |
| 98 | // verifications |
| 99 | if (!lang_mod) |
| 100 | lang_mod = cntxt_->LangMod(); |
| 101 | if (!lang_mod) { |
| 102 | fprintf(stderr, "Cube ERROR (BeamSearch::Search): could not construct " |
| 103 | "LangModel\n"); |
| 104 | return NULL; |
| 105 | } |
| 106 | |
| 107 | // free existing state |
| 108 | Cleanup(); |
| 109 | |
| 110 | // get seg pt count |
| 111 | seg_pt_cnt_ = srch_obj->SegPtCnt(); |
| 112 | if (seg_pt_cnt_ < 0) { |
| 113 | return NULL; |
| 114 | } |
| 115 | col_cnt_ = seg_pt_cnt_ + 1; |
| 116 | |
| 117 | // disregard suspicious cases |
| 118 | if (seg_pt_cnt_ > 128) { |
| 119 | fprintf(stderr, "Cube ERROR (BeamSearch::Search): segment point count is " |
| 120 | "suspiciously high; bailing out\n"); |
| 121 | return NULL; |
| 122 | } |
| 123 | |
| 124 | // alloc memory for columns |
| 125 | col_ = new SearchColumn *[col_cnt_]; |
| 126 | memset(col_, 0, col_cnt_ * sizeof(*col_)); |
| 127 | |
| 128 | // for all possible segments |
| 129 | for (int end_seg = 1; end_seg <= (seg_pt_cnt_ + 1); end_seg++) { |
| 130 | // create a search column |
| 131 | col_[end_seg - 1] = new SearchColumn(end_seg - 1, |
| 132 | cntxt_->Params()->BeamWidth()); |
| 133 | |
| 134 | // for all possible start segments |
| 135 | int init_seg = MAX(0, end_seg - cntxt_->Params()->MaxSegPerChar()); |
| 136 | for (int strt_seg = init_seg; strt_seg < end_seg; strt_seg++) { |
| 137 | int parent_nodes_cnt; |
| 138 | SearchNode **parent_nodes; |
| 139 | |
| 140 | // for the root segment, we do not have a parent |
| 141 | if (strt_seg == 0) { |
| 142 | parent_nodes_cnt = 1; |
| 143 | parent_nodes = NULL; |
| 144 | } else { |
| 145 | // for all the existing nodes in the starting column |
| 146 | parent_nodes_cnt = col_[strt_seg - 1]->NodeCount(); |
| 147 | parent_nodes = col_[strt_seg - 1]->Nodes(); |
| 148 | } |
| 149 | |
| 150 | // run the shape recognizer |
| 151 | CharAltList *char_alt_list = srch_obj->RecognizeSegment(strt_seg - 1, |
| 152 | end_seg - 1); |
| 153 | // for all the possible parents |
| 154 | for (int parent_idx = 0; parent_idx < parent_nodes_cnt; parent_idx++) { |
no test coverage detected