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 google3 cimport string
15 from basictypes cimport int32
16 from basictypes cimport int64
17 from basictypes cimport uint32
18 from basictypes cimport uint64
20 from ios cimport stringstream
23 # Constants not imported from elsewhere.
24 # TODO(kbg): Figure out how to access static class variables so I don't have
30 # Exportable helper functions.
33 cdef string tostring(data, encoding=?) except *
35 cdef string weighttostring(data, encoding=?) except *
37 cdef fst.ComposeFilter _get_compose_filter(
38 const string &compose_filter) except *
40 cdef fst.DeterminizeType _get_determinize_type(const string &det_type) except *
42 cdef fst.QueueType _get_queue_type(const string &queue_type) except *
44 cdef fst.RandArcSelection _get_rand_arc_selection(
45 const string &replace_label_type) except *
47 cdef fst.ReplaceLabelType _get_replace_label_type(
48 const string &replace_label_type, bool epsilon_on_replace) except *
54 cdef fst.WeightClass _get_WeightClass_or_One(const string &weight_type,
55 weight_string) except *
57 cdef fst.WeightClass _get_WeightClass_or_Zero(const string &weight_type,
58 weight_string) except *
61 cdef class Weight(object):
63 cdef unique_ptr[fst.WeightClass] _weight
65 cdef void _check_weight(self) except *
67 cpdef Weight copy(self)
69 cpdef string to_string(self)
71 cpdef string type(self)
74 cdef Weight _Weight_Zero(weight_type)
76 cdef Weight _Weight_One(weight_type)
78 cdef Weight _Weight_NoWeight(weight_type)
80 cdef Weight _plus(Weight lhs, Weight rhs)
82 cdef Weight _times(Weight lhs, Weight rhs)
84 cdef Weight _divide(Weight lhs, Weight rhs)
86 cdef Weight _power(Weight lhs, size_t n)
91 ctypedef fst.SymbolTable * SymbolTable_ptr
94 cdef class _SymbolTable(object):
96 cdef fst.SymbolTable *_table
98 cpdef int64 available_key(self)
100 cpdef string checksum(self)
102 cpdef SymbolTable copy(self)
104 cpdef int64 get_nth_key(self, ssize_t pos) except *
106 cpdef string labeled_checksum(self)
108 cpdef bool member(self, key)
110 cpdef string name(self)
112 cpdef size_t num_symbols(self)
114 cpdef void write(self, filename) except *
116 cpdef void write_text(self, filename) except *
119 cdef class _EncodeMapperSymbolTable(_SymbolTable):
121 cdef shared_ptr[fst.EncodeMapperClass] _encoder
124 cdef class _FstSymbolTable(_SymbolTable):
126 cdef shared_ptr[fst.FstClass] _fst
129 cdef class _MutableSymbolTable(_SymbolTable):
131 cpdef int64 add_symbol(self, symbol, int64 key=?)
133 cpdef void add_table(self, _SymbolTable syms)
135 cpdef void set_name(self, new_name) except *
138 cdef class _MutableFstSymbolTable(_MutableSymbolTable):
140 cdef shared_ptr[fst.MutableFstClass] _mfst
143 cdef class SymbolTable(_MutableSymbolTable):
145 cdef unique_ptr[fst.SymbolTable] _smart_table
148 cdef _EncodeMapperSymbolTable _init_EncodeMapperSymbolTable(
149 fst.SymbolTable *table, shared_ptr[fst.EncodeMapperClass] encoder)
152 cdef _FstSymbolTable _init_FstSymbolTable(fst.SymbolTable *table,
153 shared_ptr[fst.FstClass] ifst)
156 cdef _MutableFstSymbolTable _init_MutableFstSymbolTable(fst.SymbolTable *table,
157 shared_ptr[fst.MutableFstClass] ifst)
160 cdef SymbolTable _init_SymbolTable(fst.SymbolTable *table)
164 cdef class SymbolTableIterator(object):
166 cdef shared_ptr[fst.SymbolTable] _table
167 cdef unique_ptr[fst.SymbolTableIterator] _siter
169 cpdef bool done(self)
171 cpdef void next(self)
173 cpdef void reset(self)
175 cpdef string symbol(self)
177 cpdef int64 value(self)
183 cdef class EncodeMapper(object):
185 cdef shared_ptr[fst.EncodeMapperClass] _encoder
187 cpdef string arc_type(self)
189 cpdef uint32 flags(self)
191 cpdef _EncodeMapperSymbolTable input_symbols(self)
193 cpdef _EncodeMapperSymbolTable output_symbols(self)
195 cpdef uint64 properties(self, uint64 mask)
197 cpdef void set_input_symbols(self, _SymbolTable syms) except *
199 cpdef void set_output_symbols(self, _SymbolTable syms) except *
201 cpdef string weight_type(self)
207 ctypedef fst.FstClass * FstClass_ptr
208 ctypedef fst.MutableFstClass * MutableFstClass_ptr
209 ctypedef fst.VectorFstClass * VectorFstClass_ptr
212 cdef class _Fst(object):
214 cdef shared_ptr[fst.FstClass] _fst
216 cpdef string arc_type(self)
218 cpdef ArcIterator arcs(self, int64 state)
220 cpdef _Fst copy(self)
222 cpdef void draw(self, filename, _SymbolTable isymbols=?,
223 _SymbolTable osymbols=?, SymbolTable ssymbols=?,
224 bool acceptor=?, title=?, double width=?,
225 double height=?, bool portrait=?, bool vertical=?,
226 double ranksep=?, double nodesep=?, int32 fontsize=?,
227 int32 precision=?, float_format=?,
228 bool show_weight_one=?)
230 cpdef Weight final(self, int64 state)
232 cpdef string fst_type(self)
234 cpdef _FstSymbolTable input_symbols(self)
236 cpdef size_t num_arcs(self, int64 state) except *
238 cpdef size_t num_input_epsilons(self, int64 state) except *
240 cpdef size_t num_output_epsilons(self, int64 state) except *
242 cpdef _FstSymbolTable output_symbols(self)
244 cpdef uint64 properties(self, uint64 mask, bool test)
246 cpdef int64 start(self)
248 cpdef StateIterator states(self)
250 cpdef string text(self, _SymbolTable isymbols=?, _SymbolTable osymbols=?,
251 _SymbolTable ssymbols=?, bool acceptor=?,
252 bool show_weight_one=?, missing_sym=?)
254 cpdef bool verify(self)
256 cpdef string weight_type(self)
258 cpdef void write(self, filename) except *
260 cpdef string write_to_string(self)
263 cdef class _MutableFst(_Fst):
265 cdef shared_ptr[fst.MutableFstClass] _mfst
267 cdef void _check_mutating_imethod(self) except *
269 cdef void _add_arc(self, int64 state, Arc arc) except *
271 cpdef int64 add_state(self) except *
273 cdef void _arcsort(self, sort_type=?) except *
275 cdef void _closure(self, bool closure_plus=?) except *
277 cdef void _concat(self, _Fst ifst) except *
279 cdef void _connect(self) except *
281 cdef void _decode(self, EncodeMapper) except *
283 cdef void _delete_arcs(self, int64 state, size_t n=?) except *
285 cdef void _delete_states(self, states=?) except *
287 cdef void _encode(self, EncodeMapper) except *
289 cdef void _invert(self) except *
291 cdef void _minimize(self, float delta=?, bool allow_nondet=?) except *
293 cpdef MutableArcIterator mutable_arcs(self, int64 state)
295 cpdef int64 num_states(self)
297 cdef void _project(self, bool project_output=?) except *
299 cdef void _prune(self, float delta=?, int64 nstate=?, weight=?) except *
301 cdef void _push(self, float delta=?, bool remove_total_weight=?,
302 bool to_final=?) except *
304 cdef void _relabel_pairs(self, ipairs=?, opairs=?) except *
306 cdef void _relabel_tables(self, _SymbolTable old_isymbols=?,
307 _SymbolTable new_isymbols=?, unknown_isymbol=?,
308 bool attach_new_isymbols=?,
309 _SymbolTable old_osymbols=?, _SymbolTable new_osymbols=?,
310 unknown_osymbol=?, bool attach_new_osymbols=?) except *
312 cdef void _reserve_arcs(self, int64 state, size_t n) except *
314 cdef void _reserve_states(self, int64 n) except *
316 cdef void _reweight(self, potentials, bool to_final=?) except *
318 cdef void _rmepsilon(self, queue_type=?, bool connect=?, weight=?,
319 int64 nstate=?, float delta=?) except *
321 cdef void _set_final(self, int64 state, weight=?) except *
323 cdef void _set_properties(self, uint64 props, uint64 mask)
325 cdef void _set_start(self, int64 state) except *
327 cdef void _set_input_symbols(self, _SymbolTable syms) except *
329 cdef void _set_output_symbols(self, _SymbolTable syms) except *
331 cdef void _topsort(self) except *
333 cdef void _union(self, _Fst ifst) except *
336 # Fst construction helpers.
339 cdef _Fst _init_Fst(FstClass_ptr tfst)
341 cdef _MutableFst _init_MutableFst(MutableFstClass_ptr tfst)
343 cdef _Fst _init_XFst(FstClass_ptr tfst)
345 cdef _MutableFst _create_Fst(arc_type=?)
347 cdef _Fst _read_Fst(filename, fst_type=?)
349 cdef _Fst _deserialize_Fst(fst_string, fst_type=?)
355 cdef class Arc(object):
357 cdef unique_ptr[fst.ArcClass] _arc
362 cdef Arc _init_Arc(const fst.ArcClass &arc)
365 cdef class ArcIterator(object):
367 cdef shared_ptr[fst.FstClass] _fst
368 cdef unique_ptr[fst.ArcIteratorClass] _aiter
370 cpdef bool done(self)
372 cpdef uint32 flags(self)
374 cpdef void next(self)
376 cpdef size_t position(self)
378 cpdef void reset(self)
380 cpdef void seek(self, size_t a)
382 cpdef void set_flags(self, uint32 flags, uint32 mask)
384 cpdef object value(self)
387 cdef class MutableArcIterator(object):
389 cdef shared_ptr[fst.MutableFstClass] _mfst
390 cdef unique_ptr[fst.MutableArcIteratorClass] _aiter
392 cpdef bool done(self)
394 cpdef uint32 flags(self)
396 cpdef void next(self)
398 cpdef size_t position(self)
400 cpdef void reset(self)
402 cpdef void seek(self, size_t a)
404 cpdef void set_flags(self, uint32 flags, uint32 mask)
406 cpdef void set_value(self, Arc arc)
408 cpdef object value(self)
411 cdef class StateIterator(object):
413 cdef shared_ptr[fst.FstClass] _fst
414 cdef unique_ptr[fst.StateIteratorClass] _siter
416 cpdef bool done(self)
418 cpdef void next(self)
420 cpdef void reset(self)
422 cpdef int64 value(self)
425 # Constructive operations on Fst.
428 cdef _Fst _map(_Fst ifst, float delta=?, map_type=?, weight=?)
430 cpdef _Fst arcmap(_Fst ifst, float delta=?, map_type=?, weight=?)
432 cpdef _MutableFst compose(_Fst ifst1, _Fst ifst2, compose_filter=?,
435 cpdef _Fst convert(_Fst ifst, fst_type=?)
437 cpdef _MutableFst determinize(_Fst ifst, float delta=?, det_type=?,
438 int64 nstate=?, int64 subsequential_label=?,
439 weight=?, bool increment_subsequential_label=?)
441 cpdef _MutableFst difference(_Fst ifst1, _Fst ifst2, compose_filter=?,
444 cpdef _MutableFst disambiguate(_Fst ifst, float delta=?, int64 nstate=?,
445 int64 subsequential_label=?, weight=?)
447 cpdef _MutableFst epsnormalize(_Fst ifst, bool eps_norm_output=?)
449 cpdef bool equal(_Fst ifst1, _Fst ifst2, float delta=?)
451 cpdef bool equivalent(_Fst ifst1, _Fst ifst2, float delta=?) except *
453 cpdef _MutableFst intersect(_Fst ifst1, _Fst ifst2, compose_filter=?,
456 cpdef bool isomorphic(_Fst ifst1, _Fst ifst2, float delta=?)
458 cpdef _MutableFst prune(_Fst ifst, float delta=?, int64 nstate=?,
461 cpdef _MutableFst push(_Fst ifst, float delta=?, bool push_weights=?,
462 bool push_labels=?, bool remove_common_affix=?,
463 bool remove_total_weight=?, bool to_final=?)
465 cpdef bool randequivalent(_Fst ifst1, _Fst ifst2, int32 npath=?,
466 float delta=?, time_t seed=?, select=?,
467 int32 max_length=?) except *
469 cpdef _MutableFst randgen(_Fst ifst, int32 npath=?, time_t seed=?,
470 select=?, int32 max_length=?,
471 bool remove_total_weight=?, bool weighted=?)
473 cdef fst.ReplaceLabelType _get_replace_label_type(string rlt,
474 bool epsilon_on_replace) except *
476 cpdef _MutableFst replace(pairs, call_arc_labeling=?, return_arc_labeling=?,
477 bool epsilon_on_replace=?, int64 return_label=?)
479 cpdef _MutableFst reverse(_Fst ifst, bool require_superinitial=?)
481 cdef vector[fst.WeightClass] *_shortestdistance(_Fst ifst, float delta=?,
482 int64 nstate=?, queue_type=?,
483 bool reverse=?) except *
485 cpdef _MutableFst shortestpath(_Fst ifst, float delta=?, int32 nshortest=?,
486 int64 nstate=?, queue_type=?, bool unique=?,
489 cpdef _Fst statemap(_Fst ifst, map_type)
491 cpdef _MutableFst synchronize(_Fst ifst)
497 cdef class Compiler(object):
499 cdef unique_ptr[stringstream] _sstrm
500 cdef string _fst_type
501 cdef string _arc_type
502 cdef const fst.SymbolTable *_isymbols
503 cdef const fst.SymbolTable *_osymbols
504 cdef const fst.SymbolTable *_ssymbols
506 cdef bool _keep_isymbols
507 cdef bool _keep_osymbols
508 cdef bool _keep_state_numbering
509 cdef bool _allow_negative_labels
511 cpdef _Fst compile(self)
513 cpdef void write(self, expression)
518 cdef class FarReader(object):
520 cdef unique_ptr[fst.FarReaderClass] _reader
522 cpdef string arc_type(self)
524 cpdef bool done(self)
526 cpdef bool error(self)
528 cpdef string far_type(self)
530 cpdef bool find(self, key)
532 cpdef _Fst get_fst(self)
534 cpdef string get_key(self)
536 cpdef void next(self)
538 cpdef void reset(self)
543 cdef class FarWriter(object):
545 cdef unique_ptr[fst.FarWriterClass] _writer
547 cpdef string arc_type(self)
549 cdef void _close(self)
551 cpdef void add(self, key, _Fst ifst) except *
553 cpdef bool error(self)
555 cpdef string far_type(self)