Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef FST_LIB_REVERSE_H__
00022 #define FST_LIB_REVERSE_H__
00023
00024 #include <algorithm>
00025 #include <vector>
00026 using std::vector;
00027
00028 #include <fst/cache.h>
00029
00030 namespace fst {
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 template<class Arc, class RevArc>
00042 void Reverse(const Fst<Arc> &ifst, MutableFst<RevArc> *ofst) {
00043 typedef typename Arc::StateId StateId;
00044 typedef typename Arc::Weight Weight;
00045 typedef typename RevArc::Weight RevWeight;
00046
00047 ofst->DeleteStates();
00048 ofst->SetInputSymbols(ifst.InputSymbols());
00049 ofst->SetOutputSymbols(ifst.OutputSymbols());
00050 StateId istart = ifst.Start();
00051 StateId ostart = ofst->AddState();
00052 ofst->SetStart(ostart);
00053
00054 for (StateIterator< Fst<Arc> > siter(ifst);
00055 !siter.Done();
00056 siter.Next()) {
00057 StateId is = siter.Value();
00058 StateId os = is + 1;
00059 while (ofst->NumStates() <= os)
00060 ofst->AddState();
00061 if (is == istart)
00062 ofst->SetFinal(os, RevWeight::One());
00063
00064 Weight final = ifst.Final(is);
00065 if (final != Weight::Zero()) {
00066 RevArc oarc(0, 0, final.Reverse(), os);
00067 ofst->AddArc(0, oarc);
00068 }
00069
00070 for (ArcIterator< Fst<Arc> > aiter(ifst, is);
00071 !aiter.Done();
00072 aiter.Next()) {
00073 const Arc &iarc = aiter.Value();
00074 RevArc oarc(iarc.ilabel, iarc.olabel, iarc.weight.Reverse(), os);
00075 StateId nos = iarc.nextstate + 1;
00076 while (ofst->NumStates() <= nos)
00077 ofst->AddState();
00078 ofst->AddArc(nos, oarc);
00079 }
00080 }
00081 uint64 iprops = ifst.Properties(kCopyProperties, false);
00082 uint64 oprops = ofst->Properties(kFstProperties, false);
00083 ofst->SetProperties(ReverseProperties(iprops) | oprops, kFstProperties);
00084 }
00085
00086 }
00087
00088 #endif /// FST_LIB_REVERSE_H__
00089