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