Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions swig/ctc_beam_search_decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ std::vector<std::pair<double, std::vector<int>>> ctc_beam_search_decoder(
float hotwords_score = 0.0;
std::vector<std::string> ngram;
PathTrie *prefix_to_score = nullptr;
if (hotwords_scorer != nullptr && !hotwords_scorer->hotwords_dict.empty()) {
if (hotwords_scorer != nullptr && !hotwords_scorer->hotwords_dict.empty() &&
(c == space_id || hotwords_scorer->is_character_based)) {
if (hotwords_scorer->is_character_based) {
prefix_to_score = prefix_new;
} else {
Expand All @@ -151,25 +152,23 @@ std::vector<std::pair<double, std::vector<int>>> ctc_beam_search_decoder(

// language model scoring
float ngram_score = 0.0;
if (ext_scorer != nullptr ) {
if (ext_scorer != nullptr && (c == space_id || ext_scorer->is_character_based())) {
if (hotwords_scorer != nullptr && !hotwords_scorer->hotwords_dict.empty() &&
!(hotwords_scorer->is_character_based ^ ext_scorer->is_character_based()) &&
hotwords_scorer->window_length >= ext_scorer->get_max_order()) {
std::vector<std::string>::const_iterator first = ngram.end() - ext_scorer->get_max_order();
std::vector<std::string>::const_iterator last = ngram.end();
std::vector<std::string> slice_ngram(first, last);
ngram_score = ext_scorer->get_log_cond_prob(slice_ngram) * ext_scorer->alpha + ext_scorer->beta;
} else {
if (c == space_id || ext_scorer->is_character_based()) {
// skip scoring the space
if (ext_scorer->is_character_based()) {
prefix_to_score = prefix_new;
} else {
prefix_to_score = prefix;
}
ngram = ext_scorer->make_ngram(prefix_to_score);
ngram_score = ext_scorer->get_log_cond_prob(ngram) * ext_scorer->alpha + ext_scorer->beta;
}
else {
if (ext_scorer->is_character_based()) {
prefix_to_score = prefix_new;
} else {
prefix_to_score = prefix;
}
ngram = ext_scorer->make_ngram(prefix_to_score);
ngram_score = ext_scorer->get_log_cond_prob(ngram) * ext_scorer->alpha + ext_scorer->beta;
}
}
log_p += ngram_score;
Expand Down
7 changes: 5 additions & 2 deletions swig/hotwords.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,11 @@ float HotWordsScorer::get_hotwords_score(const std::vector<std::string>& words,
// word = std::accumulate(words.begin() + offset, words.end() - index, std::string{});
word = std::accumulate(words.begin() + offset + index, words.end(), std::string{});
} else {
// word in fixed window, traverse each word in words.
word = words[index];
// word in fixed window, traverse each word in words, skip <s> token.
if (index + offset >= words_size) {
break;
}
word = words[index + offset];
}
iter = this->hotwords_dict.find(word);
if (iter != this->hotwords_dict.end()) {
Expand Down