1 # See www.openfst.org for extensive documentation on this weighted
2 # finite-state transducer library.
5 from libc.time cimport time
6 from libc.time cimport time_t
8 from libcpp cimport bool
9 from libcpp.memory cimport shared_ptr
10 from libcpp.memory cimport unique_ptr
11 from libcpp.utility cimport pair
12 from libcpp.vector cimport vector
14 from basictypes cimport int32
15 from basictypes cimport int64
16 from basictypes cimport uint32
17 from basictypes cimport uint64
19 from ios cimport stringstream
20 from libcpp.string cimport string
23 # Exportable helper functions
26 cdef string tostring(data, encoding=?) except *
28 cdef string weighttostring(data, encoding=?) except *
30 cdef fst.ComposeFilter _get_compose_filter(
31 const string &compose_filter) except *
33 cdef fst.DeterminizeType _get_determinize_type(const string &det_type) except *
35 cdef fst.QueueType _get_queue_type(const string &queue_type) except *
37 cdef fst.RandArcSelection _get_rand_arc_selection(
38 const string &replace_label_type) except *
40 cdef fst.ReplaceLabelType _get_replace_label_type(
41 const string &replace_label_type, bool epsilon_on_replace) except *
47 cdef fst.WeightClass _get_WeightClass_or_One(const string &weight_type,
48 weight_string) except *
50 cdef fst.WeightClass _get_WeightClass_or_Zero(const string &weight_type,
51 weight_string) except *
54 cdef class Weight(object):
56 cdef unique_ptr[fst.WeightClass] _weight
58 cdef void _check_weight(self) except *
60 cpdef Weight copy(self)
62 cpdef string to_string(self)
64 cpdef string type(self)
67 cdef Weight _Weight_Zero(weight_type)
69 cdef Weight _Weight_One(weight_type)
71 cdef Weight _Weight_NoWeight(weight_type)
73 cdef Weight _plus(Weight lhs, Weight rhs)
75 cdef Weight _times(Weight lhs, Weight rhs)
77 cdef Weight _divide(Weight lhs, Weight rhs)
79 cdef Weight _power(Weight lhs, size_t n)
84 ctypedef fst.SymbolTable * SymbolTable_ptr
87 cdef class _SymbolTable(object):
89 cdef fst.SymbolTable *_table
91 cpdef int64 available_key(self)
93 cpdef string checksum(self)
95 cpdef SymbolTable copy(self)
97 cpdef int64 get_nth_key(self, ssize_t pos) except *
99 cpdef string labeled_checksum(self)
101 cpdef bool member(self, key)
103 cpdef string name(self)
105 cpdef size_t num_symbols(self)
107 cpdef void write(self, filename) except *
109 cpdef void write_text(self, filename) except *
112 cdef class _EncodeMapperSymbolTable(_SymbolTable):
114 cdef shared_ptr[fst.EncodeMapperClass] _encoder
117 cdef class _FstSymbolTable(_SymbolTable):
119 cdef shared_ptr[fst.FstClass] _fst
122 cdef class _MutableSymbolTable(_SymbolTable):
124 cpdef int64 add_symbol(self, symbol, int64 key=?)
126 cpdef void add_table(self, _SymbolTable syms)
128 cpdef void set_name(self, new_name) except *
131 cdef class _MutableFstSymbolTable(_MutableSymbolTable):
133 cdef shared_ptr[fst.MutableFstClass] _mfst
136 cdef class SymbolTable(_MutableSymbolTable):
138 cdef unique_ptr[fst.SymbolTable] _smart_table
141 cdef _EncodeMapperSymbolTable _init_EncodeMapperSymbolTable(
142 fst.SymbolTable *table, shared_ptr[fst.EncodeMapperClass] encoder)
145 cdef _FstSymbolTable _init_FstSymbolTable(fst.SymbolTable *table,
146 shared_ptr[fst.FstClass] ifst)
149 cdef _MutableFstSymbolTable _init_MutableFstSymbolTable(fst.SymbolTable *table,
150 shared_ptr[fst.MutableFstClass] ifst)
153 cdef SymbolTable _init_SymbolTable(fst.SymbolTable *table)
157 cdef class SymbolTableIterator(object):
159 cdef shared_ptr[fst.SymbolTable] _table
160 cdef unique_ptr[fst.SymbolTableIterator] _siter
162 cpdef bool done(self)
164 cpdef void next(self)
166 cpdef void reset(self)
168 cpdef string symbol(self)
170 cpdef int64 value(self)
176 cdef class EncodeMapper(object):
178 cdef shared_ptr[fst.EncodeMapperClass] _encoder
180 cpdef string arc_type(self)
182 cpdef uint32 flags(self)
184 cpdef _EncodeMapperSymbolTable input_symbols(self)
186 cpdef _EncodeMapperSymbolTable output_symbols(self)
188 cpdef uint64 properties(self, uint64 mask)
190 cpdef void set_input_symbols(self, _SymbolTable syms) except *
192 cpdef void set_output_symbols(self, _SymbolTable syms) except *
194 cpdef string weight_type(self)
200 ctypedef fst.FstClass * FstClass_ptr
201 ctypedef fst.MutableFstClass * MutableFstClass_ptr
202 ctypedef fst.VectorFstClass * VectorFstClass_ptr
205 cdef class _Fst(object):
207 cdef shared_ptr[fst.FstClass] _fst
209 cpdef string arc_type(self)
211 cpdef ArcIterator arcs(self, int64 state)
213 cpdef _Fst copy(self)
215 cpdef void draw(self, filename, _SymbolTable isymbols=?,
216 _SymbolTable osymbols=?, SymbolTable ssymbols=?,
217 bool acceptor=?, title=?, double width=?,
218 double height=?, bool portrait=?, bool vertical=?,
219 double ranksep=?, double nodesep=?, int32 fontsize=?,
220 int32 precision=?, float_format=?,
221 bool show_weight_one=?)
223 cpdef Weight final(self, int64 state)
225 cpdef string fst_type(self)
227 cpdef _FstSymbolTable input_symbols(self)
229 cpdef size_t num_arcs(self, int64 state) except *
231 cpdef size_t num_input_epsilons(self, int64 state) except *
233 cpdef size_t num_output_epsilons(self, int64 state) except *
235 cpdef _FstSymbolTable output_symbols(self)
237 cpdef uint64 properties(self, uint64 mask, bool test)
239 cpdef int64 start(self)
241 cpdef StateIterator states(self)
243 cpdef string text(self, _SymbolTable isymbols=?, _SymbolTable osymbols=?,
244 _SymbolTable ssymbols=?, bool acceptor=?,
245 bool show_weight_one=?, missing_sym=?)
247 cpdef bool verify(self)
249 cpdef string weight_type(self)
251 cpdef void write(self, filename) except *
253 cpdef string write_to_string(self)
256 cdef class _MutableFst(_Fst):
258 cdef shared_ptr[fst.MutableFstClass] _mfst
260 cdef void _check_mutating_imethod(self) except *
262 cdef void _add_arc(self, int64 state, Arc arc) except *
264 cpdef int64 add_state(self) except *
266 cdef void _arcsort(self, sort_type=?) except *
268 cdef void _closure(self, bool closure_plus=?) except *
270 cdef void _concat(self, _Fst ifst) except *
272 cdef void _connect(self) except *
274 cdef void _decode(self, EncodeMapper) except *
276 cdef void _delete_arcs(self, int64 state, size_t n=?) except *
278 cdef void _delete_states(self, states=?) except *
280 cdef void _encode(self, EncodeMapper) except *
282 cdef void _invert(self) except *
284 cdef void _minimize(self, float delta=?, bool allow_nondet=?) except *
286 cpdef MutableArcIterator mutable_arcs(self, int64 state)
288 cpdef int64 num_states(self)
290 cdef void _project(self, bool project_output=?) except *
292 cdef void _prune(self, float delta=?, int64 nstate=?, weight=?) except *
294 cdef void _push(self, float delta=?, bool remove_total_weight=?,
295 bool to_final=?) except *
297 cdef void _relabel_pairs(self, ipairs=?, opairs=?) except *
299 cdef void _relabel_tables(self, _SymbolTable old_isymbols=?,
300 _SymbolTable new_isymbols=?, unknown_isymbol=?,
301 bool attach_new_isymbols=?,
302 _SymbolTable old_osymbols=?, _SymbolTable new_osymbols=?,
303 unknown_osymbol=?, bool attach_new_osymbols=?) except *
305 cdef void _reserve_arcs(self, int64 state, size_t n) except *
307 cdef void _reserve_states(self, int64 n) except *
309 cdef void _reweight(self, potentials, bool to_final=?) except *
311 cdef void _rmepsilon(self, bool connect=?, float delta=?, int64 nstate=?,
314 cdef void _set_final(self, int64 state, weight=?) except *
316 cdef void _set_properties(self, uint64 props, uint64 mask)
318 cdef void _set_start(self, int64 state) except *
320 cdef void _set_input_symbols(self, _SymbolTable syms) except *
322 cdef void _set_output_symbols(self, _SymbolTable syms) except *
324 cdef void _topsort(self) except *
326 cdef void _union(self, _Fst ifst) except *
329 # Fst construction helpers.
332 cdef _Fst _init_Fst(FstClass_ptr tfst)
334 cdef _MutableFst _init_MutableFst(MutableFstClass_ptr tfst)
336 cdef _Fst _init_XFst(FstClass_ptr tfst)
338 cdef _MutableFst _create_Fst(arc_type=?)
340 cdef _Fst _read_Fst(filename, fst_type=?)
342 cdef _Fst _deserialize_Fst(fst_string, fst_type=?)
348 cdef class Arc(object):
350 cdef unique_ptr[fst.ArcClass] _arc
355 cdef Arc _init_Arc(const fst.ArcClass &arc)
358 cdef class ArcIterator(object):
360 cdef shared_ptr[fst.FstClass] _fst
361 cdef unique_ptr[fst.ArcIteratorClass] _aiter
363 cpdef bool done(self)
365 cpdef uint32 flags(self)
367 cpdef void next(self)
369 cpdef size_t position(self)
371 cpdef void reset(self)
373 cpdef void seek(self, size_t a)
375 cpdef void set_flags(self, uint32 flags, uint32 mask)
377 cpdef object value(self)
380 cdef class MutableArcIterator(object):
382 cdef shared_ptr[fst.MutableFstClass] _mfst
383 cdef unique_ptr[fst.MutableArcIteratorClass] _aiter
385 cpdef bool done(self)
387 cpdef uint32 flags(self)
389 cpdef void next(self)
391 cpdef size_t position(self)
393 cpdef void reset(self)
395 cpdef void seek(self, size_t a)
397 cpdef void set_flags(self, uint32 flags, uint32 mask)
399 cpdef void set_value(self, Arc arc)
401 cpdef object value(self)
404 cdef class StateIterator(object):
406 cdef shared_ptr[fst.FstClass] _fst
407 cdef unique_ptr[fst.StateIteratorClass] _siter
409 cpdef bool done(self)
411 cpdef void next(self)
413 cpdef void reset(self)
415 cpdef int64 value(self)
418 # Constructive operations on Fst.
421 cdef _Fst _map(_Fst ifst, float delta=?, map_type=?, weight=?)
423 cpdef _Fst arcmap(_Fst ifst, float delta=?, map_type=?, weight=?)
425 cpdef _MutableFst compose(_Fst ifst1, _Fst ifst2, compose_filter=?,
428 cpdef _Fst convert(_Fst ifst, fst_type=?)
430 cpdef _MutableFst determinize(_Fst ifst, float delta=?, det_type=?,
431 int64 nstate=?, int64 subsequential_label=?,
432 weight=?, bool increment_subsequential_label=?)
434 cpdef _MutableFst difference(_Fst ifst1, _Fst ifst2, compose_filter=?,
437 cpdef _MutableFst disambiguate(_Fst ifst, float delta=?, int64 nstate=?,
438 int64 subsequential_label=?, weight=?)
440 cpdef _MutableFst epsnormalize(_Fst ifst, bool eps_norm_output=?)
442 cpdef bool equal(_Fst ifst1, _Fst ifst2, float delta=?)
444 cpdef bool equivalent(_Fst ifst1, _Fst ifst2, float delta=?) except *
446 cpdef _MutableFst intersect(_Fst ifst1, _Fst ifst2, compose_filter=?,
449 cpdef bool isomorphic(_Fst ifst1, _Fst ifst2, float delta=?)
451 cpdef _MutableFst prune(_Fst ifst, float delta=?, int64 nstate=?,
454 cpdef _MutableFst push(_Fst ifst, float delta=?, bool push_weights=?,
455 bool push_labels=?, bool remove_common_affix=?,
456 bool remove_total_weight=?, bool to_final=?)
458 cpdef bool randequivalent(_Fst ifst1, _Fst ifst2, int32 npath=?,
459 float delta=?, time_t seed=?, select=?,
460 int32 max_length=?) except *
462 cpdef _MutableFst randgen(_Fst ifst, int32 npath=?, time_t seed=?,
463 select=?, int32 max_length=?,
464 bool remove_total_weight=?, bool weighted=?)
466 cdef fst.ReplaceLabelType _get_replace_label_type(string rlt,
467 bool epsilon_on_replace) except *
469 cpdef _MutableFst replace(pairs, call_arc_labeling=?, return_arc_labeling=?,
470 bool epsilon_on_replace=?, int64 return_label=?)
472 cpdef _MutableFst reverse(_Fst ifst, bool require_superinitial=?)
474 cpdef _MutableFst rmepsilon(_Fst ifst, bool connect=?, float delta=?,
475 int64 nstate=?, queue_type=?, bool reverse=?,
478 cdef vector[fst.WeightClass] *_shortestdistance(_Fst ifst, float delta=?,
479 int64 nstate=?, queue_type=?,
480 bool reverse=?) except *
482 cpdef _MutableFst shortestpath(_Fst ifst, float delta=?, int32 nshortest=?,
483 int64 nstate=?, queue_type=?, bool unique=?,
486 cpdef _Fst statemap(_Fst ifst, map_type)
488 cpdef _MutableFst synchronize(_Fst ifst)
494 cdef class Compiler(object):
496 cdef unique_ptr[stringstream] _sstrm
497 cdef string _fst_type
498 cdef string _arc_type
499 cdef const fst.SymbolTable *_isymbols
500 cdef const fst.SymbolTable *_osymbols
501 cdef const fst.SymbolTable *_ssymbols
503 cdef bool _keep_isymbols
504 cdef bool _keep_osymbols
505 cdef bool _keep_state_numbering
506 cdef bool _allow_negative_labels
508 cpdef _Fst compile(self)
510 cpdef void write(self, expression)
515 cdef class FarReader(object):
517 cdef unique_ptr[fst.FarReaderClass] _reader
519 cpdef string arc_type(self)
521 cpdef bool done(self)
523 cpdef bool error(self)
525 cpdef string far_type(self)
527 cpdef bool find(self, key)
529 cpdef _Fst get_fst(self)
531 cpdef string get_key(self)
533 cpdef void next(self)
535 cpdef void reset(self)
540 cdef class FarWriter(object):
542 cdef unique_ptr[fst.FarWriterClass] _writer
544 cpdef string arc_type(self)
546 cdef void _close(self)
548 cpdef void add(self, key, _Fst ifst) except *
550 cpdef bool error(self)
552 cpdef string far_type(self)