Cambridge SMT System
fstio.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use these files except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 // Copyright 2012 - Gonzalo Iglesias, AdriĆ  de Gispert, William Byrne
14 
22 #ifndef FSTIO_HPP
23 #define FSTIO_HPP
24 
25 #include <fst/script/print.h>
26 
27 namespace fst {
28 
34 template< class Arc>
35 inline void PrintFst ( const Fst<Arc>& fst, std::ostream *os ) {
36  script::PrintFst<Arc>(fst, *os);
37 
38  // FstPrinter<Arc> printer ( fst, 0, 0, 0, false, false );
39  // ///What is this string for?
40  // std::string foo;
41  // printer.Print ( os, foo );
42 };
43 
47 inline bool DetectFstFile(std::string const &filename
48  , std::string const &extname ="fst") {
50 
51  return (ends_with ( filename, "." + extname + ".gz" )
52  || ends_with ( filename, "." + extname )
53  )
54  ? true: false;
55 };
56 
63 template < class Arc >
64 inline Fst<Arc> *FstRead ( const std::string& filename ) {
65  ucam::util::iszfstream file ( filename );
66  FstReadOptions fro;
67  Fst<Arc> *h = Fst<Arc>::Read ( *file.getStream(), fro );
68  USER_CHECK ( h, "Error while reading an FST" );
69  return h;
70 };
71 
77 template < class Arc >
78 inline VectorFst<Arc> *VectorFstRead ( const std::string& filename ) {
79  ucam::util::iszfstream file ( filename );
80  FstReadOptions fro;
81  VectorFst<Arc> *h = VectorFst<Arc>::Read ( *file.getStream(), fro );
82  USER_CHECK ( h,
83  "Error while reading an FST (is it a vector fst, is the semiring correct?" );
84  return h;
85 };
86 
93 template < class Arc >
94 inline ConstFst<Arc> *ConstFstRead ( const std::string& filename ) {
95  ucam::util::iszfstream file ( filename );
96  FstReadOptions fro;
97  ConstFst<Arc> *h = ConstFst<Arc>::Read ( *file.getStream(), fro );
98  USER_CHECK ( h,
99  "Error while reading an FST (is it a const fst, is the semiring correct?" );
100  return h;
101 };
102 
110 template <class Arc>
111 inline void FstWrite ( const Fst<Arc>& fst
112  , const std::string& filename
113  , const std::string& txtname = "txt" ) {
114  if ( filename == "/dev/null" ) return;
115  LDEBUG ("Started..." << filename);
116  //boost::scoped_ptr<oszfstream> file ( new oszfstream ( filename ) );
117  ucam::util::oszfstream file (filename);
118  if (!file.is_open() ) {
119  LERROR ("Error opening " << filename);
120  exit (EXIT_FAILURE);
121  }
122  if ( filename != "-"
123  && filename != "/dev/stdout"
124  && filename != "/dev/stderr"
125  && (ucam::util::ends_with ( filename, "." + txtname + ".gz" )
126  || ucam::util::ends_with ( filename, "." + txtname )
127  )
128  ) {
129  PrintFst<Arc> ( fst, file.getStream() );
130  file.close();
131  return;
132  }
133  fst.Write ( *file.getStream(), FstWriteOptions (filename) );
134  file.close();
135  LDEBUG ("Finished...");
136 };
137 
141 template<class Arc>
142 struct Hyp {
143  std::basic_string<unsigned> hyp, ohyp;
144  typename Arc::Weight cost;
145 
146  Hyp (std::basic_string<unsigned> const& h
147  , std::basic_string<unsigned> const &oh
148  , typename Arc::Weight const& c)
149  : hyp (h)
150  , ohyp(oh)
151  , cost (c) {
152  }
153 
154  Hyp (Hyp<Arc> const& h)
155  : hyp (h.hyp)
156  , ohyp(h.ohyp)
157  , cost (h.cost) {
158  }
159 };
160 
169 template <class Arc, class HypT>
170 void printStrings (const VectorFst<Arc>& fst, std::vector<HypT>* hyps) {
171 
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;
175  Arc a = ai.Value();
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;
188  delete ai2;
189  ai2 = new ArcIterator<VectorFst<Arc> > (fst, nextState);
190  }
191  delete ai2;
192  hyps->push_back (HypT (hyp, ohyp, w) );
193  }
194 }
195 
196 } // end namespace
197 
198 #endif // FSTIO_HPP
Wrapper stream class that writes to pipes, text files or gzipped files.
Definition: szfstream.hpp:200
std::ostream * getStream()
Returns internal stream.
Definition: szfstream.hpp:241
VectorFst< Arc > * VectorFstRead(const std::string &filename)
Templated method that reads VectorFst.
Definition: fstio.hpp:78
bool DetectFstFile(std::string const &filename, std::string const &extname="fst")
Detect trivially by extension whether it is an fst or not.
Definition: fstio.hpp:47
Definition: fstio.hpp:27
std::basic_string< unsigned > ohyp
Definition: fstio.hpp:143
#define LDEBUG(msg)
Struct template that represents a hypothesis in a lattice.
Definition: fstio.hpp:142
Fst< Arc > * FstRead(const std::string &filename)
Templated method that reads an fst.
Definition: fstio.hpp:64
ConstFst< Arc > * ConstFstRead(const std::string &filename)
Templated method that reads ConstFst.
Definition: fstio.hpp:94
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.
Definition: fstio.hpp:111
Hyp(std::basic_string< unsigned > const &h, std::basic_string< unsigned > const &oh, typename Arc::Weight const &c)
Definition: fstio.hpp:146
bool is_open()
Checks whether the file is open.
Definition: szfstream.hpp:318
void PrintFst(const Fst< Arc > &fst, std::ostream *os)
Templated method that writes an fst in openfst text format to a stream.
Definition: fstio.hpp:35
std::basic_string< unsigned > hyp
Definition: fstio.hpp:143
std::istream * getStream()
Returns internal stream.
Definition: szfstream.hpp:71
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...
Definition: fstio.hpp:170
#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)
Arc::Weight cost
Definition: fstio.hpp:144
#define LERROR(msg)
Hyp(Hyp< Arc > const &h)
Definition: fstio.hpp:154
Wrapper stream class that reads pipes, text files or gzipped files.
Definition: szfstream.hpp:34
bool ends_with(std::string const &haystack, std::string const &needle)
void close()
Closes the file.
Definition: szfstream.hpp:323