ConstArpaLmDeterministicFst Class Reference

This class wraps a ConstArpaLm format language model with the interface defined in DeterministicOnDemandFst. More...

#include <const-arpa-lm.h>

Inheritance diagram for ConstArpaLmDeterministicFst:
Collaboration diagram for ConstArpaLmDeterministicFst:

Public Types

typedef fst::StdArc::Weight Weight
 
typedef fst::StdArc::StateId StateId
 
typedef fst::StdArc::Label Label
 
- Public Types inherited from DeterministicOnDemandFst< fst::StdArc >
typedef fst::StdArc ::StateId StateId
 
typedef fst::StdArc ::Weight Weight
 
typedef fst::StdArc ::Label Label
 

Public Member Functions

 ConstArpaLmDeterministicFst (const ConstArpaLm &lm)
 
virtual StateId Start ()
 
virtual Weight Final (StateId s)
 
virtual bool GetArc (StateId s, Label ilabel, fst::StdArc *oarc)
 
- Public Member Functions inherited from DeterministicOnDemandFst< fst::StdArc >
virtual Weight Final (StateId s)=0
 
virtual bool GetArc (StateId s, Label ilabel, fst::StdArc *oarc)=0
 Note: ilabel must not be epsilon. More...
 
virtual ~DeterministicOnDemandFst ()
 

Private Types

typedef unordered_map< std::vector< Label >, StateId, VectorHasher< Label > > MapType
 

Private Attributes

StateId start_state_
 
MapType wseq_to_state_
 
std::vector< std::vector< Label > > state_to_wseq_
 
const ConstArpaLmlm_
 

Detailed Description

This class wraps a ConstArpaLm format language model with the interface defined in DeterministicOnDemandFst.

Definition at line 391 of file const-arpa-lm.h.

Member Typedef Documentation

◆ Label

typedef fst::StdArc::Label Label

Definition at line 396 of file const-arpa-lm.h.

◆ MapType

typedef unordered_map<std::vector<Label>, StateId, VectorHasher<Label> > MapType
private

Definition at line 412 of file const-arpa-lm.h.

◆ StateId

typedef fst::StdArc::StateId StateId

Definition at line 395 of file const-arpa-lm.h.

◆ Weight

typedef fst::StdArc::Weight Weight

Definition at line 394 of file const-arpa-lm.h.

Constructor & Destructor Documentation

◆ ConstArpaLmDeterministicFst()

ConstArpaLmDeterministicFst ( const ConstArpaLm lm)
explicit

Definition at line 1013 of file const-arpa-lm.cc.

References ConstArpaLm::BosSymbol(), ConstArpaLmDeterministicFst::lm_, ConstArpaLmDeterministicFst::start_state_, ConstArpaLmDeterministicFst::state_to_wseq_, and ConstArpaLmDeterministicFst::wseq_to_state_.

1014  : lm_(lm) {
1015  // Creates a history state for <s>.
1016  std::vector<Label> bos_state(1, lm_.BosSymbol());
1017  state_to_wseq_.push_back(bos_state);
1018  wseq_to_state_[bos_state] = 0;
1019  start_state_ = 0;
1020 }
std::vector< std::vector< Label > > state_to_wseq_
int32 BosSymbol() const

Member Function Documentation

◆ Final()

fst::StdArc::Weight Final ( StateId  s)
virtual

Definition at line 1022 of file const-arpa-lm.cc.

References ConstArpaLm::EosSymbol(), ConstArpaLm::GetNgramLogprob(), KALDI_ASSERT, ConstArpaLmDeterministicFst::lm_, logprob, and ConstArpaLmDeterministicFst::state_to_wseq_.

1022  {
1023  // At this point, we should have created the state.
1024  KALDI_ASSERT(static_cast<size_t>(s) < state_to_wseq_.size());
1025  const std::vector<Label>& wseq = state_to_wseq_[s];
1026  float logprob = lm_.GetNgramLogprob(lm_.EosSymbol(), wseq);
1027  return Weight(-logprob);
1028 }
std::vector< std::vector< Label > > state_to_wseq_
float logprob
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
float GetNgramLogprob(const int32 word, const std::vector< int32 > &hist) const
int32 EosSymbol() const

◆ GetArc()

bool GetArc ( StateId  s,
Label  ilabel,
fst::StdArc oarc 
)
virtual

Definition at line 1030 of file const-arpa-lm.cc.

References ConstArpaLm::GetNgramLogprob(), ConstArpaLm::HistoryStateExists(), KALDI_ASSERT, ConstArpaLmDeterministicFst::lm_, logprob, ConstArpaLm::NgramOrder(), ConstArpaLmDeterministicFst::state_to_wseq_, and ConstArpaLmDeterministicFst::wseq_to_state_.

1031  {
1032  // At this point, we should have created the state.
1033  KALDI_ASSERT(static_cast<size_t>(s) < state_to_wseq_.size());
1034  std::vector<Label> wseq = state_to_wseq_[s];
1035 
1036  float logprob = lm_.GetNgramLogprob(ilabel, wseq);
1037  if (logprob == std::numeric_limits<float>::min()) {
1038  return false;
1039  }
1040 
1041  // Locates the next state in ConstArpaLm. Note that OOV and backoff have been
1042  // taken care of in ConstArpaLm.
1043  wseq.push_back(ilabel);
1044  while (wseq.size() >= lm_.NgramOrder()) {
1045  // History state has at most lm_.NgramOrder() -1 words in the state.
1046  wseq.erase(wseq.begin(), wseq.begin() + 1);
1047  }
1048  while (!lm_.HistoryStateExists(wseq)) {
1049  KALDI_ASSERT(wseq.size() > 0);
1050  wseq.erase(wseq.begin(), wseq.begin() + 1);
1051  }
1052 
1053  std::pair<const std::vector<Label>, StateId> wseq_state_pair(
1054  wseq, static_cast<Label>(state_to_wseq_.size()));
1055 
1056  // Attemps to insert the current <wseq_state_pair>. If the pair already exists
1057  // then it returns false.
1058  typedef MapType::iterator IterType;
1059  std::pair<IterType, bool> result = wseq_to_state_.insert(wseq_state_pair);
1060 
1061  // If the pair was just inserted, then also add it to <state_to_wseq_>.
1062  if (result.second == true)
1063  state_to_wseq_.push_back(wseq);
1064 
1065  // Creates the arc.
1066  oarc->ilabel = ilabel;
1067  oarc->olabel = ilabel;
1068  oarc->nextstate = result.first->second;
1069  oarc->weight = Weight(-logprob);
1070 
1071  return true;
1072 }
std::vector< std::vector< Label > > state_to_wseq_
float logprob
int32 NgramOrder() const
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
float GetNgramLogprob(const int32 word, const std::vector< int32 > &hist) const
bool HistoryStateExists(const std::vector< int32 > &hist) const

◆ Start()

virtual StateId Start ( )
inlinevirtual

Implements DeterministicOnDemandFst< fst::StdArc >.

Definition at line 402 of file const-arpa-lm.h.

Member Data Documentation

◆ lm_

◆ start_state_

StateId start_state_
private

◆ state_to_wseq_

std::vector<std::vector<Label> > state_to_wseq_
private

◆ wseq_to_state_


The documentation for this class was generated from the following files: