parent
642e0840b4
commit
2455d88997
@ -1,50 +1,66 @@
|
|||||||
|
// 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.
|
||||||
|
|
||||||
#include "decoder/ctc_tlg_decoder.h"
|
#include "decoder/ctc_tlg_decoder.h"
|
||||||
namespace ppspeech {
|
namespace ppspeech {
|
||||||
|
|
||||||
TLGDecoder::TLGDecoder(TLGDecoderOptions opts) {
|
TLGDecoder::TLGDecoder(TLGDecoderOptions opts) {
|
||||||
fst_.reset(fst::Fst<fst::StdArc>::Read(opts.fst_path));
|
fst_.reset(fst::Fst<fst::StdArc>::Read(opts.fst_path));
|
||||||
CHECK(fst_ != nullptr);
|
CHECK(fst_ != nullptr);
|
||||||
word_symbol_table_.reset(fst::SymbolTable::ReadText(opts.word_symbol_table));
|
word_symbol_table_.reset(
|
||||||
|
fst::SymbolTable::ReadText(opts.word_symbol_table));
|
||||||
decoder_.reset(new kaldi::LatticeFasterOnlineDecoder(*fst_, opts.opts));
|
decoder_.reset(new kaldi::LatticeFasterOnlineDecoder(*fst_, opts.opts));
|
||||||
decoder_->InitDecoding();
|
decoder_->InitDecoding();
|
||||||
|
frame_decoded_size_ = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TLGDecoder::InitDecoder() {
|
void TLGDecoder::InitDecoder() {
|
||||||
decoder_->InitDecoding();
|
decoder_->InitDecoding();
|
||||||
|
frame_decoded_size_ = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TLGDecoder::AdvanceDecode(const std::shared_ptr<kaldi::DecodableInterface>& decodable) {
|
void TLGDecoder::AdvanceDecode(
|
||||||
while (1) {
|
const std::shared_ptr<kaldi::DecodableInterface>& decodable) {
|
||||||
AdvanceDecoding(decodable.get());
|
while (!decodable->IsLastFrame(frame_decoded_size_)) {
|
||||||
if (decodable->IsLastFrame(num_frame_decoded_)) break;
|
LOG(INFO) << "num frame decode: " << frame_decoded_size_;
|
||||||
|
AdvanceDecoding(decodable.get());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TLGDecoder::AdvanceDecoding(kaldi::DecodableInterface* decodable) {
|
void TLGDecoder::AdvanceDecoding(kaldi::DecodableInterface* decodable) {
|
||||||
// skip blank frame?
|
decoder_->AdvanceDecoding(decodable, 1);
|
||||||
decoder_->AdvanceDecoding(decodable, 1);
|
frame_decoded_size_++;
|
||||||
num_frame_decoded_++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TLGDecoder::Reset() {
|
void TLGDecoder::Reset() {
|
||||||
decoder_->InitDecoding();
|
InitDecoder();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string TLGDecoder::GetFinalBestPath() {
|
std::string TLGDecoder::GetFinalBestPath() {
|
||||||
decoder_->FinalizeDecoding();
|
decoder_->FinalizeDecoding();
|
||||||
kaldi::Lattice lat;
|
kaldi::Lattice lat;
|
||||||
kaldi::LatticeWeight weight;
|
kaldi::LatticeWeight weight;
|
||||||
std::vector<int> alignment;
|
std::vector<int> alignment;
|
||||||
std::vector<int> words_id;
|
std::vector<int> words_id;
|
||||||
decoder_->GetBestPath(&lat, true);
|
decoder_->GetBestPath(&lat, true);
|
||||||
fst::GetLinearSymbolSequence(lat, &alignment, &words_id, &weight);
|
fst::GetLinearSymbolSequence(lat, &alignment, &words_id, &weight);
|
||||||
std::string words;
|
std::string words;
|
||||||
for (int32 idx = 0; idx < words_id.size(); ++idx) {
|
for (int32 idx = 0; idx < words_id.size(); ++idx) {
|
||||||
std::string word = word_symbol_table_->Find(words_id[idx]);
|
std::string word = word_symbol_table_->Find(words_id[idx]);
|
||||||
words += word;
|
words += word;
|
||||||
}
|
}
|
||||||
return words;
|
return words;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in new issue