HFST - Helsinki Finite-State Transducer Technology - C++ API  version 3.9.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ConvertTransducerFormat.h
Go to the documentation of this file.
1 // Copyright (c) 2016 University of Helsinki
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 3 of the License, or (at your option) any later version.
7 // See the file COPYING included with this distribution for more
8 // information.
9 
10 #ifndef _CONVERT_TRANSDUCER_H_
11 #define _CONVERT_TRANSDUCER_H_
12 
13 #if HAVE_CONFIG_H
14 # include <config.h>
15 #endif // HAVE_CONFIG_H
16 
17 #include <map>
18 #include <iostream>
19 #include <vector>
20 #include <map>
21 
22 #if HAVE_OPENFST
23 #ifdef _MSC_VER
24 #include "back-ends/openfstwin/src/include/fst/fstlib.h"
25 #else
26 #include "back-ends/openfst/src/include/fst/fstlib.h"
27 #endif // _MSC_VER
28 #endif // HAVE_OPENFST
29 
30 #if HAVE_SFST
31 #include "back-ends/sfst/fst.h"
32 #endif // HAVE_SFST
33 
34 #if HAVE_FOMA
35 #ifndef _FOMALIB_H_
36 #define _FOMALIB_H_
37 #include "back-ends/foma/fomalib.h"
38 #endif // _FOMALIB_H_
39 #endif // HAVE_FOMA
40 
41 #if HAVE_XFSM
42 #include "xfsm/xfsm_api.h"
43 #endif
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_XFSM
164  static HfstBasicTransducer * xfsm_to_hfst_basic_transducer(NETptr t);
165  static NETptr hfst_basic_transducer_to_xfsm(const HfstBasicTransducer * t);
166 #endif // HAVE_XFSM
167 
168 #if HAVE_OPENFST
169  static HfstBasicTransducer * tropical_ofst_to_hfst_basic_transducer
170  (fst::StdVectorFst * t, bool has_hfst_header=true);
171 
172  static StateId hfst_state_to_state_id
173  (HfstState s, std::map<HfstState, StateId> &state_map,
174  fst::StdVectorFst * t);
175 
176  static fst::StdVectorFst * hfst_basic_transducer_to_tropical_ofst
177  (const HfstBasicTransducer * t);
178 
179 
180  /* static HfstFastTransducer * tropical_ofst_to_hfst_fast_transducer
181  (fst::StdVectorFst * t, bool has_hfst_header=true);
182 
183  static fst::StdVectorFst * hfst_fast_transducer_to_tropical_ofst
184  (const HfstFastTransducer * t);
185 
186 
187 
188  static HfstConstantTransducer * tropical_ofst_to_hfst_constant_transducer
189  (fst::StdVectorFst * t, bool has_hfst_header=true);
190 
191  static fst::StdVectorFst * hfst_constant_transducer_to_tropical_ofst
192  (const HfstConstantTransducer * t); */
193 
194 #if HAVE_OPENFST_LOG
195  static HfstBasicTransducer * log_ofst_to_hfst_basic_transducer
196  (LogFst * t, bool had_hfst_header=true);
197 
198  static StateId hfst_state_to_state_id
199  (HfstState s, std::map<HfstState, StateId> &state_map,
200  LogFst * t);
201 
202  static LogFst * hfst_basic_transducer_to_log_ofst
203  (const HfstBasicTransducer * t);
204 
205 
206  /* static HfstFastTransducer * log_ofst_to_hfst_fast_transducer
207  (LogFst * t, bool has_hfst_header=true);
208 
209  static LogFst * hfst_fast_transducer_to_log_ofst
210  (const HfstFastTransducer * t);
211 
212 
213  static HfstConstantTransducer * log_ofst_to_hfst_constant_transducer
214  (LogFst * t, bool had_hfst_header=true);
215 
216  static LogFst * hfst_constant_transducer_to_log_ofst
217  (const HfstConstantTransducer * t); */
218 
219 #endif
220 
221 #endif // HAVE_OPENFST
222 
223 
224 
225  static HfstBasicTransducer * hfst_ol_to_hfst_basic_transducer
226  (hfst_ol::Transducer * t);
227 
228  static hfst_ol::Transducer * hfst_basic_transducer_to_hfst_ol
229  (const HfstBasicTransducer * t, bool weighted,
230  std::string options="", HfstTransducer * harmonizer = NULL);
231 
232  // A way to smuggle a hfst_ol backend into a HfstTransducer wrapper
233  static HfstTransducer * hfst_ol_to_hfst_transducer
234  (hfst_ol::Transducer * t);
235 
236 
237  /* Define here the functions that convert between HfstBasicTransducer and
238  your transducer class. */
239  //#if HAVE_MY_TRANSDUCER_LIBRARY
240  //static HfstBasicTransducer *
241  // my_transducer_library_transducer_to_hfst_basic_transducer
242  // (my_namespace::MyFst * t);
243  //
244  //static my_namespace::MyFst *
245  // hfst_basic_transducer_to_my_transducer_library_transducer
246  // (const HfstBasicTransducer * t);
247  //#endif // HAVE_MY_TRANSDUCER_LIBRARY
248 
249  friend class StringVectorInitializer;
250  friend class String2NumberMapInitializer;
251 
252  };
253 
254  // Initialization of static members in class
255  // ConvertTransducerFormat..
256  class StringVectorInitializer {
257  public:
258  StringVectorInitializer
259  (StringVector &vector) {
260  vector.push_back(std::string("@_EPSILON_SYMBOL_@"));
261  vector.push_back(std::string("@_UNKNOWN_SYMBOL_@"));
262  vector.push_back(std::string("@_IDENTITY_SYMBOL_@"));
263  }
264  };
265 
266  class String2NumberMapInitializer {
267  public:
268  String2NumberMapInitializer
269  (ConversionFunctions::String2NumberMap &map) {
270  map["@_EPSILON_SYMBOL_@"] = 0;
271  map["@_UNKNOWN_SYMBOL_@"] = 1;
272  map["@_IDENTITY_SYMBOL_@"] = 2;
273  }
274  };
275 
276 } }
277 #endif // _CONVERT_TRANSDUCER_H_
278 
A compiled transducer format, suitable for fast lookup operations.
Definition: transducer.h:833
unsigned int HfstState
The number of a state in an HfstTransitionGraph.
Definition: HfstDataTypes.h:119
A synchronous finite-state transducer.
Definition: HfstTransducer.h:253
HfstTransitionGraph< HfstTropicalTransducerTransitionData > HfstBasicTransducer
An HfstTransitionGraph with transitions of type HfstTropicalTransducerTransitionData and weight type ...
Definition: HfstDataTypes.h:113