190 std::deque<std::pair<int,int>> best_choices;
191 ExtractBestPaths(&best_nodes, &second_nodes);
193 DebugPath(unicharset, best_nodes);
194 ExtractPathAsUnicharIds(second_nodes, &unichar_ids, &certs, &ratings,
196 tprintf(
"\nSecond choice path:\n");
197 DebugUnicharPath(unicharset, second_nodes, unichar_ids, certs, ratings,
204 if (lstm_choice_mode == 2) {
205 ExtractPathAsUnicharIds(best_nodes, &unichar_ids, &certs, &ratings,
206 &xcoords, &best_choices);
207 if (best_choices.size() > 0) {
208 current_char = best_choices.front().first;
209 timestepEnd = best_choices.front().second;
210 best_choices.pop_front();
213 ExtractPathAsUnicharIds(best_nodes, &unichar_ids, &certs, &ratings,
216 int num_ids = unichar_ids.
size();
218 DebugUnicharPath(unicharset, best_nodes, unichar_ids, certs, ratings,
223 float prev_space_cert = 0.0f;
224 for (
int word_start = 0; word_start < num_ids; word_start = word_end) {
225 for (word_end = word_start + 1; word_end < num_ids; ++word_end) {
230 int index = xcoords[word_end];
231 if (best_nodes[index]->start_of_word)
break;
237 float space_cert = 0.0f;
238 if (word_end < num_ids && unichar_ids[word_end] ==
UNICHAR_SPACE)
239 space_cert = certs[word_end];
241 word_start > 0 && unichar_ids[word_start - 1] ==
UNICHAR_SPACE;
243 WERD_RES* word_res = InitializeWord(
244 leading_space, line_box, word_start, word_end,
245 std::min(space_cert, prev_space_cert), unicharset, xcoords, scale_factor);
246 if (lstm_choice_mode == 1) {
247 for (
size_t i = timestepEnd; i < xcoords[word_end]; i++) {
250 timestepEnd = xcoords[word_end];
251 }
else if (lstm_choice_mode == 2) {
253 std::vector<std::pair<const char*, float>> choice_pairs;
254 for (
size_t i = timestepEnd; i < xcoords[word_end]; i++) {
255 for (std::pair<const char*, float> choice :
timesteps[i]) {
256 if (std::strcmp(choice.first,
"") != 0) {
257 sum += choice.second;
258 choice_pairs.push_back(choice);
261 if ((best_choices.size() > 0 && i == best_choices.front().second - 1)
262 || i == xcoords[word_end]-1) {
263 std::map<const char*, float> summed_propabilities;
264 for (
auto it = choice_pairs.begin(); it != choice_pairs.end(); ++it) {
265 summed_propabilities[it->first] += it->second;
267 std::vector<std::pair<const char*, float>> accumulated_timestep;
268 accumulated_timestep.push_back(std::pair<const char*,float>
270 (current_char), 2.0));
272 for (
auto it = summed_propabilities.begin();
273 it != summed_propabilities.end(); ++it) {
277 while (accumulated_timestep.size() > pos
278 && accumulated_timestep[pos].second > it->second) {
281 accumulated_timestep.insert(accumulated_timestep.begin() + pos,
282 std::pair<const char*,float>(it->first,
285 if (best_choices.size() > 0) {
286 current_char = best_choices.front().first;
287 best_choices.pop_front();
289 choice_pairs.clear();
290 word_res->
timesteps.push_back(accumulated_timestep);
294 timestepEnd = xcoords[word_end];
296 for (
int i = word_start; i < word_end; ++i) {
297 BLOB_CHOICE_LIST* choices =
new BLOB_CHOICE_LIST;
298 BLOB_CHOICE_IT bc_it(choices);
300 unichar_ids[i], ratings[i], certs[i], -1, 1.0f,
302 int col = i - word_start;
304 bc_it.add_after_then_move(choice);
307 int index = xcoords[word_end - 1];
310 prev_space_cert = space_cert;
311 if (word_end < num_ids && unichar_ids[word_end] ==
UNICHAR_SPACE)
void FakeWordFromRatings(PermuterType permuter)
bool IsSpaceDelimited(UNICHAR_ID unichar_id) const
void set_matrix_cell(int col, int row)
std::vector< std::vector< std::pair< const char *, float > > > timesteps
DLLSYM void tprintf(const char *format,...)
void put(ICOORD pos, const T &thing)
const char * id_to_unichar_ext(UNICHAR_ID id) const
std::vector< std::vector< std::pair< const char *, float > > > timesteps