diff --git a/native_client/ctcdecode/ctc_beam_search_decoder.cpp b/native_client/ctcdecode/ctc_beam_search_decoder.cpp index f65888c4..c28dacd2 100644 --- a/native_client/ctcdecode/ctc_beam_search_decoder.cpp +++ b/native_client/ctcdecode/ctc_beam_search_decoder.cpp @@ -232,7 +232,7 @@ DecoderState::decode(size_t num_results) const for (PathTrie* prefix : prefixes_copy) { Output output; - output.tokens = prefix->get_path_vec(); + prefix->get_path_vec(output.tokens); output.timesteps = prefix->timesteps; output.confidence = scores[prefix]; outputs.push_back(output); diff --git a/native_client/ctcdecode/path_trie.cpp b/native_client/ctcdecode/path_trie.cpp index 50aca873..f4c77320 100644 --- a/native_client/ctcdecode/path_trie.cpp +++ b/native_client/ctcdecode/path_trie.cpp @@ -99,15 +99,13 @@ PathTrie* PathTrie::get_path_trie(unsigned int new_char, float cur_log_prob_c, b } } -std::vector PathTrie::get_path_vec() { - if (parent == nullptr) { - return std::vector{}; +void PathTrie::get_path_vec(std::vector& output) { + // Recursive call: recurse back until stop condition, then append data in + // correct order as we walk back down the stack in the lines below. + if (parent != nullptr) { + parent->get_path_vec(output); + output.push_back(character); } - std::vector output_tokens=parent->get_path_vec(); - if (character != ROOT_) { - output_tokens.push_back(character); - } - return output_tokens; } PathTrie* PathTrie::get_prev_grapheme(std::vector& output, diff --git a/native_client/ctcdecode/path_trie.h b/native_client/ctcdecode/path_trie.h index 0e832f15..a3ef7bdb 100644 --- a/native_client/ctcdecode/path_trie.h +++ b/native_client/ctcdecode/path_trie.h @@ -24,7 +24,7 @@ public: PathTrie* get_path_trie(unsigned int new_char, float log_prob_c, bool reset = true); // get the prefix data in correct time order from root to current node - std::vector get_path_vec(); + void get_path_vec(std::vector& output); // get the prefix data in correct time order from beginning of last grapheme to current node PathTrie* get_prev_grapheme(std::vector& output,