You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
101 lines
3.3 KiB
101 lines
3.3 KiB
2 years ago
|
// Copyright (c) 2020 Mobvoi Inc (Binbin Zhang)
|
||
2 years ago
|
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
|
||
|
//
|
||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
// you may not use this file except in compliance with the License.
|
||
|
// You may obtain a copy of the License at
|
||
|
//
|
||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||
|
//
|
||
|
// Unless required by applicable law or agreed to in writing, software
|
||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
// See the License for the specific language governing permissions and
|
||
|
// limitations under the License.
|
||
|
|
||
2 years ago
|
// modified from
|
||
|
// https://github.com/wenet-e2e/wenet/blob/main/runtime/core/decoder/ctc_prefix_beam_search.cc
|
||
2 years ago
|
|
||
2 years ago
|
#pragma once
|
||
|
|
||
|
#include "decoder/ctc_beam_search_opt.h"
|
||
|
#include "decoder/ctc_prefix_beam_search_score.h"
|
||
|
#include "decoder/decoder_itf.h"
|
||
2 years ago
|
#include "fst/symbol-table.h"
|
||
|
|
||
2 years ago
|
namespace ppspeech {
|
||
2 years ago
|
class ContextGraph;
|
||
2 years ago
|
class CTCPrefixBeamSearch : public DecoderBase {
|
||
2 years ago
|
public:
|
||
2 years ago
|
CTCPrefixBeamSearch(const std::string& vocab_path,
|
||
2 years ago
|
const CTCBeamSearchOptions& opts);
|
||
2 years ago
|
~CTCPrefixBeamSearch() {}
|
||
|
|
||
2 years ago
|
SearchType Type() const { return SearchType::kPrefixBeamSearch; }
|
||
|
|
||
2 years ago
|
void InitDecoder() override;
|
||
2 years ago
|
|
||
2 years ago
|
void Reset() override;
|
||
2 years ago
|
|
||
|
void AdvanceDecode(
|
||
2 years ago
|
const std::shared_ptr<kaldi::DecodableInterface>& decodable) override;
|
||
2 years ago
|
|
||
2 years ago
|
std::string GetFinalBestPath() override;
|
||
|
std::string GetPartialResult() override;
|
||
2 years ago
|
|
||
2 years ago
|
void FinalizeSearch();
|
||
2 years ago
|
|
||
2 years ago
|
const std::shared_ptr<fst::SymbolTable> WordSymbolTable() const override {
|
||
2 years ago
|
return unit_table_;
|
||
|
}
|
||
2 years ago
|
|
||
|
const std::vector<std::vector<int>>& Inputs() const { return hypotheses_; }
|
||
|
const std::vector<std::vector<int>>& Outputs() const { return outputs_; }
|
||
|
const std::vector<float>& Likelihood() const { return likelihood_; }
|
||
2 years ago
|
const std::vector<float>& ViterbiLikelihood() const {
|
||
|
return viterbi_likelihood_;
|
||
|
}
|
||
2 years ago
|
const std::vector<std::vector<int>>& Times() const { return times_; }
|
||
|
|
||
2 years ago
|
protected:
|
||
|
std::string GetBestPath() override;
|
||
|
std::vector<std::pair<double, std::string>> GetNBestPath() override;
|
||
|
std::vector<std::pair<double, std::string>> GetNBestPath(int n) override;
|
||
|
|
||
2 years ago
|
private:
|
||
2 years ago
|
std::string GetBestPath(int index);
|
||
2 years ago
|
|
||
2 years ago
|
void AdvanceDecoding(
|
||
|
const std::vector<std::vector<kaldi::BaseFloat>>& logp);
|
||
2 years ago
|
|
||
2 years ago
|
void UpdateOutputs(const std::pair<std::vector<int>, PrefixScore>& prefix);
|
||
|
void UpdateHypotheses(
|
||
|
const std::vector<std::pair<std::vector<int>, PrefixScore>>& prefix);
|
||
|
void UpdateFinalContext();
|
||
|
|
||
|
|
||
|
private:
|
||
2 years ago
|
CTCBeamSearchOptions opts_;
|
||
2 years ago
|
std::shared_ptr<fst::SymbolTable> unit_table_{nullptr};
|
||
2 years ago
|
|
||
|
std::unordered_map<std::vector<int>, PrefixScore, PrefixScoreHash>
|
||
|
cur_hyps_;
|
||
|
|
||
|
// n-best list and corresponding likelihood, in sorted order
|
||
|
std::vector<std::vector<int>> hypotheses_;
|
||
|
std::vector<float> likelihood_;
|
||
|
|
||
|
std::vector<std::vector<int>> times_;
|
||
|
std::vector<float> viterbi_likelihood_;
|
||
|
|
||
|
// Outputs contain the hypotheses_ and tags lik: <context> and </context>
|
||
|
std::vector<std::vector<int>> outputs_;
|
||
|
|
||
2 years ago
|
std::shared_ptr<ContextGraph> context_graph_{nullptr};
|
||
2 years ago
|
|
||
2 years ago
|
DISALLOW_COPY_AND_ASSIGN(CTCPrefixBeamSearch);
|
||
|
};
|
||
|
|
||
2 years ago
|
|
||
2 years ago
|
} // namespace ppspeech
|