KwsTermsAligner Class Reference

#include <kws-scoring.h>

Collaboration diagram for KwsTermsAligner:

Public Member Functions

void AddRef (const KwsTerm &ref)
 
void AddHyp (const KwsTerm &hyp)
 
int nof_hyps () const
 
int nof_refs () const
 
 KwsTermsAligner (const KwsTermsAlignerOptions &opts)
 
KwsAlignment AlignTerms ()
 
virtual float AlignerScore (const KwsTerm &ref, const KwsTerm &hyp)
 

Private Types

typedef std::vector< KwsTermTermArray
 
typedef std::vector< KwsTerm >::iterator TermIterator
 
typedef unordered_map< int, boolTermUseMap
 

Private Member Functions

int FindBestRefIndex (const KwsTerm &term)
 
TermIterator FindNextRef (const KwsTerm &hyp, const TermIterator &prev, const TermIterator &last)
 
bool RefExistsMaybe (const KwsTerm &term)
 
void FillUnmatchedRefs (KwsAlignment *ali)
 

Private Attributes

unordered_map< int, unordered_map< std::string, TermArray > > refs_
 
unordered_map< int, unordered_map< std::string, TermUseMap > > used_ref_terms_
 
std::list< KwsTermhyps_
 
KwsTermsAlignerOptions opts_
 
int nof_refs_
 
int nof_hyps_
 

Detailed Description

Definition at line 136 of file kws-scoring.h.

Member Typedef Documentation

◆ TermArray

typedef std::vector<KwsTerm> TermArray
private

Definition at line 160 of file kws-scoring.h.

◆ TermIterator

typedef std::vector<KwsTerm>::iterator TermIterator
private

Definition at line 161 of file kws-scoring.h.

◆ TermUseMap

typedef unordered_map<int, bool> TermUseMap
private

Definition at line 162 of file kws-scoring.h.

Constructor & Destructor Documentation

◆ KwsTermsAligner()

KwsTermsAligner ( const KwsTermsAlignerOptions opts)
explicit

Definition at line 119 of file kws-scoring.cc.

119  :
120  opts_(opts),
121  nof_refs_(0),
122  nof_hyps_(0) { }
KwsTermsAlignerOptions opts_
Definition: kws-scoring.h:166

Member Function Documentation

◆ AddHyp()

void AddHyp ( const KwsTerm hyp)
inline

Definition at line 143 of file kws-scoring.h.

Referenced by main().

143  {
144  hyps_.push_back(hyp);
145  nof_hyps_++;
146  }
std::list< KwsTerm > hyps_
Definition: kws-scoring.h:165

◆ AddRef()

void AddRef ( const KwsTerm ref)
inline

Definition at line 138 of file kws-scoring.h.

References KwsTerm::kw_id(), and KwsTerm::utt_id().

Referenced by main().

138  {
139  refs_[ref.utt_id()][ref.kw_id()].push_back(ref);
140  nof_refs_++;
141  }
unordered_map< int, unordered_map< std::string, TermArray > > refs_
Definition: kws-scoring.h:163

◆ AlignerScore()

float AlignerScore ( const KwsTerm ref,
const KwsTerm hyp 
)
virtual

Definition at line 236 of file kws-scoring.cc.

References KwsTerm::end_time(), and KwsTerm::start_time().

Referenced by KwsTermsAligner::AlignTerms(), and KwsTermsAligner::FindBestRefIndex().

236  {
237  float overlap = std::min(ref.end_time(), hyp.end_time())
238  - std::max(ref.start_time(), hyp.start_time());
239  float join = std::max(ref.end_time(), hyp.end_time())
240  - std::min(ref.start_time(), hyp.start_time());
241  return static_cast<float>(overlap) / join;
242 }

◆ AlignTerms()

KwsAlignment AlignTerms ( )

Definition at line 125 of file kws-scoring.cc.

References KwsAlignment::Add(), AlignedTermsPair::aligner_score, KwsTermsAligner::AlignerScore(), KwsTermsAligner::FillUnmatchedRefs(), KwsTermsAligner::FindBestRefIndex(), AlignedTermsPair::hyp, KwsTermsAligner::hyps_, KALDI_LOG, AlignedTermsPair::ref, KwsTermsAligner::refs_, KwsAlignment::size(), and KwsTermsAligner::used_ref_terms_.

Referenced by main().

125  {
126  KwsAlignment alignment;
127 
128  used_ref_terms_.clear();
129  std::list<KwsTerm>::iterator it = hyps_.begin();
130  for (; it != hyps_.end(); ++it) {
131  AlignedTermsPair ref_hyp_pair;
132  ref_hyp_pair.hyp = *it;
133  ref_hyp_pair.aligner_score = -std::numeric_limits<float>::infinity();
134 
135  int ref_idx = FindBestRefIndex(*it);
136  if (ref_idx >= 0) { // If found
137  int utt_id = it->utt_id();
138  std::string kw_id = it->kw_id();
139 
140  ref_hyp_pair.ref = refs_[utt_id][kw_id][ref_idx];
141  used_ref_terms_[utt_id][kw_id][ref_idx] = true;
142  ref_hyp_pair.aligner_score = AlignerScore(ref_hyp_pair.ref,
143  ref_hyp_pair.hyp);
144  }
145 
146  alignment.Add(ref_hyp_pair);
147  }
148  KALDI_LOG << "Alignment size before adding unseen: " << alignment.size();
149  // Finally, find the terms in ref which have not been seen in hyp
150  // and add them into the alignment
151  FillUnmatchedRefs(&alignment);
152  KALDI_LOG << "Alignment size after adding unseen: " << alignment.size();
153  return alignment;
154 }
virtual float AlignerScore(const KwsTerm &ref, const KwsTerm &hyp)
Definition: kws-scoring.cc:236
unordered_map< int, unordered_map< std::string, TermUseMap > > used_ref_terms_
Definition: kws-scoring.h:164
void FillUnmatchedRefs(KwsAlignment *ali)
Definition: kws-scoring.cc:156
std::list< KwsTerm > hyps_
Definition: kws-scoring.h:165
unordered_map< int, unordered_map< std::string, TermArray > > refs_
Definition: kws-scoring.h:163
#define KALDI_LOG
Definition: kaldi-error.h:153
int FindBestRefIndex(const KwsTerm &term)
Definition: kws-scoring.cc:182

◆ FillUnmatchedRefs()

void FillUnmatchedRefs ( KwsAlignment ali)
private

Definition at line 156 of file kws-scoring.cc.

References KwsAlignment::Add(), AlignedTermsPair::aligner_score, AlignedTermsPair::ref, KwsTermsAligner::refs_, and KwsTermsAligner::used_ref_terms_.

Referenced by KwsTermsAligner::AlignTerms().

156  {
157  // We have to traverse the whole ref_ structure and check
158  // against the used_ref_terms_ structure if the given ref term
159  // was already used or not. If not, we will add it to the alignment
160  typedef unordered_map<std::string, TermArray> KwList;
161  typedef KwList::iterator KwIndex;
162  typedef unordered_map<int, KwList >::iterator UttIndex;
163 
164  for (UttIndex utt = refs_.begin(); utt != refs_.end(); ++utt) {
165  int utt_id = utt->first;
166  for (KwIndex kw = refs_[utt_id].begin(); kw != refs_[utt_id].end(); ++kw) {
167  std::string kw_id = kw->first;
168  for (TermIterator term = refs_[utt_id][kw_id].begin();
169  term != refs_[utt_id][kw_id].end(); ++term ) {
170  int idx = term - refs_[utt_id][kw_id].begin();
171  if (!used_ref_terms_[utt_id][kw_id][idx]) {
172  AlignedTermsPair missed_hyp;
173  missed_hyp.aligner_score = -std::numeric_limits<float>::infinity();
174  missed_hyp.ref = refs_[utt_id][kw_id][idx];
175  ali->Add(missed_hyp);
176  }
177  }
178  }
179  }
180 }
unordered_map< int, unordered_map< std::string, TermUseMap > > used_ref_terms_
Definition: kws-scoring.h:164
std::vector< KwsTerm >::iterator TermIterator
Definition: kws-scoring.h:161
unordered_map< int, unordered_map< std::string, TermArray > > refs_
Definition: kws-scoring.h:163

◆ FindBestRefIndex()

int FindBestRefIndex ( const KwsTerm term)
private

Definition at line 182 of file kws-scoring.cc.

References KwsTermsAligner::AlignerScore(), KwsTermsAligner::FindNextRef(), KwsTerm::kw_id(), KwsTermsAligner::RefExistsMaybe(), KwsTermsAligner::refs_, KwsTermsAligner::used_ref_terms_, and KwsTerm::utt_id().

Referenced by KwsTermsAligner::AlignTerms().

182  {
183  if (!RefExistsMaybe(term)) {
184  return -1;
185  }
186  int utt_id = term.utt_id();
187  std::string kw_id = term.kw_id();
188 
189  TermIterator start_mark = refs_[utt_id][kw_id].begin();
190  TermIterator end_mark = refs_[utt_id][kw_id].end();
191 
192  TermIterator it = FindNextRef(term, start_mark, end_mark);
193  if (it == end_mark) {
194  return -1;
195  }
196 
197  int best_ref_idx = -1;
198  float best_ref_score = -std::numeric_limits<float>::infinity();
199  do {
200  float current_score = AlignerScore(*it, term);
201  int current_index = it - start_mark;
202  if ((current_score > best_ref_score) &&
203  (!used_ref_terms_[utt_id][kw_id][current_index])) {
204  best_ref_idx = current_index;
205  best_ref_score = current_score;
206  }
207 
208  it = FindNextRef(term, ++it, end_mark);
209  } while (it != end_mark);
210 
211  return best_ref_idx;
212 }
bool RefExistsMaybe(const KwsTerm &term)
Definition: kws-scoring.cc:215
virtual float AlignerScore(const KwsTerm &ref, const KwsTerm &hyp)
Definition: kws-scoring.cc:236
unordered_map< int, unordered_map< std::string, TermUseMap > > used_ref_terms_
Definition: kws-scoring.h:164
std::vector< KwsTerm >::iterator TermIterator
Definition: kws-scoring.h:161
unordered_map< int, unordered_map< std::string, TermArray > > refs_
Definition: kws-scoring.h:163
TermIterator FindNextRef(const KwsTerm &hyp, const TermIterator &prev, const TermIterator &last)
Definition: kws-scoring.cc:228

◆ FindNextRef()

KwsTermsAligner::TermIterator FindNextRef ( const KwsTerm hyp,
const TermIterator prev,
const TermIterator last 
)
private

Definition at line 228 of file kws-scoring.cc.

References KwsTermsAlignerOptions::max_distance, and KwsTermsAligner::opts_.

Referenced by KwsTermsAligner::FindBestRefIndex().

231  {
232  return std::find_if(prev, last,
233  kws_internal::KwTermEqual(opts_.max_distance, ref));
234 }
KwsTermsAlignerOptions opts_
Definition: kws-scoring.h:166

◆ nof_hyps()

int nof_hyps ( ) const
inline

Definition at line 148 of file kws-scoring.h.

Referenced by main().

148 {return nof_hyps_;}

◆ nof_refs()

int nof_refs ( ) const
inline

Definition at line 149 of file kws-scoring.h.

Referenced by main().

149 {return nof_refs_;}

◆ RefExistsMaybe()

bool RefExistsMaybe ( const KwsTerm term)
private

Definition at line 215 of file kws-scoring.cc.

References count, KwsTerm::kw_id(), KwsTermsAligner::refs_, and KwsTerm::utt_id().

Referenced by KwsTermsAligner::FindBestRefIndex().

215  {
216  int utt_id = term.utt_id();
217  std::string kw_id = term.kw_id();
218  if (refs_.count(utt_id) != 0) {
219  if (refs_[utt_id].count(kw_id) != 0) {
220  return true;
221  }
222  }
223  return false;
224 }
const size_t count
unordered_map< int, unordered_map< std::string, TermArray > > refs_
Definition: kws-scoring.h:163

Member Data Documentation

◆ hyps_

std::list<KwsTerm> hyps_
private

Definition at line 165 of file kws-scoring.h.

Referenced by KwsTermsAligner::AlignTerms().

◆ nof_hyps_

int nof_hyps_
private

Definition at line 168 of file kws-scoring.h.

◆ nof_refs_

int nof_refs_
private

Definition at line 167 of file kws-scoring.h.

◆ opts_

KwsTermsAlignerOptions opts_
private

Definition at line 166 of file kws-scoring.h.

Referenced by KwsTermsAligner::FindNextRef().

◆ refs_

unordered_map<int, unordered_map<std::string, TermArray > > refs_
private

◆ used_ref_terms_

unordered_map<int, unordered_map<std::string, TermUseMap > > used_ref_terms_
private

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