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.
129 lines
4.0 KiB
129 lines
4.0 KiB
3 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.
|
||
|
|
||
3 years ago
|
#pragma once
|
||
|
|
||
2 years ago
|
#include "base/common.h"
|
||
|
#include "decoder/decoder_itf.h"
|
||
3 years ago
|
#include "kaldi/decoder/lattice-faster-online-decoder.h"
|
||
3 years ago
|
#include "util/parse-options.h"
|
||
|
|
||
2 years ago
|
DECLARE_string(word_symbol_table);
|
||
2 years ago
|
DECLARE_string(graph_path);
|
||
2 years ago
|
DECLARE_int32(max_active);
|
||
|
DECLARE_double(beam);
|
||
|
DECLARE_double(lattice_beam);
|
||
2 years ago
|
DECLARE_int32(nbest);
|
||
2 years ago
|
|
||
3 years ago
|
namespace ppspeech {
|
||
|
|
||
|
struct TLGDecoderOptions {
|
||
2 years ago
|
kaldi::LatticeFasterDecoderConfig opts{};
|
||
3 years ago
|
// todo remove later, add into decode resource
|
||
2 years ago
|
std::string word_symbol_table;
|
||
|
std::string fst_path;
|
||
2 years ago
|
int nbest;
|
||
|
|
||
|
TLGDecoderOptions() : word_symbol_table(""), fst_path(""), nbest(10) {}
|
||
2 years ago
|
|
||
2 years ago
|
static TLGDecoderOptions InitFromFlags() {
|
||
2 years ago
|
TLGDecoderOptions decoder_opts;
|
||
|
decoder_opts.word_symbol_table = FLAGS_word_symbol_table;
|
||
|
decoder_opts.fst_path = FLAGS_graph_path;
|
||
|
LOG(INFO) << "fst path: " << decoder_opts.fst_path;
|
||
|
LOG(INFO) << "fst symbole table: " << decoder_opts.word_symbol_table;
|
||
|
|
||
|
decoder_opts.opts.max_active = FLAGS_max_active;
|
||
|
decoder_opts.opts.beam = FLAGS_beam;
|
||
|
decoder_opts.opts.lattice_beam = FLAGS_lattice_beam;
|
||
2 years ago
|
decoder_opts.nbest = FLAGS_nbest;
|
||
2 years ago
|
LOG(INFO) << "LatticeFasterDecoder max active: "
|
||
|
<< decoder_opts.opts.max_active;
|
||
|
LOG(INFO) << "LatticeFasterDecoder beam: " << decoder_opts.opts.beam;
|
||
|
LOG(INFO) << "LatticeFasterDecoder lattice_beam: "
|
||
|
<< decoder_opts.opts.lattice_beam;
|
||
3 years ago
|
|
||
2 years ago
|
return decoder_opts;
|
||
|
}
|
||
3 years ago
|
};
|
||
|
|
||
2 years ago
|
class TLGDecoder : public DecoderBase {
|
||
3 years ago
|
public:
|
||
|
explicit TLGDecoder(TLGDecoderOptions opts);
|
||
2 years ago
|
~TLGDecoder() = default;
|
||
|
|
||
2 years ago
|
void InitDecoder() override;
|
||
|
void Reset() override;
|
||
2 years ago
|
|
||
|
void AdvanceDecode(
|
||
2 years ago
|
const std::shared_ptr<kaldi::DecodableInterface>& decodable) override;
|
||
2 years ago
|
|
||
3 years ago
|
void Decode();
|
||
2 years ago
|
|
||
2 years ago
|
std::string GetFinalBestPath() override;
|
||
|
std::string GetPartialResult() override;
|
||
2 years ago
|
|
||
2 years ago
|
const std::shared_ptr<fst::SymbolTable> WordSymbolTable() const override {
|
||
|
return word_symbol_table_;
|
||
|
}
|
||
|
|
||
3 years ago
|
int DecodeLikelihoods(const std::vector<std::vector<BaseFloat>>& probs,
|
||
2 years ago
|
const std::vector<std::string>& nbest_words);
|
||
2 years ago
|
|
||
2 years ago
|
void FinalizeSearch() override;
|
||
|
const std::vector<std::vector<int>>& Inputs() const override {
|
||
|
return hypotheses_;
|
||
|
}
|
||
|
const std::vector<std::vector<int>>& Outputs() const override {
|
||
2 years ago
|
return olabels_;
|
||
2 years ago
|
} // outputs_; }
|
||
|
const std::vector<float>& Likelihood() const override {
|
||
|
return likelihood_;
|
||
|
}
|
||
|
const std::vector<std::vector<int>>& Times() const override {
|
||
|
return times_;
|
||
|
}
|
||
|
|
||
2 years ago
|
protected:
|
||
|
std::string GetBestPath() override {
|
||
|
CHECK(false);
|
||
|
return {};
|
||
|
}
|
||
|
std::vector<std::pair<double, std::string>> GetNBestPath() override {
|
||
|
CHECK(false);
|
||
|
return {};
|
||
|
}
|
||
|
std::vector<std::pair<double, std::string>> GetNBestPath(int n) override {
|
||
|
CHECK(false);
|
||
|
return {};
|
||
|
}
|
||
3 years ago
|
|
||
|
private:
|
||
3 years ago
|
void AdvanceDecoding(kaldi::DecodableInterface* decodable);
|
||
3 years ago
|
|
||
2 years ago
|
int num_frame_decoded_;
|
||
2 years ago
|
std::vector<std::vector<int>> hypotheses_;
|
||
2 years ago
|
std::vector<std::vector<int>> olabels_;
|
||
2 years ago
|
std::vector<float> likelihood_;
|
||
|
std::vector<std::vector<int>> times_;
|
||
|
|
||
3 years ago
|
std::shared_ptr<kaldi::LatticeFasterOnlineDecoder> decoder_;
|
||
3 years ago
|
std::shared_ptr<fst::Fst<fst::StdArc>> fst_;
|
||
3 years ago
|
std::shared_ptr<fst::SymbolTable> word_symbol_table_;
|
||
2 years ago
|
TLGDecoderOptions opts_;
|
||
3 years ago
|
};
|
||
3 years ago
|
|
||
|
|
||
2 years ago
|
} // namespace ppspeech
|