272d54c432520617e2559f6708904f7692f66294
[platform/upstream/openfst.git] / src / extensions / python / pywrapfst.pxd
1 # See www.openfst.org for extensive documentation on this weighted
2 # finite-state transducer library.
3
4
5 from libc.time cimport time
6 from libc.time cimport time_t
7
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
13
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
19 cimport fst as fst
20 from ios cimport stringstream
21
22
23 # Constants not imported from elsewhere.
24 # TODO(kbg): Figure out how to access static class variables so I don't have
25 # to do it this way.
26
27 cdef int64 kNoSymbol
28
29
30 # Exportable helper functions.
31
32
33 cdef string tostring(data, encoding=?) except *
34
35 cdef string weighttostring(data, encoding=?) except *
36
37 cdef fst.ComposeFilter _get_compose_filter(
38     const string &compose_filter) except *
39
40 cdef fst.DeterminizeType _get_determinize_type(const string &det_type) except *
41
42 cdef fst.QueueType _get_queue_type(const string &queue_type) except *
43
44 cdef fst.RandArcSelection _get_rand_arc_selection(
45     const string &replace_label_type) except *
46
47 cdef fst.ReplaceLabelType _get_replace_label_type(
48     const string &replace_label_type, bool epsilon_on_replace) except *
49
50
51 # Weight.
52
53
54 cdef fst.WeightClass _get_WeightClass_or_One(const string &weight_type,
55                                              weight_string) except *
56
57 cdef fst.WeightClass _get_WeightClass_or_Zero(const string &weight_type,
58                                               weight_string) except *
59
60
61 cdef class Weight(object):
62
63   cdef unique_ptr[fst.WeightClass] _weight
64
65   cdef void _check_weight(self) except *
66
67   cpdef Weight copy(self)
68
69   cpdef string to_string(self)
70
71   cpdef string type(self)
72
73
74 cdef Weight _Weight_Zero(weight_type)
75
76 cdef Weight _Weight_One(weight_type)
77
78 cdef Weight _Weight_NoWeight(weight_type)
79
80 cdef Weight _plus(Weight lhs, Weight rhs)
81
82 cdef Weight _times(Weight lhs, Weight rhs)
83
84 cdef Weight _divide(Weight lhs, Weight rhs)
85
86 cdef Weight _power(Weight lhs, size_t n)
87
88
89 # SymbolTable.
90
91 ctypedef fst.SymbolTable * SymbolTable_ptr
92
93
94 cdef class _SymbolTable(object):
95
96   cdef fst.SymbolTable *_table
97
98   cpdef int64 available_key(self)
99
100   cpdef string checksum(self)
101
102   cpdef SymbolTable copy(self)
103
104   cpdef int64 get_nth_key(self, ssize_t pos) except *
105
106   cpdef string labeled_checksum(self)
107
108   cpdef bool member(self, key)
109
110   cpdef string name(self)
111
112   cpdef size_t num_symbols(self)
113
114   cpdef void write(self, filename) except *
115
116   cpdef void write_text(self, filename) except *
117
118
119 cdef class _EncodeMapperSymbolTable(_SymbolTable):
120
121   cdef shared_ptr[fst.EncodeMapperClass] _encoder
122
123
124 cdef class _FstSymbolTable(_SymbolTable):
125
126   cdef shared_ptr[fst.FstClass] _fst
127
128
129 cdef class _MutableSymbolTable(_SymbolTable):
130
131   cpdef int64 add_symbol(self, symbol, int64 key=?)
132
133   cpdef void add_table(self, _SymbolTable syms)
134
135   cpdef void set_name(self, new_name) except *
136
137
138 cdef class _MutableFstSymbolTable(_MutableSymbolTable):
139
140   cdef shared_ptr[fst.MutableFstClass] _mfst
141
142
143 cdef class SymbolTable(_MutableSymbolTable):
144
145   cdef unique_ptr[fst.SymbolTable] _smart_table
146
147
148 cdef _EncodeMapperSymbolTable _init_EncodeMapperSymbolTable(
149     fst.SymbolTable *table, shared_ptr[fst.EncodeMapperClass] encoder)
150
151
152 cdef _FstSymbolTable _init_FstSymbolTable(fst.SymbolTable *table,
153                                           shared_ptr[fst.FstClass] ifst)
154
155
156 cdef _MutableFstSymbolTable _init_MutableFstSymbolTable(fst.SymbolTable *table,
157     shared_ptr[fst.MutableFstClass] ifst)
158
159
160 cdef SymbolTable _init_SymbolTable(fst.SymbolTable *table)
161
162
163
164 cdef class SymbolTableIterator(object):
165
166   cdef shared_ptr[fst.SymbolTable] _table
167   cdef unique_ptr[fst.SymbolTableIterator] _siter
168
169   cpdef bool done(self)
170
171   cpdef void next(self)
172
173   cpdef void reset(self)
174
175   cpdef string symbol(self)
176
177   cpdef int64 value(self)
178
179
180 # EncodeMapper.
181
182
183 cdef class EncodeMapper(object):
184
185   cdef shared_ptr[fst.EncodeMapperClass] _encoder
186
187   cpdef string arc_type(self)
188
189   cpdef uint32 flags(self)
190
191   cpdef _EncodeMapperSymbolTable input_symbols(self)
192
193   cpdef _EncodeMapperSymbolTable output_symbols(self)
194
195   cpdef uint64 properties(self, uint64 mask)
196
197   cpdef void set_input_symbols(self, _SymbolTable syms) except *
198
199   cpdef void set_output_symbols(self, _SymbolTable syms) except *
200
201   cpdef string weight_type(self)
202
203
204 # Fst.
205
206
207 ctypedef fst.FstClass * FstClass_ptr
208 ctypedef fst.MutableFstClass * MutableFstClass_ptr
209 ctypedef fst.VectorFstClass * VectorFstClass_ptr
210
211
212 cdef class _Fst(object):
213
214   cdef shared_ptr[fst.FstClass] _fst
215
216   cpdef string arc_type(self)
217
218   cpdef ArcIterator arcs(self, int64 state)
219
220   cpdef _Fst copy(self)
221
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=?)
229
230   cpdef Weight final(self, int64 state)
231
232   cpdef string fst_type(self)
233
234   cpdef _FstSymbolTable input_symbols(self)
235
236   cpdef size_t num_arcs(self, int64 state) except *
237
238   cpdef size_t num_input_epsilons(self, int64 state) except *
239
240   cpdef size_t num_output_epsilons(self, int64 state) except *
241
242   cpdef _FstSymbolTable output_symbols(self)
243
244   cpdef uint64 properties(self, uint64 mask, bool test)
245
246   cpdef int64 start(self)
247
248   cpdef StateIterator states(self)
249
250   cpdef string text(self, _SymbolTable isymbols=?, _SymbolTable osymbols=?,
251                     _SymbolTable ssymbols=?, bool acceptor=?,
252                     bool show_weight_one=?, missing_sym=?)
253
254   cpdef bool verify(self)
255
256   cpdef string weight_type(self)
257
258   cpdef void write(self, filename) except *
259
260   cpdef string write_to_string(self)
261
262
263 cdef class _MutableFst(_Fst):
264
265   cdef shared_ptr[fst.MutableFstClass] _mfst
266
267   cdef void _check_mutating_imethod(self) except *
268
269   cdef void _add_arc(self, int64 state, Arc arc) except *
270
271   cpdef int64 add_state(self) except *
272
273   cdef void _arcsort(self, sort_type=?) except *
274
275   cdef void _closure(self, bool closure_plus=?) except *
276
277   cdef void _concat(self, _Fst ifst) except *
278
279   cdef void _connect(self) except *
280
281   cdef void _decode(self, EncodeMapper) except *
282
283   cdef void _delete_arcs(self, int64 state, size_t n=?) except *
284
285   cdef void _delete_states(self, states=?) except *
286
287   cdef void _encode(self, EncodeMapper) except *
288
289   cdef void _invert(self) except *
290
291   cdef void _minimize(self, float delta=?, bool allow_nondet=?) except *
292
293   cpdef MutableArcIterator mutable_arcs(self, int64 state)
294
295   cpdef int64 num_states(self)
296
297   cdef void _project(self, bool project_output=?) except *
298
299   cdef void _prune(self, float delta=?, int64 nstate=?, weight=?) except *
300
301   cdef void _push(self, float delta=?, bool remove_total_weight=?,
302                   bool to_final=?) except *
303
304   cdef void _relabel_pairs(self, ipairs=?, opairs=?) except *
305
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 *
311
312   cdef void _reserve_arcs(self, int64 state, size_t n) except *
313
314   cdef void _reserve_states(self, int64 n) except *
315
316   cdef void _reweight(self, potentials, bool to_final=?) except *
317
318   cdef void _rmepsilon(self, queue_type=?, bool connect=?, weight=?,
319                        int64 nstate=?, float delta=?) except *
320
321   cdef void _set_final(self, int64 state, weight=?) except *
322
323   cdef void _set_properties(self, uint64 props, uint64 mask)
324
325   cdef void _set_start(self, int64 state) except *
326
327   cdef void _set_input_symbols(self, _SymbolTable syms) except *
328
329   cdef void _set_output_symbols(self, _SymbolTable syms) except *
330
331   cdef void _topsort(self) except *
332
333   cdef void _union(self, _Fst ifst) except *
334
335
336 # Fst construction helpers.
337
338
339 cdef _Fst _init_Fst(FstClass_ptr tfst)
340
341 cdef _MutableFst _init_MutableFst(MutableFstClass_ptr tfst)
342
343 cdef _Fst _init_XFst(FstClass_ptr tfst)
344
345 cdef _MutableFst _create_Fst(arc_type=?)
346
347 cdef _Fst _read_Fst(filename, fst_type=?)
348
349 cdef _Fst _deserialize_Fst(fst_string, fst_type=?)
350
351
352 # Iterators.
353
354
355 cdef class Arc(object):
356
357   cdef unique_ptr[fst.ArcClass] _arc
358
359   cpdef Arc copy(self)
360
361
362 cdef Arc _init_Arc(const fst.ArcClass &arc)
363
364
365 cdef class ArcIterator(object):
366
367   cdef shared_ptr[fst.FstClass] _fst
368   cdef unique_ptr[fst.ArcIteratorClass] _aiter
369
370   cpdef bool done(self)
371
372   cpdef uint32 flags(self)
373
374   cpdef void next(self)
375
376   cpdef size_t position(self)
377
378   cpdef void reset(self)
379
380   cpdef void seek(self, size_t a)
381
382   cpdef void set_flags(self, uint32 flags, uint32 mask)
383
384   cpdef object value(self)
385
386
387 cdef class MutableArcIterator(object):
388
389   cdef shared_ptr[fst.MutableFstClass] _mfst
390   cdef unique_ptr[fst.MutableArcIteratorClass] _aiter
391
392   cpdef bool done(self)
393
394   cpdef uint32 flags(self)
395
396   cpdef void next(self)
397
398   cpdef size_t position(self)
399
400   cpdef void reset(self)
401
402   cpdef void seek(self, size_t a)
403
404   cpdef void set_flags(self, uint32 flags, uint32 mask)
405
406   cpdef void set_value(self, Arc arc)
407
408   cpdef object value(self)
409
410
411 cdef class StateIterator(object):
412
413   cdef shared_ptr[fst.FstClass] _fst
414   cdef unique_ptr[fst.StateIteratorClass] _siter
415
416   cpdef bool done(self)
417
418   cpdef void next(self)
419
420   cpdef void reset(self)
421
422   cpdef int64 value(self)
423
424
425 # Constructive operations on Fst.
426
427
428 cdef _Fst _map(_Fst ifst, float delta=?, map_type=?, weight=?)
429
430 cpdef _Fst arcmap(_Fst ifst, float delta=?, map_type=?, weight=?)
431
432 cpdef _MutableFst compose(_Fst ifst1, _Fst ifst2, compose_filter=?,
433                           bool connect=?)
434
435 cpdef _Fst convert(_Fst ifst, fst_type=?)
436
437 cpdef _MutableFst determinize(_Fst ifst, float delta=?, det_type=?,
438                               int64 nstate=?, int64 subsequential_label=?,
439                               weight=?, bool increment_subsequential_label=?)
440
441 cpdef _MutableFst difference(_Fst ifst1, _Fst ifst2, compose_filter=?,
442                              bool connect=?)
443
444 cpdef _MutableFst disambiguate(_Fst ifst, float delta=?, int64 nstate=?,
445                                int64 subsequential_label=?, weight=?)
446
447 cpdef _MutableFst epsnormalize(_Fst ifst, bool eps_norm_output=?)
448
449 cpdef bool equal(_Fst ifst1, _Fst ifst2, float delta=?)
450
451 cpdef bool equivalent(_Fst ifst1, _Fst ifst2, float delta=?) except *
452
453 cpdef _MutableFst intersect(_Fst ifst1, _Fst ifst2, compose_filter=?,
454                             bool connect=?)
455
456 cpdef bool isomorphic(_Fst ifst1, _Fst ifst2, float delta=?)
457
458 cpdef _MutableFst prune(_Fst ifst, float delta=?, int64 nstate=?,
459                         weight=?)
460
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=?)
464
465 cpdef bool randequivalent(_Fst ifst1, _Fst ifst2, int32 npath=?,
466                           float delta=?, time_t seed=?, select=?,
467                           int32 max_length=?) except *
468
469 cpdef _MutableFst randgen(_Fst ifst, int32 npath=?, time_t seed=?,
470                           select=?, int32 max_length=?,
471                           bool remove_total_weight=?, bool weighted=?)
472
473 cdef fst.ReplaceLabelType _get_replace_label_type(string rlt,
474     bool epsilon_on_replace) except *
475
476 cpdef _MutableFst replace(pairs, call_arc_labeling=?, return_arc_labeling=?,
477                           bool epsilon_on_replace=?, int64 return_label=?)
478
479 cpdef _MutableFst reverse(_Fst ifst, bool require_superinitial=?)
480
481 cdef vector[fst.WeightClass] *_shortestdistance(_Fst ifst, float delta=?,
482                                                 int64 nstate=?, queue_type=?,
483                                                 bool reverse=?) except *
484
485 cpdef _MutableFst shortestpath(_Fst ifst, float delta=?, int32 nshortest=?,
486                                int64 nstate=?, queue_type=?, bool unique=?,
487                                weight=?)
488
489 cpdef _Fst statemap(_Fst ifst, map_type)
490
491 cpdef _MutableFst synchronize(_Fst ifst)
492
493
494 # Compiler.
495
496
497 cdef class Compiler(object):
498
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
505   cdef bool _acceptor
506   cdef bool _keep_isymbols
507   cdef bool _keep_osymbols
508   cdef bool _keep_state_numbering
509   cdef bool _allow_negative_labels
510
511   cpdef _Fst compile(self)
512
513   cpdef void write(self, expression)
514
515
516 # FarReader.
517
518 cdef class FarReader(object):
519
520   cdef unique_ptr[fst.FarReaderClass] _reader
521
522   cpdef string arc_type(self)
523
524   cpdef bool done(self)
525
526   cpdef bool error(self)
527
528   cpdef string far_type(self)
529
530   cpdef bool find(self, key)
531
532   cpdef _Fst get_fst(self)
533
534   cpdef string get_key(self)
535
536   cpdef void next(self)
537
538   cpdef void reset(self)
539
540
541 # FarWriter.
542
543 cdef class FarWriter(object):
544
545   cdef unique_ptr[fst.FarWriterClass] _writer
546
547   cpdef string arc_type(self)
548
549   cdef void _close(self)
550
551   cpdef void add(self, key, _Fst ifst) except *
552
553   cpdef bool error(self)
554
555   cpdef string far_type(self)