25 #include <fst/script/print.h> 35 inline void PrintFst (
const Fst<Arc>&
fst, std::ostream *os ) {
36 script::PrintFst<Arc>(fst, *os);
48 , std::string
const &extname =
"fst") {
51 return (
ends_with ( filename,
"." + extname +
".gz" )
63 template <
class Arc >
64 inline Fst<Arc> *
FstRead (
const std::string& filename ) {
67 Fst<Arc> *h = Fst<Arc>::Read ( *file.
getStream(), fro );
68 USER_CHECK ( h,
"Error while reading an FST" );
77 template <
class Arc >
81 VectorFst<Arc> *h = VectorFst<Arc>::Read ( *file.
getStream(), fro );
83 "Error while reading an FST (is it a vector fst, is the semiring correct?" );
93 template <
class Arc >
94 inline ConstFst<Arc> *
ConstFstRead (
const std::string& filename ) {
97 ConstFst<Arc> *h = ConstFst<Arc>::Read ( *file.
getStream(), fro );
99 "Error while reading an FST (is it a const fst, is the semiring correct?" );
112 ,
const std::string& filename
113 ,
const std::string& txtname =
"txt" ) {
114 if ( filename ==
"/dev/null" )
return;
115 LDEBUG (
"Started..." << filename);
119 LERROR (
"Error opening " << filename);
123 && filename !=
"/dev/stdout" 124 && filename !=
"/dev/stderr" 133 fst.Write ( *file.
getStream(), FstWriteOptions (filename) );
146 Hyp (std::basic_string<unsigned>
const& h
147 , std::basic_string<unsigned>
const &oh
148 ,
typename Arc::Weight
const& c)
169 template <
class Arc,
class HypT>
172 typename Arc::StateId start = fst.Start();
173 for (ArcIterator<VectorFst<Arc> > ai (fst, start); !ai.Done(); ai.Next() ) {
174 std::basic_string<unsigned>
hyp,
ohyp;
176 hyp.push_back (a.ilabel);
177 ohyp.push_back (a.olabel);
178 typename Arc::Weight w = a.weight;
179 typename Arc::StateId nextState = a.nextstate;
180 ArcIterator<VectorFst<Arc> >* ai2 =
181 new ArcIterator<VectorFst<Arc> > (fst, nextState);
182 while (!ai2->Done() ) {
183 Arc a2 = ai2->Value();
184 hyp.push_back (a2.ilabel);
185 ohyp.push_back (a2.olabel);
186 w =
Times (w, a2.weight);
187 nextState = a2.nextstate;
189 ai2 =
new ArcIterator<VectorFst<Arc> > (fst, nextState);
192 hyps->push_back (HypT (hyp, ohyp, w) );
Wrapper stream class that writes to pipes, text files or gzipped files.
std::ostream * getStream()
Returns internal stream.
VectorFst< Arc > * VectorFstRead(const std::string &filename)
Templated method that reads VectorFst.
bool DetectFstFile(std::string const &filename, std::string const &extname="fst")
Detect trivially by extension whether it is an fst or not.
std::basic_string< unsigned > ohyp
Struct template that represents a hypothesis in a lattice.
Fst< Arc > * FstRead(const std::string &filename)
Templated method that reads an fst.
ConstFst< Arc > * ConstFstRead(const std::string &filename)
Templated method that reads ConstFst.
void FstWrite(const Fst< Arc > &fst, const std::string &filename, const std::string &txtname="txt")
Templated method that writes an fst either in binary or text format.
Hyp(std::basic_string< unsigned > const &h, std::basic_string< unsigned > const &oh, typename Arc::Weight const &c)
bool is_open()
Checks whether the file is open.
void PrintFst(const Fst< Arc > &fst, std::ostream *os)
Templated method that writes an fst in openfst text format to a stream.
std::basic_string< unsigned > hyp
std::istream * getStream()
Returns internal stream.
void printStrings(const VectorFst< Arc > &fst, std::vector< HypT > *hyps)
Traverses an fst and stores the hypotheses in a vector. Typically the fst is the result of ShortestPa...
#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)
Wrapper stream class that reads pipes, text files or gzipped files.
bool ends_with(std::string const &haystack, std::string const &needle)
void close()
Closes the file.