12 #ifndef _TROPICAL_WEIGHT_TRANSDUCER_H_
13 #define _TROPICAL_WEIGHT_TRANSDUCER_H_
20 #include "back-ends/openfstwin/src/include/fst/fstlib.h"
22 #include "../../../config.h"
23 #include "back-ends/openfst/src/include/fst/fstlib.h"
26 #include "HfstExtractStrings.h"
38 namespace implementations
42 typedef StdArc::StateId StateId;
44 typedef std::vector<StdArc> StdArcVector;
45 struct StdArcLessThan {
46 bool operator() (
const StdArc &arc1,
const StdArc &arc2)
const; };
49 using std::ostringstream;
50 using std::stringstream;
52 void openfst_tropical_set_hopcroft(
bool value);
54 class TropicalWeightInputStream
59 istream &input_stream;
60 void skip_identifier_version_3_0(
void);
61 void skip_hfst_header(
void);
63 TropicalWeightInputStream(
void);
64 TropicalWeightInputStream(
const std::string &filename);
66 bool is_eof(
void)
const;
67 bool is_bad(
void)
const;
68 bool is_good(
void)
const;
69 bool is_fst(
void)
const;
70 bool operator() (
void)
const;
71 void ignore(
unsigned int);
72 StdVectorFst * read_transducer();
75 short stream_get_short();
76 void stream_unget(
char c);
78 static bool is_fst(FILE * f);
79 static bool is_fst(istream &s);
82 class TropicalWeightOutputStream
87 ostream &output_stream;
91 TropicalWeightOutputStream(
bool hfst_format=
true);
92 TropicalWeightOutputStream
93 (
const std::string &filename,
bool hfst_format=
false);
95 void write(
const char &c);
96 void write_transducer(StdVectorFst * transducer);
99 class TropicalWeightTransitionIterator;
101 typedef StateId TropicalWeightState;
103 class TropicalWeightStateIterator
106 StateIterator<StdVectorFst> * iterator;
108 TropicalWeightStateIterator(StdVectorFst * t);
109 ~TropicalWeightStateIterator(
void);
112 TropicalWeightState value(
void);
116 class TropicalWeightTransition
122 TropicalWeightTransition(
const StdArc &arc, StdVectorFst *t);
123 ~TropicalWeightTransition(
void);
124 std::string get_input_symbol(
void)
const;
125 std::string get_output_symbol(
void)
const;
126 TropicalWeightState get_target_state(
void)
const;
127 TropicalWeight get_weight(
void)
const;
131 class TropicalWeightTransitionIterator
134 ArcIterator<StdVectorFst> * arc_iterator;
137 TropicalWeightTransitionIterator(StdVectorFst * t, StateId state);
138 ~TropicalWeightTransitionIterator(
void);
141 TropicalWeightTransition value(
void);
145 class TropicalWeightTransducer
148 static StdVectorFst * create_empty_transducer(
void);
149 static StdVectorFst * create_epsilon_transducer(
void);
152 static StdVectorFst * define_transducer(
const std::string &symbol);
153 static StdVectorFst * define_transducer
154 (
const std::string &isymbol,
const std::string &osymbol);
155 static StdVectorFst * define_transducer
157 static StdVectorFst * define_transducer
159 static StdVectorFst * define_transducer
160 (
const std::vector<StringPairSet> &spsv);
163 static StdVectorFst * define_transducer(
unsigned int number);
164 static StdVectorFst * define_transducer
165 (
unsigned int inumber,
unsigned int onumber);
166 static StdVectorFst * define_transducer
167 (
const hfst::NumberPairVector &npv);
168 static StdVectorFst * define_transducer
169 (
const hfst::NumberPairSet &nps,
bool cyclic=
false);
170 static StdVectorFst * define_transducer
171 (
const std::vector<NumberPairSet> &npsv);
173 static StdVectorFst * copy(StdVectorFst * t);
174 static StdVectorFst * determinize(StdVectorFst * t);
175 static StdVectorFst * minimize(StdVectorFst * t);
176 static StdVectorFst * remove_epsilons(StdVectorFst * t);
177 static StdVectorFst * n_best(StdVectorFst * t,
unsigned int n);
178 static StdVectorFst * prune(StdVectorFst * t);
179 static StdVectorFst * repeat_star(StdVectorFst * t);
180 static StdVectorFst * repeat_plus(StdVectorFst * t);
181 static StdVectorFst * repeat_n(StdVectorFst * t,
unsigned int n);
182 static StdVectorFst * repeat_le_n(StdVectorFst * t,
unsigned int n);
183 static StdVectorFst * optionalize(StdVectorFst * t);
184 static StdVectorFst * invert(StdVectorFst * t);
185 static StdVectorFst * reverse(StdVectorFst * transducer);
186 static StdVectorFst * extract_input_language(StdVectorFst * t);
187 static StdVectorFst * extract_output_language(StdVectorFst * t);
188 static void extract_paths
189 (StdVectorFst * t, hfst::ExtractStringsCb& callback,
190 int cycles=-1, FdTable<int64>* fd=NULL,
bool filter_fd=
false
193 static void extract_random_paths
196 static void extract_random_paths_fd
199 static StdVectorFst * compose(StdVectorFst * t1,
201 static StdVectorFst * concatenate(StdVectorFst * t1,
203 static StdVectorFst * disjunct(StdVectorFst * t1,
206 static StdVectorFst * disjunct
208 static StdVectorFst * disjunct
209 (StdVectorFst * t,
const NumberPairVector &npv);
211 static StdVectorFst * disjunct_as_tries(StdVectorFst * t1,
212 const StdVectorFst * t2);
214 static StdVectorFst * intersect(StdVectorFst * t1,
216 static StdVectorFst * subtract(StdVectorFst * t1,
218 static StdVectorFst * set_weight(StdVectorFst * t,
float f);
219 static StdVectorFst * set_final_weights(StdVectorFst * t,
float weight,
bool increment=
false);
220 static StdVectorFst * transform_weights
221 (StdVectorFst * t,
float (*func)(
float f));
222 static StdVectorFst * push_weights
223 (StdVectorFst * t,
bool to_initial_state);
225 static std::pair<StdVectorFst*, StdVectorFst*> harmonize
226 (StdVectorFst *t1, StdVectorFst *t2,
bool unknown_symbols_in_use=
true);
228 static void write_in_att_format(StdVectorFst * t, FILE *ofile);
229 static void write_in_att_format_number(StdVectorFst * t, FILE *ofile);
233 static void write_in_att_format(StdVectorFst * t, std::ostream &os);
234 static void write_in_att_format_number
235 (StdVectorFst * t, std::ostream &os);
237 static StdVectorFst * read_in_att_format(FILE *ifile);
239 static bool are_equivalent(StdVectorFst *one, StdVectorFst *another);
240 static bool is_cyclic(StdVectorFst * t);
241 static bool is_automaton(StdVectorFst * t);
243 static FdTable<int64>* get_flag_diacritics(StdVectorFst * t);
245 static void print_alphabet(
const StdVectorFst *t);
248 static StdVectorFst * insert_freely
249 (StdVectorFst * t,
const StringPair &symbol_pair);
250 static StdVectorFst * substitute
251 (StdVectorFst * t, std::string old_symbol, std::string new_symbol);
252 static StdVectorFst * substitute(StdVectorFst * t,
255 static StdVectorFst * substitute(StdVectorFst * t,
258 static StdVectorFst * substitute(StdVectorFst * t,
260 StdVectorFst *transducer);
263 static StdVectorFst * insert_freely
264 (StdVectorFst * t,
const NumberPair &number_pair);
265 static StdVectorFst * substitute
266 (StdVectorFst * t,
unsigned int,
unsigned int);
267 static StdVectorFst * substitute(StdVectorFst * t,
268 NumberPair old_number_pair,
269 NumberPair new_number_pair);
270 static StdVectorFst * substitute(StdVectorFst * t,
271 const NumberPair old_number_pair,
272 StdVectorFst *transducer);
274 static void insert_to_alphabet
275 (StdVectorFst *t,
const std::string &symbol);
276 static void remove_from_alphabet
277 (StdVectorFst *t,
const std::string &symbol);
278 static StringSet get_alphabet(StdVectorFst *t);
279 static void get_first_input_symbols
280 (StdVectorFst *t, StateId s, std::set<StateId> & visited_states, StringSet & symbols);
281 static StringSet get_first_input_symbols(StdVectorFst *t);
282 static unsigned int get_symbol_number(StdVectorFst *t,
283 const std::string &symbol);
284 static unsigned int get_biggest_symbol_number(StdVectorFst *t);
285 static StringVector get_symbol_vector(StdVectorFst *t);
287 static NumberNumberMap create_mapping
288 (StdVectorFst * t1, StdVectorFst * t2);
289 static void recode_symbol_numbers
290 (StdVectorFst * t, hfst::NumberNumberMap &km);
291 static StdVectorFst * expand_arcs
292 (StdVectorFst * t, hfst::StringSet &unknown,
293 bool unknown_symbols_in_use);
296 static StdVectorFst * compose_intersect(StdVectorFst * t,
300 float get_profile_seconds();
302 static unsigned int number_of_states(
const StdVectorFst * t);
303 static unsigned int number_of_arcs(
const StdVectorFst * t);
306 static void set_symbol_table
308 std::vector<std::pair<unsigned short, std::string> > symbol_mappings);
311 static fst::SymbolTable create_symbol_table(std::string name);
312 static void initialize_symbol_tables(StdVectorFst *t);
313 static void remove_symbol_table(StdVectorFst *t);
317 typedef std::map<int, StateId> StateMap;
318 static StateId add_and_map_state(StdVectorFst *t,
int state_number,
319 StateMap &state_map);
321 static int has_arc(StdVectorFst &t,
322 StdArc::StateId sourcestate,
323 StdArc::Label ilabel,
324 StdArc::Label olabel);
325 static void disjunct_as_tries(StdVectorFst &t1,
327 const StdVectorFst * t2,
329 static void add_sub_trie(StdVectorFst &t1,
331 const StdVectorFst * t2,
335 static StateId add_state(StdVectorFst *t);
336 static void set_final_weight(StdVectorFst *t, StateId s,
float w);
337 static void add_transition
338 (StdVectorFst *t, StateId source,
339 std::string &isymbol, std::string &osymbol,
float w, StateId target);
340 static float get_final_weight(StdVectorFst *t, StateId s);
341 static float is_final(StdVectorFst *t, StateId s);
342 static StateId get_initial_state(StdVectorFst *t);
343 static void represent_empty_transducer_as_having_one_state
std::pair< String, String > StringPair
A symbol pair in a transition.
Definition: HfstSymbolDefs.h:71
std::vector< std::pair< std::string, std::string > > StringPairVector
A vector of string pairs.
Definition: HfstDataTypes.h:106
Class declarations for flag diacritic handling.
std::set< HfstTwoLevelPath > HfstTwoLevelPaths
A set of two-level weighted paths.
Definition: HfstDataTypes.h:110
std::set< StringPair > StringPairSet
A set of symbol pairs used in substituting symbol pairs and in rule functions.
Definition: HfstSymbolDefs.h:83
Typedefs and functions for symbols, symbol pairs and sets of symbols.