// sherpa-onnx/csrc/lodr-fst.h // // Contains code copied from icefall/utils/ngram_lm.py // Copyright (c) 2023 Xiaomi Corporation // // Copyright (c) 2025 Tilde SIA (Askars Salimbajevs) #ifndef SHERPA_ONNX_CSRC_LODR_FST_H_ #define SHERPA_ONNX_CSRC_LODR_FST_H_ #include #include #include #include #include #include #include #include #include #include "kaldifst/csrc/kaldi-fst-io.h" namespace sherpa_onnx { class Hypothesis; class LodrFst { public: explicit LodrFst(const std::string &fst_path, int32_t backoff_id = -1); std::pair, std::vector> GetNextStateCosts( int32_t state, int32_t label); float GetFinalCost(int32_t state); void ComputeScore(float scale, Hypothesis *hyp, int32_t offset); private: fst::StdVectorFst YsToFst(const std::vector &ys, int32_t offset); std::vector> ProcessBackoffArcs( int32_t state, float cost); std::optional> GetNextStatesCostsNoBackoff( int32_t state, int32_t label); int32_t FindBackoffId(); int32_t backoff_id_ = -1; std::unique_ptr fst_; // owned by this class }; class LodrStateCost { public: explicit LodrStateCost( LodrFst* fst, const std::unordered_map &state_cost = {}); LodrStateCost ForwardOneStep(int32_t label); float Score() const; float FinalScore() const; private: // The fst_ is not owned by this class and borrowed from the caller // (e.g. OnlineRnnLM). LodrFst* fst_; std::unordered_map state_cost_; }; } // namespace sherpa_onnx #endif // SHERPA_ONNX_CSRC_LODR_FST_H_