HFST - Helsinki Finite-State Transducer Technology API  version 3.7.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
HfstInputStream.h
Go to the documentation of this file.
1 // This program is free software: you can redistribute it and/or modify
2 // it under the terms of the GNU General Public License as published by
3 // the Free Software Foundation, version 3 of the License.
4 //
5 // This program is distributed in the hope that it will be useful,
6 // but WITHOUT ANY WARRANTY; without even the implied warranty of
7 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8 // GNU General Public License for more details.
9 //
10 // You should have received a copy of the GNU General Public License
11 // along with this program. If not, see <http://www.gnu.org/licenses/>.
12 #ifndef _HFST_INPUTSTREAM_H_
13 #define _HFST_INPUTSTREAM_H_
14 
15 #if HAVE_CONFIG_H
16 # include <config.h>
17 #endif
18 
19 #include "HfstDataTypes.h"
20 
25 namespace hfst
26 {
27 
28  namespace implementations {
29 #if HAVE_OPENFST
30  class LogWeightInputStream;
31 #if HAVE_OPENFST_LOG
32  class TropicalWeightInputStream;
33 #endif
34 #endif
35 #if HAVE_SFST
36  class SfstInputStream;
37 #endif
38 #if HAVE_FOMA
39  class FomaInputStream;
40 #endif
41 #if HAVE_MY_TRANSDUCER_LIBRARY
42  class MyTransducerLibraryInputStream;
43 #endif
44  class HfstOlInputStream;
45  }
46 
47 
95  {
96  protected:
97 
98  union StreamImplementation
99  {
100 #if HAVE_SFST
101  hfst::implementations::SfstInputStream * sfst;
102 #endif
103 #if HAVE_OPENFST
104  hfst::implementations::TropicalWeightInputStream * tropical_ofst;
105 #if HAVE_OPENFST_LOG
106  hfst::implementations::LogWeightInputStream * log_ofst;
107 #endif
108 #endif
109 #if HAVE_FOMA
110  hfst::implementations::FomaInputStream * foma;
111 #endif
112 
113 #if HAVE_MY_TRANSDUCER_LIBRARY
115  my_transducer_library;
116 #endif
117 
118  hfst::implementations::HfstOlInputStream * hfst_ol;
119  };
120 
121  /* The backend implementation */
122  StreamImplementation implementation;
123  /* Implementation type */
124  ImplementationType type;
125  /* Name of next transducer, given in the hfst header */
126  std::string name;
127  std::map<std::string,std::string> props;
128  /* How many bytes have been already read by the function
129  when processing the hfst header */
130  unsigned int bytes_to_skip;
131  /* The name of the file, if stdin, name is "" */
132  std::string filename;
133  /* Whether the current transducer has an hfst header */
134  bool has_hfst_header;
135 
136  /* A special case where an OpenFst transducer has no symbol tables but an
137  SFST alphabet is appended at the end. Should not occur very often, but
138  possible when converting old transducers into version 3.0. transducers */
139  bool hfst_version_2_weighted_transducer;
140 
141  /* The stream that the reading operations use when reading the first
142  transducer. Then the type of the transducer is not known so there
143  is no backend implementation whose reading functions could be used.
144  If input_stream==NULL, the backend implementation is used */
145  std::istream * input_stream;
146 
147  /* Basic stream operators, work on input_stream (if not NULL) or on
148  the stream implementation. */
149 
150  /* Extract one character from the stream */
151  char stream_get();
152 
153  /* Extract one character from the stream and store it in @a c. */
154  char &stream_get(char &c);
155 
156  /* Extract one short from the stream and store it in @a i. */
157  short &stream_get(short &i);
158 
159  /* Extract one unsigned short from the stream and store it in @a i. */
160  unsigned short &stream_get(unsigned short &i);
161 
162  /* Return character c to the stream */
163  void stream_unget(char c);
164  /* Whether the stream is at end */
165  bool stream_eof();
166  /* Get a string from the stream */
167  std::string stream_getstring();
168  /* Return the next character in the stream without extracting it */
169  char stream_peek();
170  /* The stream implementation ignores n bytes. */
171  void ignore(unsigned int n);
172 
173  /* The type of a transducer not supported directly by HFST version 3.0
174  but which can occur in conversion functions. */
175  enum TransducerType {
176  /* See the above variable. */
177  HFST_VERSION_2_WEIGHTED,
178  /* An SFST transducer with no alphabet, not supported. */
179  HFST_VERSION_2_UNWEIGHTED_WITHOUT_ALPHABET,
180  /* Old header + ordinary SFST transducer. */
181  HFST_VERSION_2_UNWEIGHTED,
182  /* An OpenFst transducer, can cause problems if it does not have
183  symbol tables. */
184  OPENFST_TROPICAL_,
185  OPENFST_LOG_,
186  /* An SFST transducer. */
187  SFST_,
188  /* A foma transducer. */
189  FOMA_,
190  /* Your transducer type */
191  //MY_TRANSDUCER_LIBRARY_,
192  /* Transducer type not recognized. */
193  ERROR_TYPE_
194  };
195 
196  /* Read a transducer from the stream. */
197  void read_transducer(HfstTransducer &t);
198  /* Type of next transducer in the stream. */
199  ImplementationType stream_fst_type();
200 
201  // methods used by function stream_fst_type
202  TransducerType guess_fst_type(int &bytes_read);
203  bool read_hfst_header(int &bytes_read);
204  bool read_library_header(int &bytes_read);
205  int get_header_size(int &bytes_read);
206  StringPairVector get_header_data(int header_size);
207  void process_header_data
208  (StringPairVector &header_data, bool warnings=false);
209  bool set_implementation_specific_header_data
210  (StringPairVector &data, unsigned int index);
211 
212 
213  bool read_library_header_old(int &bytes_read);
214  ImplementationType get_fst_type_old(int &bytes_read);
215 
216  public:
217 
226  HfstInputStream(void);
227 
239  HfstInputStream(const std::string &filename);
240 
242  ~HfstInputStream(void);
243 
247  void close(void);
248 
250  bool is_eof(void);
252  bool is_bad(void);
254  bool is_good(void);
255 
261  ImplementationType get_type(void) const;
262 
263  friend class HfstTransducer;
264  };
265 
266 
267 }
268 
269 
270 
271 #endif
void close(void)
Close the stream.
Definition: HfstInputStream.cc:1093
std::vector< std::pair< std::string, std::string > > StringPairVector
A vector of string pairs.
Definition: HfstDataTypes.h:106
A synchronous finite-state transducer.
Definition: HfstTransducer.h:227
ImplementationType
The type of an HfstTransducer.
Definition: HfstDataTypes.h:43
bool is_eof(void)
Whether the stream is at end.
Definition: HfstInputStream.cc:1131
bool is_bad(void)
Whether badbit is set.
Definition: HfstInputStream.cc:1169
HfstInputStream(void)
Create a stream to standard input for reading binary transducers.
Definition: HfstInputStream.cc:905
A skeleton class for reading a new type of binary transducers from a stream.
Definition: MyTransducerLibraryTransducer.h:59
ImplementationType get_type(void) const
The type of the first transducer in the stream.
Definition: HfstInputStream.cc:1247
Datatypes that are needed when using the HFST API.
A stream for reading HFST binary transducers.
Definition: HfstInputStream.h:94
~HfstInputStream(void)
Destructor.
Definition: HfstInputStream.cc:1053
bool is_good(void)
Whether the state of the stream is good for input operations.
Definition: HfstInputStream.cc:1208