HFST - Helsinki Finite-State Transducer Technology API  version 3.7.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ConvertTransducerFormat.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 
13 #ifndef _CONVERT_TRANSDUCER_H_
14 #define _CONVERT_TRANSDUCER_H_
15 
16 #if HAVE_CONFIG_H
17 # include <config.h>
18 #endif // HAVE_CONFIG_H
19 
20 #include <map>
21 #include <iostream>
22 #include <vector>
23 #include <map>
24 
25 #if HAVE_OPENFST
26 #ifdef WINDOWS
27 #include "back-ends/openfstwin/src/include/fst/fstlib.h"
28 #else
29 #include "back-ends/openfst/src/include/fst/fstlib.h"
30 #endif // WINDOWS
31 #endif // HAVE_OPENFST
32 
33 #if HAVE_SFST
34 #include "back-ends/sfst/fst.h"
35 #endif // HAVE_SFST
36 
37 #if HAVE_FOMA
38 #ifndef _FOMALIB_H_
39 #define _FOMALIB_H_
40 #include <stdbool.h>
41 #include "back-ends/foma/fomalib.h"
42 #endif // _FOMALIB_H_
43 #endif // HAVE_FOMA
44 
45 /* Add here your transducer library header (and possibly a guard). */
46 //#if HAVE_MY_TRANSDUCER_LIBRARY
47 //#ifndef _MY_TRANSDUCER_LIBRARY_LIB_H_
48 //#define _MY_TRANSDUCER_LIBRARY_LIB_H_
49 //#include "my_transducer_library/MyTransducerLibrary.h"
50 //#endif // _MY_TRANSDUCER_LIBRARY_LIB_H_
51 //#endif // HAVE_MY_TRANSDUCER_LIBRARY
52 
53 #include "../HfstExceptionDefs.h"
54 #include "optimized-lookup/transducer.h"
55 //#include "HfstConstantTransducer.h"
56 
57 struct fsm;
58 
59 #include "../HfstDataTypes.h"
60 
65 namespace hfst {
66 
67 namespace implementations {
68 
69 #if HAVE_OPENFST
70  typedef fst::StdArc::StateId StateId;
71  typedef fst::ArcIterator<fst::StdVectorFst> StdArcIterator;
72 
73 #if HAVE_OPENFST_LOG
74  typedef fst::ArcTpl<fst::LogWeight> LogArc;
75  typedef fst::VectorFst<LogArc> LogFst;
76 #endif
77 #endif
78 
79 
80  class ConversionFunctions {
81 
82  public:
83 
84  typedef std::map<std::string, unsigned int> String2NumberMap;
85  typedef std::vector<unsigned int> NumberVector;
86 
87  /* A number-to-string vector common to all transducers during a session. */
88  static StringVector number_to_string_vector;
89 
90  /* A string-to-number map common to all transducers during a session. */
91  static String2NumberMap string_to_number_map;
92 
93  /* Get the string that is represented by \a number in the number-to-string
94  vector. If \a number is not found, return the empty string. */
95  static std::string get_string(unsigned int number);
96 
97  /* Get the number that represents \a str in the string-to-number map.
98  If \a str is not found, add it to the next free index. */
99  static unsigned int get_number(const std::string &str);
100 
101  /* Get a vector that tells how a transducer that follows
102  the number-to-symbol encoding of \a coding should be harmonized so that
103  it will follow the one of number_to_string_vector. */
104  static NumberVector get_harmonization_vector
105  (const StringVector &coding_vector);
106 
107  static HfstBasicTransducer * hfst_transducer_to_hfst_basic_transducer
108  (const hfst::HfstTransducer &t);
109 
110 #if HAVE_SFST
111  static void sfst_to_hfst_basic_transducer
112  ( SFST::Node *node,
113  HfstBasicTransducer *net,
114  std::vector<unsigned int> &harmonization_vector);
115 
116  static HfstBasicTransducer * sfst_to_hfst_basic_transducer
117  (SFST::Transducer * t);
118 
119  static SFST::Transducer * hfst_basic_transducer_to_sfst
120  (const HfstBasicTransducer * t);
121 
122  /* static void sfst_to_hfst_fast_transducer
123  ( SFST::Node *node,
124  HfstFastTransducer *net, NumberVector &harmonization_vector);
125 
126  static HfstFastTransducer * sfst_to_hfst_fast_transducer
127  (SFST::Transducer * t);
128 
129  static SFST::Transducer * hfst_fast_transducer_to_sfst
130  (const HfstFastTransducer * t);
131 
132  static void sfst_to_hfst_constant_transducer
133  ( SFST::Node *node,
134  HfstConstantTransducer *net);
135 
136  static HfstConstantTransducer * sfst_to_hfst_constant_transducer
137  (SFST::Transducer * t);
138 
139  static SFST::Transducer * hfst_constant_transducer_to_sfst
140  (const HfstConstantTransducer * t); */
141 #endif // HAVE_SFST
142 
143 #if HAVE_FOMA
144  static HfstBasicTransducer * foma_to_hfst_basic_transducer(struct fsm * t);
145 
146  static struct fsm * hfst_basic_transducer_to_foma
147  (const HfstBasicTransducer * t);
148 
149 
150  /* static HfstFastTransducer * foma_to_hfst_fast_transducer(struct fsm * t);
151 
152  static struct fsm * hfst_fast_transducer_to_foma
153  (const HfstFastTransducer * t);
154 
155 
156  static HfstConstantTransducer * foma_to_hfst_constant_transducer
157  (struct fsm * t);
158 
159  static struct fsm * hfst_constant_transducer_to_foma
160  (const HfstConstantTransducer * t); */
161 #endif // HAVE_FOMA
162 
163 #if HAVE_OPENFST
164  static HfstBasicTransducer * tropical_ofst_to_hfst_basic_transducer
165  (fst::StdVectorFst * t, bool has_hfst_header=true);
166 
167  static StateId hfst_state_to_state_id
168  (HfstState s, std::map<HfstState, StateId> &state_map,
169  fst::StdVectorFst * t);
170 
171  static fst::StdVectorFst * hfst_basic_transducer_to_tropical_ofst
172  (const HfstBasicTransducer * t);
173 
174 
175  /* static HfstFastTransducer * tropical_ofst_to_hfst_fast_transducer
176  (fst::StdVectorFst * t, bool has_hfst_header=true);
177 
178  static fst::StdVectorFst * hfst_fast_transducer_to_tropical_ofst
179  (const HfstFastTransducer * t);
180 
181 
182 
183  static HfstConstantTransducer * tropical_ofst_to_hfst_constant_transducer
184  (fst::StdVectorFst * t, bool has_hfst_header=true);
185 
186  static fst::StdVectorFst * hfst_constant_transducer_to_tropical_ofst
187  (const HfstConstantTransducer * t); */
188 
189 #if HAVE_OPENFST_LOG
190  static HfstBasicTransducer * log_ofst_to_hfst_basic_transducer
191  (LogFst * t, bool had_hfst_header=true);
192 
193  static StateId hfst_state_to_state_id
194  (HfstState s, std::map<HfstState, StateId> &state_map,
195  LogFst * t);
196 
197  static LogFst * hfst_basic_transducer_to_log_ofst
198  (const HfstBasicTransducer * t);
199 
200 
201  /* static HfstFastTransducer * log_ofst_to_hfst_fast_transducer
202  (LogFst * t, bool has_hfst_header=true);
203 
204  static LogFst * hfst_fast_transducer_to_log_ofst
205  (const HfstFastTransducer * t);
206 
207 
208  static HfstConstantTransducer * log_ofst_to_hfst_constant_transducer
209  (LogFst * t, bool had_hfst_header=true);
210 
211  static LogFst * hfst_constant_transducer_to_log_ofst
212  (const HfstConstantTransducer * t); */
213 
214 #endif
215 
216 #endif // HAVE_OPENFST
217 
218 
219 
220  static HfstBasicTransducer * hfst_ol_to_hfst_basic_transducer
221  (hfst_ol::Transducer * t);
222 
223  static hfst_ol::Transducer * hfst_basic_transducer_to_hfst_ol
224  (const HfstBasicTransducer * t, bool weighted,
225  std::string options="", HfstTransducer * harmonizer = NULL);
226 
227  // A way to smuggle a hfst_ol backend into a HfstTransducer wrapper
228  static HfstTransducer * hfst_ol_to_hfst_transducer
229  (hfst_ol::Transducer * t);
230 
231 
232  /* Define here the functions that convert between HfstBasicTransducer and
233  your transducer class. */
234  //#if HAVE_MY_TRANSDUCER_LIBRARY
235  //static HfstBasicTransducer *
236  // my_transducer_library_transducer_to_hfst_basic_transducer
237  // (my_namespace::MyFst * t);
238  //
239  //static my_namespace::MyFst *
240  // hfst_basic_transducer_to_my_transducer_library_transducer
241  // (const HfstBasicTransducer * t);
242  //#endif // HAVE_MY_TRANSDUCER_LIBRARY
243 
244  friend class StringVectorInitializer;
245  friend class String2NumberMapInitializer;
246 
247  };
248 
249  // Initialization of static members in class
250  // ConvertTransducerFormat..
251  class StringVectorInitializer {
252  public:
253  StringVectorInitializer
254  (StringVector &vector) {
255  vector.push_back(std::string("@_EPSILON_SYMBOL_@"));
256  vector.push_back(std::string("@_UNKNOWN_SYMBOL_@"));
257  vector.push_back(std::string("@_IDENTITY_SYMBOL_@"));
258  }
259  };
260 
261  class String2NumberMapInitializer {
262  public:
263  String2NumberMapInitializer
264  (ConversionFunctions::String2NumberMap &map) {
265  map["@_EPSILON_SYMBOL_@"] = 0;
266  map["@_UNKNOWN_SYMBOL_@"] = 1;
267  map["@_IDENTITY_SYMBOL_@"] = 2;
268  }
269  };
270 
271 } }
272 #endif // _CONVERT_TRANSDUCER_H_
273 
A compiled transducer format, suitable for fast lookup operations.
Definition: 3831_transducer.h:804
unsigned int HfstState
The number of a state in an HfstTransitionGraph.
Definition: HfstDataTypes.h:120
A synchronous finite-state transducer.
Definition: HfstTransducer.h:227
HfstTransitionGraph< HfstTropicalTransducerTransitionData > HfstBasicTransducer
An HfstTransitionGraph with transitions of type HfstTropicalTransducerTransitionData and weight type ...
Definition: HfstDataTypes.h:114