28 #if OPENFSTVERSION>=1003002 //1.3.2 29 return fst::FloatLimits<float>::PosInfinity();
31 return fst::FloatLimits<float>::kPosInfinity;
43 unordered_set<std::string> *vcb ) {
44 USER_CHECK ( vcb,
"NULL pointer not accepted" );
45 using fst::StateIterator;
47 using fst::ArcIterator;
48 typedef typename Arc::StateId StateId;
49 for ( StateIterator< VectorFst<Arc> > si ( myfst ); !si.Done(); si.Next() ) {
50 StateId state_id = si.Value();
51 for ( ArcIterator< VectorFst<Arc> > ai ( myfst, si.Value() ); !ai.Done();
54 vcb->insert (
toString ( arc.ilabel ) );
68 unordered_set<unsigned> *vcb,
69 unsigned offset = 0) {
70 USER_CHECK ( vcb,
"NULL pointer not accepted" );
71 using fst::StateIterator;
73 using fst::ArcIterator;
74 typedef typename Arc::StateId StateId;
75 for ( StateIterator< VectorFst<Arc> > si ( myfst ); !si.Done(); si.Next() ) {
76 StateId state_id = si.Value();
77 for ( ArcIterator< VectorFst<Arc> > ai ( myfst, si.Value() ); !ai.Done();
79 vcb->insert ( ai.Value().ilabel + offset );
92 unordered_set<std::string> *vcb ) {
93 USER_CHECK ( vcb,
"NULL pointer not accepted" );
94 typedef typename Arc::StateId StateId;
95 using fst::StateIterator;
97 using fst::ArcIterator;
98 for ( StateIterator< VectorFst<Arc> > si ( myfst ); !si.Done(); si.Next() ) {
99 StateId state_id = si.Value();
100 for ( ArcIterator< VectorFst<Arc> > ai ( myfst, si.Value() ); !ai.Done();
102 Arc arc = ai.Value();
119 USER_CHECK ( myfst,
"NULL pointer not accepted" );
120 USER_CHECK ( myfst->NumStates(),
"Number of states is zero!" );
121 typedef typename Arc::StateId StateId;
122 fst::TopSort ( myfst );
123 fst::Map ( myfst, fst::RmWeightMapper<Arc>() );
124 for ( fst::StateIterator< fst::VectorFst<Arc> > si ( *myfst ); !si.Done();
126 StateId state_id = si.Value();
128 myfst->AddArc ( 0, Arc ( 0, 0, Arc::Weight::One(), si.Value() ) );
129 myfst->SetFinal ( state_id, Arc::Weight::One() );
132 fst::RmEpsilon ( myfst );
150 fst::VectorFst<Arc> *myfst ) {
151 fst::EncodeMapper<Arc> em ( fst::kEncodeLabels, fst::ENCODE );
152 fst::Encode ( myfst, &em );
154 fst::VectorFst<Arc> *fst2 =
new fst::VectorFst<Arc>;
155 fst::Determinize ( *myfst, fst2 );
156 fst::Minimize ( fst2 );
157 fst::EncodeMapper<Arc> em2 ( em,
159 fst::Decode ( fst2, em2 );
165 fst::VectorFst<Arc> *out ) {
168 fst::EncodeMapper<Arc> em ( fst::kEncodeLabels, fst::ENCODE );
169 fst::Encode ( out, &em );
171 fst::VectorFst<Arc> fst2;
172 fst::Determinize ( *out , &fst2 );
173 fst::Minimize ( &fst2 );
174 fst::EncodeMapper<Arc> em2 ( em,
176 fst::Decode ( &fst2, em2 );
191 inline std::basic_string<CharTypeT>
195 VectorFst<Arc> hypfst;
196 ShortestPath(latfst, &hypfst);
197 Project(&hypfst, PROJECT_INPUT);
200 basic_string<CharTypeT> hypstr;
201 for (StateIterator< VectorFst<Arc> > si(hypfst); !si.Done();
203 for (ArcIterator< VectorFst<Arc> > ai(hypfst, si.Value());
204 !ai.Done(); ai.Next()) {
206 ss << ai.Value().ilabel;
207 StringTypeT value; ss >> value;
219 , std::vector<CharTypeT> &hyp) {
221 std::basic_string<CharTypeT> aux = FstGetBestHypothesis<Arc,CharTypeT, CharTypeT>(latfst);
223 hyp.resize(aux.size());
224 std::copy(aux.begin(), aux.end(), hyp.begin());
230 , std::string &hyp) {
231 std::basic_string<unsigned> aux = FstGetBestHypothesis<Arc,unsigned, unsigned>(latfst);
233 for (
unsigned k =0; k < aux.size(); ++k){
234 std::stringstream ss; ss << aux[k];
235 hyp += ss.str() +
" ";
254 std::ostream *hyps,
unsigned s = 0 ) {
255 static std::basic_string<unsigned> ihyp;
256 static std::basic_string<unsigned> ohyp;
257 static std::basic_string<typename Arc::Weight> cost;
259 "The state most surely doesn't exist! Topsort this lattice and make sure you use a valid s" ) )
261 typename Arc::Weight fvalue = Arc::Weight::One();
262 fst::ArcIterator< fst::VectorFst<Arc> > i ( pcostslat, s );
264 for ( ; !i.Done(); i.Next() ) {
266 ihyp.push_back ( a.ilabel );
267 ohyp.push_back ( a.olabel );
268 cost.push_back ( a.weight );
270 ihyp.resize ( ihyp.size() - 1 );
271 ohyp.resize ( ohyp.size() - 1 );
272 cost.resize ( cost.size() - 1 );
274 fvalue = pcostslat.Final ( s );
275 if ( fvalue == Arc::Weight::Zero() )
return;
277 for (
unsigned k = 0; k < ihyp.size();
278 ++k )
if ( ihyp[k] != 0 ) *hyps << ihyp[k] <<
" ";
280 for (
unsigned k = 0; k < ihyp.size();
281 ++k )
if ( ohyp[k] != 0 ) *hyps << ohyp[k] <<
" ";
283 typename Arc::Weight c = fvalue;
284 for (
unsigned k = 0; k < cost.size(); ++k ) {
285 c =
Times ( c, cost[k] );
301 if ( h1.
cost > h2.
cost )
return true;
309 , unordered_map<std::string, float>& finalhyps
310 ,
bool input =
true ) {
311 fst::VectorFst<Arc> ofst ( fst );
312 fst::TopSort ( &ofst );
313 std::vector <std::vector<struct hypcost> > partialhyps;
318 std::vector<struct hypcost> vhcempty;
319 partialhyps.resize ( ofst.NumStates() );
320 partialhyps[0].push_back ( hcempty );
321 for ( fst::StateIterator< fst::MutableFst<Arc> > si ( ofst ); !si.Done();
323 typename Arc::Weight value = ofst.Final ( si.Value() );
324 std::vector<struct hypcost>& hypc = partialhyps[ ( unsigned ) si.Value()];
325 for (
unsigned k = 0; k < hypc.size(); ++k ) {
326 if ( value != Arc::Weight::Zero() ) {
327 finalhyps[partialhyps[ ( unsigned ) si.Value()][k].hyp] =
328 partialhyps[ ( unsigned ) si.Value()][k].cost + ( float ) value.Value();
330 for ( fst::MutableArcIterator< fst::MutableFst<Arc> > ai ( &ofst, si.Value() );
331 !ai.Done(); ai.Next() ) {
332 struct hypcost hcnew = hypc[k];
333 Arc arc = ai.Value();
338 hcnew.
cost += ( float ) arc.weight.Value();
339 partialhyps[arc.nextstate].push_back ( hcnew );
342 partialhyps[ ( unsigned ) si.Value()] =
349 using fst::VectorFst;
350 VectorFst<StdArc> tmp;
351 fst::ShortestPath (*fst, &tmp);
352 fst::RmEpsilon (&tmp);
354 for (fst::StateIterator< VectorFst<StdArc> > si (tmp); !si.Done(); si.Next() ) {
355 for (fst::ArcIterator< VectorFst<StdArc> > ai (tmp, si.Value() ); !ai.Done();
366 fst::VectorFst<Arc>* tmp =
new fst::VectorFst<Arc>;
367 fst::Push<Arc, fst::REWEIGHT_TO_FINAL> (*fst, tmp, fst::kPushWeights);
371 inline fst::VectorFst<fst::LogArc>*
StdToLog (
const fst::VectorFst<fst::StdArc>*
373 fst::VectorFst<fst::LogArc>* tmp =
new fst::VectorFst<fst::LogArc>;
374 fst::Map (*fst, tmp, fst::StdToLogMapper() );
378 inline fst::VectorFst<fst::StdArc>*
LogToStd (
const fst::VectorFst<fst::LogArc>*
381 using fst::VectorFst;
382 VectorFst<StdArc>* tmp =
new VectorFst<StdArc>;
383 fst::Map (*fst, tmp, fst::LogToStdMapper() );
389 ,
const double scale) {
391 using fst::VectorFst;
392 VectorFst<StdArc>* fstscaled = fst->Copy();
393 for (fst::StateIterator< VectorFst<StdArc> > si (*fstscaled); !si.Done();
395 for (fst::MutableArcIterator< VectorFst<StdArc> > ai (fstscaled, si.Value() );
396 !ai.Done(); ai.Next() ) {
397 StdArc arc = ai.Value();
398 arc.weight =
static_cast<StdArc::Weight
> (arc.weight.Value() * scale);
401 StdArc::Weight
final = fstscaled->Final (si.Value() );
403 fstscaled->SetFinal (si.Value(),
404 static_cast<StdArc::Weight
> (
final.Value() * scale) );
410 #define UNIT_COST_POSITIVE 1 411 #define UNIT_COST_NEGATIVE -1 415 unsigned& jMin,
unsigned& jMax) {
417 using fst::RmWeightMapper;
418 using fst::TimesMapper;
419 fst::VectorFst<Arc>* tmp = fst->Copy();
420 Map (tmp, RmWeightMapper<Arc, Arc>() );
423 Map (tmp, RmWeightMapper<Arc, Arc>() );
430 fst::VectorFst<fst::StdArc>*
fst) {
432 fst::StdArc::Label x = 0;
433 for (fst::StateIterator< fst::VectorFst<StdArc> > si (*fst); !si.Done();
435 for (fst::ArcIterator< fst::VectorFst<StdArc> > ai (*fst, si.Value() );
436 !ai.Done(); ai.Next() ) {
437 if (ai.Value().ilabel > x) {
438 x = ai.Value().ilabel;
446 const fst::StdArc::Weight cost) {
447 for (fst::StateIterator< fst::MutableFst<fst::StdArc> > si (*fst); !si.Done();
450 fst->SetFinal (si.Value(), cost);
464 fst::VectorFst<Arc> *
fst,
465 const std::string& tidxwords =
"",
466 typename Arc::Weight finalweight = Arc::Weight::One()
468 assert (sidxwords !=
"");
469 std::vector<std::string> swords;
470 boost::algorithm::split (swords, sidxwords,
471 boost::algorithm::is_any_of (
" " ) );
474 for (
unsigned k = 0; k < swords.size(); ++k) {
475 typename Arc::Label swidx = ucam::util::toNumber<unsigned> (swords[k]);
477 fst->AddArc (k, Arc (swidx, 0, Arc::Weight::One(), k + 1) );
479 if (tidxwords ==
"") {
480 fst->SetFinal (swords.size(), Arc::Weight::One() );
481 fst::Project<Arc> (fst,
483 fst->SetFinal (swords.size(), finalweight);
486 std::vector<std::string> twords;
487 boost::algorithm::split (twords, tidxwords,
488 boost::algorithm::is_any_of (
" " ) );
489 for (
unsigned k = swords.size(); k < swords.size() + twords.size(); ++k) {
490 typename Arc::Label twidx = ucam::util::toNumber<unsigned>
491 (twords[k - swords.size()]);
493 fst->AddArc (k, Arc (0, twidx, Arc::Weight::One(), k + 1) );
495 fst->SetFinal (swords.size() + twords.size(), finalweight);
496 fst::Determinize (fst::RmEpsilonFst<Arc> (*fst), fst);
498 fst::RmEpsilon (fst);
506 std::vector<pair <typename Arc::Label, typename Arc::Label> > ipairs;
507 std::vector<pair <typename Arc::Label, typename Arc::Label> > opairs;
512 typename Arc::Label labelreplace) {
513 ipairs.push_back (pair <typename Arc::Label, typename Arc::Label> (labelfind,
518 typename Arc::Label labelreplace) {
519 opairs.push_back (pair <typename Arc::Label, typename Arc::Label> (labelfind,
524 fst::Relabel (hypfst, ipairs, opairs);
527 inline fst::VectorFst<Arc>& operator() (fst::VectorFst<Arc>& hypfst) {
528 fst::Relabel (&hypfst, ipairs, opairs);
fst::VectorFst< fst::LogArc > * StdToLog(const fst::VectorFst< fst::StdArc > *fst)
std::string toString(const T &x, uint pr=2)
Converts an arbitrary type to string Converts to string integers, floats, doubles Quits execution if ...
void FstGetBestStringHypothesis(const fst::VectorFst< Arc > &latfst, std::string &hyp)
fst::VectorFst< fst::StdArc > * LogToStd(const fst::VectorFst< fst::LogArc > *fst)
#define UNIT_COST_POSITIVE
#define LDBG_EXECUTE(order)
RelabelUtil & addIPL(typename Arc::Label labelfind, typename Arc::Label labelreplace)
void SetFinalStateCost(fst::MutableFst< fst::StdArc > *fst, const fst::StdArc::Weight cost)
fst::TropicalWeightTpl< F > Map(double)
unsigned ShortestPathLength(const fst::VectorFst< fst::StdArc > *fst)
fst::VectorFst< Arc > * EncodeDeterminizeMinimizeDecode(fst::VectorFst< Arc > *myfst)
Encodes, determinizes, minimizes and decodes an fst.
void string2fst(const std::string &sidxwords, fst::VectorFst< Arc > *fst, const std::string &tidxwords="", typename Arc::Weight finalweight=Arc::Weight::One())
Convenience method that creates an fsa/fst from one/two string(s) of numbers.
void printstrings(const fst::VectorFst< Arc > &pcostslat, std::ostream *hyps, unsigned s=0)
Trivial function that outputs all the hypothesis in the lattice with its cost.
void GetMinAndMaxHypothesisLength(const fst::VectorFst< Arc > *fst, unsigned &jMin, unsigned &jMax)
void buildSubstringTransducer(fst::VectorFst< Arc > *myfst)
Builds substring version of an fst. This is a destructive implementation.
Utility functor for relabeling one or more lattices. Note that you can chain commands. See Unit test in fstutils.gtest.cpp for an example.
Struct for priority queue comparison.
#define UNIT_COST_NEGATIVE
void extractTargetVocabulary(const fst::VectorFst< Arc > &myfst, unordered_set< std::string > *vcb)
Extract target (right-side) vocabulary from an fst.
void extractSourceVocabulary(const fst::VectorFst< Arc > &myfst, unordered_set< std::string > *vcb)
Extract source (left-side) vocabulary from an fst.
RelabelUtil & addOPL(typename Arc::Label labelfind, typename Arc::Label labelreplace)
float ZPosInfinity()
Just a wrapper to maintain compatibility with OpenFST 1.3.1, last version using kPosInfinity constant...
Class used by priority queue to compare two hypotheses and decide which one wins. ...
fst::VectorFst< fst::StdArc > * FstScaleWeights(fst::VectorFst< fst::StdArc > *fst, const double scale)
#define USER_CHECK(exp, comment)
Tests whether exp is true. If not, comment is printed and program ends.
TropicalSparseTupleWeight< T > Times(const TropicalSparseTupleWeight< T > &w1, const TropicalSparseTupleWeight< T > &w2)
fst::VectorFst< Arc > * PushWeightsToFinal(const fst::VectorFst< Arc > *fst)
fst::StdArc::Label GetFirstUnusedLabelId(const fst::VectorFst< fst::StdArc > *fst)
std::basic_string< CharTypeT > FstGetBestHypothesis(const fst::VectorFst< Arc > &latfst)
Takes the 1-best of an fst and converts to string.