Imported Upstream version 1.6.6
[platform/upstream/openfst.git] / src / extensions / python / fst.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_t
6 from libc.time cimport time
7
8 from libcpp cimport bool
9 from libcpp.vector cimport vector
10 from libcpp.utility cimport pair
11
12 from libcpp.string cimport string
13 from basictypes cimport int32
14 from basictypes cimport int64
15 from basictypes cimport uint32
16 from basictypes cimport uint64
17 from ios cimport istream
18 from ios cimport ostream
19
20
21 cdef extern from "<fst/util.h>" nogil:
22
23   # Note that this is a copy, so it should be viewed as read-only.
24
25   bool FLAGS_fst_error_fatal
26
27
28 cdef extern from "<fst/fstlib.h>" namespace "fst" nogil:
29
30   # FST properties.
31   const uint64 kExpanded
32   const uint64 kMutable
33   const uint64 kError
34   const uint64 kAcceptor
35   const uint64 kNotAcceptor
36   const uint64 kIDeterministic
37   const uint64 kNonIDeterministic
38   const uint64 kODeterministic
39   const uint64 kNonODeterministic
40   const uint64 kEpsilons
41   const uint64 kNoEpsilons
42   const uint64 kIEpsilons
43   const uint64 kNoIEpsilons
44   const uint64 kOEpsilons
45   const uint64 kNoOEpsilons
46   const uint64 kILabelSorted
47   const uint64 kNotILabelSorted
48   const uint64 kOLabelSorted
49   const uint64 kNotOLabelSorted
50   const uint64 kWeighted
51   const uint64 kUnweighted
52   const uint64 kCyclic
53   const uint64 kAcyclic
54   const uint64 kInitialCyclic
55   const uint64 kInitialAcyclic
56   const uint64 kTopSorted
57   const uint64 kNotTopSorted
58   const uint64 kAccessible
59   const uint64 kNotAccessible
60   const uint64 kCoAccessible
61   const uint64 kNotCoAccessible
62   const uint64 kString
63   const uint64 kNotString
64   const uint64 kWeightedCycles
65   const uint64 kUnweightedCycles
66   const uint64 kNullProperties
67   const uint64 kCopyProperties
68   const uint64 kIntrinsicProperties
69   const uint64 kExtrinsicProperties
70   const uint64 kSetStartProperties
71   const uint64 kSetFinalProperties
72   const uint64 kAddStateProperties
73   const uint64 kAddArcProperties
74   const uint64 kSetArcProperties
75   const uint64 kDeleteStatesProperties
76   const uint64 kDeleteArcsProperties
77   const uint64 kStateSortProperties
78   const uint64 kArcSortProperties
79   const uint64 kILabelInvariantProperties
80   const uint64 kOLabelInvariantProperties
81   const uint64 kWeightInvariantProperties
82   const uint64 kAddSuperFinalProperties
83   const uint64 kRmSuperFinalProperties
84   const uint64 kBinaryProperties
85   const uint64 kTrinaryProperties
86   const uint64 kPosTrinaryProperties
87   const uint64 kNegTrinaryProperties
88   const uint64 kFstProperties
89
90   # ArcIterator flags.
91   const uint32 kArcILabelValue
92   const uint32 kArcOLabelValue
93   const uint32 kArcWeightValue
94   const uint32 kArcNextStateValue
95   const uint32 kArcNoCache
96   const uint32 kArcValueFlags
97   const uint32 kArcFlags
98
99   # EncodeMapper flags.
100   const uint32 kEncodeLabels
101   const uint32 kEncodeWeights
102   const uint32 kEncodeFlags
103
104   # Default argument constants.
105   const float kDelta
106   const float kShortestDelta
107   const int64 kNoLabel
108   const int64 kNoStateId
109
110   enum ClosureType:
111     CLOSURE_STAR
112     CLOSURE_PLUS
113
114
115   enum ComposeFilter:
116     AUTO_FILTER
117     NULL_FILTER
118     SEQUENCE_FILTER
119     ALT_SEQUENCE_FILTER
120     MATCH_FILTER
121     TRIVIAL_FILTER
122
123
124   cdef cppclass ComposeOptions:
125
126     ComposeOptions(bool, ComposeFilter)
127
128
129   enum DeterminizeType:
130     DETERMINIZE_FUNCTIONAL
131     DETERMINIZE_NONFUNCTIONAL
132     DETERMINIZE_DISAMBIGUATE
133
134
135   enum EncodeType:
136     DECODE
137     ENCODE
138
139
140   enum EpsNormalizeType:
141     EPS_NORM_INPUT
142     EPS_NORM_OUTPUT
143
144
145   enum ProjectType:
146     PROJECT_INPUT
147     PROJECT_OUTPUT
148
149
150   enum QueueType:
151     TRIVIAL_QUEUE
152     FIFO_QUEUE
153     LIFO_QUEUE
154     SHORTEST_FIRST_QUEUE
155     TOP_ORDER_QUEUE
156     STATE_ORDER_QUEUE
157     SCC_QUEUE
158     AUTO_QUEUE
159     OTHER_QUEUE
160
161
162   # This is a templated struct at the C++ level, but Cython does not support
163   # templated structs unless we pretend they are full-blown classes.
164   cdef cppclass RandGenOptions[RandArcSelection]:
165
166     RandGenOptions(const RandArcSelection &, int32, int32, bool, bool)
167
168
169   enum ReplaceLabelType:
170     REPLACE_LABEL_NEITHER
171     REPLACE_LABEL_INPUT
172     REPLACE_LABEL_OUTPUT
173     REPLACE_LABEL_BOTH
174
175
176   enum ReweightType:
177     REWEIGHT_TO_INITIAL
178     REWEIGHT_TO_FINAL
179
180
181   cdef cppclass SymbolTableTextOptions:
182
183     SymbolTableTextOptions(bool)
184
185
186   # Symbol tables.
187   cdef cppclass SymbolTable:
188
189     SymbolTable()
190
191     SymbolTable(const string &)
192
193     @staticmethod
194     SymbolTable *Read(const string &)
195
196     @staticmethod
197     SymbolTable *ReadText(const string &, const SymbolTableTextOptions &)
198
199     int64 AddSymbol(const string &, int64)
200
201     int64 AddSymbol(const string &)
202
203     SymbolTable *Copy()
204
205     # Aliased for overload.
206     string FindSymbol "Find"(int64)
207
208     # Aliased for overload.
209     int64 FindIndex "Find"(string)
210
211     # Aliased for overload.
212     bool MemberSymbol "Member"(string)
213
214     # Aliased for overload.
215     bool MemberIndex "Member"(int64)
216
217     void AddTable(const SymbolTable &)
218
219     int64 GetNthKey(ssize_t)
220
221     const string &Name()
222
223     void SetName(const string &)
224
225     string CheckSum()
226
227     string LabeledCheckSum()
228
229     bool Write(const string &)
230
231     bool WriteText(const string &)
232
233     int64 AvailableKey()
234
235     size_t NumSymbols()
236
237
238   SymbolTable *CompactSymbolTable(const SymbolTable &syms)
239
240   SymbolTable *MergeSymbolTable(const SymbolTable &, const SymbolTable &,
241                                 bool *)
242
243   SymbolTable *FstReadSymbols(const string &, bool)
244
245
246   cdef cppclass SymbolTableIterator:
247
248     SymbolTableIterator(const SymbolTable &)
249
250     bool Done()
251
252     void Next()
253
254     void Reset()
255
256     string Symbol()
257
258     int64 Value()
259
260
261 cdef extern from "<fst/script/fstscript.h>" namespace "fst::script" nogil:
262
263
264   # Weights.
265   cdef cppclass WeightClass:
266
267     WeightClass()
268
269     WeightClass(const WeightClass &)
270
271     WeightClass(const string &, const string &)
272
273     const string &Type()
274
275     string ToString()
276
277     @staticmethod
278     const WeightClass &Zero(const string &)
279
280     @staticmethod
281     const WeightClass &One(const string &)
282
283     @staticmethod
284     const WeightClass &NoWeight(const string &)
285
286   # Alias.
287   cdef bool Eq "operator=="(const WeightClass &, const WeightClass &)
288
289   # Alias.
290   cdef bool Ne "operator!="(const WeightClass &, const WeightClass &)
291
292   cdef WeightClass Plus(const WeightClass &, const WeightClass &)
293
294   cdef WeightClass Times(const WeightClass &, const WeightClass &)
295
296   cdef WeightClass Divide(const WeightClass &, const WeightClass &)
297
298   cdef WeightClass Power(const WeightClass &, size_t)
299
300   # Arcs.
301   cdef cppclass ArcClass:
302
303     ArcClass(const ArcClass &)
304
305     ArcClass(int64, int64, const WeightClass &, int64)
306
307     int64 ilabel
308     int64 olabel
309     WeightClass weight
310     int64 nextstate
311
312
313   # FSTs.
314
315
316   cdef cppclass FstClass:
317
318     FstClass(const FstClass &)
319
320     @staticmethod
321     FstClass *Read(const string &)
322
323     # Aliased for overload.
324     @staticmethod
325     FstClass *ReadFromStream "Read"(istream &, const string &)
326
327     int64 Start()
328
329     WeightClass Final(int64)
330
331     size_t NumArcs(int64)
332
333     size_t NumInputEpsilons(int64)
334
335     size_t NumOutputEpsilons(int64)
336
337     const string &ArcType()
338
339     const string &FstType()
340
341     const SymbolTable *InputSymbols()
342
343     const SymbolTable *OutputSymbols()
344
345     const string &WeightType()
346
347     bool Write(const string &)
348
349     bool Write(ostream &, const string &)
350
351     uint64 Properties(uint64, bool)
352
353     bool ValidStateId(int64)
354
355
356   cdef cppclass MutableFstClass(FstClass):
357
358     bool AddArc(int64, const ArcClass &)
359
360     int64 AddState()
361
362     bool DeleteArcs(int64, size_t)
363
364     bool DeleteArcs(int64)
365
366     bool DeleteStates(const vector[int64] &)
367
368     void DeleteStates()
369
370     SymbolTable *MutableInputSymbols()
371
372     SymbolTable *MutableOutputSymbols()
373
374     int64 NumStates()
375
376     bool ReserveArcs(int64, size_t)
377
378     void ReserveStates(int64)
379
380     bool SetStart(int64)
381
382     bool SetFinal(int64, const WeightClass &)
383
384     void SetInputSymbols(SymbolTable *)
385
386     void SetOutputSymbols(SymbolTable *)
387
388     void SetProperties(uint64, uint64)
389
390   cdef cppclass VectorFstClass(MutableFstClass):
391
392    VectorFstClass(const FstClass &)
393
394    VectorFstClass(const string &)
395
396
397   # EncodeMapper.
398   cdef cppclass EncodeMapperClass:
399
400     EncodeMapperClass(const string &, uint32, EncodeType)
401
402     # Aliased to __call__ as Cython doesn't have good support for operator().
403     ArcClass __call__ "operator()"(const ArcClass &)
404
405     const string &ArcType()
406
407     uint32 Flags()
408
409     uint64 Properties(uint64)
410
411     EncodeType Type()
412
413     const SymbolTable *InputSymbols()
414
415     const SymbolTable *OutputSymbols()
416
417     void SetInputSymbols(const SymbolTable *)
418
419     void SetOutputSymbols(const SymbolTable *)
420
421     const string &WeightType()
422
423
424   # Iterators.
425
426
427   cdef cppclass ArcIteratorClass:
428
429     ArcIteratorClass(const FstClass &, int64)
430
431     bool Done()
432
433     ArcClass Value()
434
435     void Next()
436
437     void Reset()
438
439     void Seek(size_t)
440
441     size_t Position()
442
443     uint32 Flags()
444
445     void SetFlags(uint32, uint32)
446
447   cdef cppclass MutableArcIteratorClass:
448
449     MutableArcIteratorClass(MutableFstClass *, int64)
450
451     bool Done()
452
453     ArcClass Value()
454
455     void Next()
456
457     void Reset()
458
459     void Seek(size_t)
460
461     void SetValue(const ArcClass &)
462
463     size_t Position()
464
465     uint32 Flags()
466
467     void SetFlags(uint32, uint32)
468
469   cdef cppclass StateIteratorClass:
470
471     StateIteratorClass(const FstClass &)
472
473     bool Done()
474
475     int64 Value()
476
477     void Next()
478
479     void Reset()
480
481
482 ctypedef pair[int64, const FstClass *] LabelFstClassPair
483
484 ctypedef pair[int64, int64] LabelPair
485
486
487 cdef extern from "<fst/script/fstscript.h>" namespace "fst::script" nogil:
488
489   enum ArcFilterType:
490     ANY_ARC_FILTER
491     EPSILON_ARC_FILTER
492     INPUT_EPSILON_ARC_FILTER
493     OUTPUT_EPSILON_ARC_FILTER
494
495   enum ArcSortType:
496     ILABEL_SORT
497     OLABEL_SORT
498
499   cdef void ArcSort(MutableFstClass *, ArcSortType)
500
501   cdef ClosureType GetClosureType(bool)
502
503   cdef void Closure(MutableFstClass *, ClosureType)
504
505   cdef FstClass *CompileFstInternal(istream &, const string &,
506                                     const string &, const string &,
507                                     const SymbolTable *, const SymbolTable *,
508                                     const SymbolTable*, bool, bool, bool, bool,
509                                     bool)
510
511   cdef void Compose(FstClass &, FstClass &, MutableFstClass *,
512                     const ComposeOptions &)
513
514   cdef void Concat(MutableFstClass *, const FstClass &)
515
516   cdef void Connect(MutableFstClass *)
517
518   cdef FstClass *Convert(const FstClass &, const string &)
519
520   cdef void Decode(MutableFstClass *, const EncodeMapperClass &)
521
522   cdef cppclass DeterminizeOptions:
523
524     DeterminizeOptions(float, const WeightClass &, int64, int64,
525                        DeterminizeType, bool)
526
527   cdef void Determinize(const FstClass &, MutableFstClass *,
528                         const DeterminizeOptions &)
529
530   cdef cppclass DisambiguateOptions:
531
532     DisambiguateOptions(float, const WeightClass &, int64, int64)
533
534   cdef void Disambiguate(const FstClass &, MutableFstClass *,
535                          const DisambiguateOptions &)
536
537   cdef void Difference(const FstClass &, const FstClass &, MutableFstClass *,
538                        const ComposeOptions &)
539
540   cdef void DrawFst(const FstClass &fst, const SymbolTable *,
541                     const SymbolTable *, const SymbolTable *, bool,
542                     const string &, float, float, bool, bool, float, float, int,
543                     int, const string &, bool, ostream *, const string &)
544
545   cdef void Encode(MutableFstClass *, EncodeMapperClass *)
546
547   cdef EpsNormalizeType GetEpsNormalizeType(bool)
548
549   cdef void EpsNormalize(const FstClass &, MutableFstClass *, EpsNormalizeType)
550
551   cdef bool Equal(const FstClass &, const FstClass &, float)
552
553   cdef bool Equivalent(const FstClass &, const FstClass &, float)
554
555   cdef void Intersect(const FstClass &, const FstClass &, MutableFstClass *,
556                       const ComposeOptions &)
557
558   cdef void Invert(MutableFstClass *fst)
559
560   cdef bool Isomorphic(const FstClass &, const FstClass &, float)
561
562   enum MapType:
563     ARC_SUM_MAPPER
564     IDENTITY_MAPPER
565     INPUT_EPSILON_MAPPER
566     INVERT_MAPPER
567     OUTPUT_EPSILON_MAPPER
568     PLUS_MAPPER
569     QUANTIZE_MAPPER
570     RMWEIGHT_MAPPER
571     SUPERFINAL_MAPPER
572     TIMES_MAPPER
573     TO_LOG_MAPPER
574     TO_LOG64_MAPPER
575     TO_STD_MAPPER
576
577   cdef FstClass *Map(const FstClass &, MapType, float, const WeightClass &)
578
579   cdef void Minimize(MutableFstClass *, MutableFstClass *, float, bool)
580
581   cdef ProjectType GetProjectType(bool)
582
583   cdef void Project(MutableFstClass *, ProjectType)
584
585   cdef void PrintFst(const FstClass &, ostream &, const string &,
586                      const SymbolTable *, const SymbolTable *,
587                      const SymbolTable *, bool, bool, const string &)
588
589   cdef void Prune(const FstClass &, MutableFstClass *, const WeightClass &,
590                   int64, float)
591
592   cdef void Prune(MutableFstClass *, const WeightClass &, int64, float)
593
594   cdef void Push(const FstClass &, MutableFstClass *, uint32 flags,
595                  ReweightType, float)
596
597   cdef void Push(MutableFstClass *, ReweightType, float, bool)
598
599   enum RandArcSelection:
600     UNIFORM_ARC_SELECTOR
601     LOG_PROB_ARC_SELECTOR
602     FAST_LOG_PROB_ARC_SELECTOR
603
604   cdef bool RandEquivalent(const FstClass &, const FstClass &, int32, float,
605                            time_t, const RandGenOptions[RandArcSelection] &)
606
607   cdef void RandGen(const FstClass &, MutableFstClass *, time_t,
608                     const RandGenOptions[RandArcSelection] &)
609
610   cdef void Relabel(MutableFstClass *, const SymbolTable *,
611                     const SymbolTable *, const string &, bool,
612                     const SymbolTable *, const SymbolTable *, const string &,
613                     bool)
614
615   cdef void Relabel(MutableFstClass *, const vector[LabelPair] &,
616                     const vector[LabelPair] &)
617
618   cdef cppclass ReplaceOptions:
619
620      ReplaceOptions(int64, ReplaceLabelType, ReplaceLabelType, int64)
621
622   cdef void Replace(const vector[LabelFstClassPair] &, MutableFstClass *,
623                     const ReplaceOptions &)
624
625   cdef void Reverse(const FstClass &, MutableFstClass *, bool)
626
627   cdef void Reweight(MutableFstClass *, const vector[WeightClass] &,
628                      ReweightType)
629
630   cdef cppclass RmEpsilonOptions:
631
632     RmEpsilonOptions(QueueType, bool, const WeightClass &, int64, float)
633
634   cdef void RmEpsilon(MutableFstClass *, const RmEpsilonOptions &)
635
636   cdef cppclass ShortestDistanceOptions:
637
638     ShortestDistanceOptions(QueueType, ArcFilterType, int64, float)
639
640   cdef void ShortestDistance(const FstClass &, vector[WeightClass] *,
641                              const ShortestDistanceOptions &)
642
643   cdef void ShortestDistance(const FstClass &, vector[WeightClass] *, bool,
644                              float)
645
646   cdef cppclass ShortestPathOptions:
647
648     ShortestPathOptions(QueueType, int32, bool, float, const WeightClass &,
649                         int64)
650
651   cdef void ShortestPath(const FstClass &, MutableFstClass *,
652                          const ShortestPathOptions &)
653
654   cdef void Synchronize(const FstClass &, MutableFstClass *)
655
656   cdef bool TopSort(MutableFstClass *)
657
658   cdef void Union(MutableFstClass *, const FstClass &)
659
660   cdef bool Verify(const FstClass &)
661
662
663 cdef extern from "<fst/script/getters.h>" namespace "fst::script" nogil:
664
665   cdef bool GetArcSortType(const string &, ArcSortType *)
666
667   cdef bool GetComposeFilter(const string &, ComposeFilter *)
668
669   cdef bool GetDeterminizeType(const string &, DeterminizeType *)
670
671   cdef uint32 GetEncodeFlags(bool, bool)
672
673   cdef bool GetMapType(const string &, MapType *)
674
675   cdef uint32 GetPushFlags(bool, bool, bool, bool)
676
677   cdef bool GetQueueType(const string &, QueueType *)
678
679   cdef bool GetRandArcSelection(const string &, RandArcSelection *)
680
681   cdef bool GetReplaceLabelType(string, bool, ReplaceLabelType *)
682
683   cdef ReweightType GetReweightType(bool)
684
685
686 cdef extern from "<fst/extensions/far/far.h>" namespace "fst" nogil:
687
688   enum FarType:
689     FAR_DEFAULT
690     FAR_STTABLE
691     FAR_STLIST
692     FAR_FST
693     FAR_SSTABLE
694
695 cdef extern from "<fst/extensions/far/getters.h>" \
696     namespace "fst" nogil:
697
698   string GetFarTypeString(FarType)
699
700
701 cdef extern from "<fst/extensions/far/getters.h>" \
702     namespace "fst::script" nogil:
703
704   FarType GetFarType(const string &)
705
706
707 cdef extern from "<fst/extensions/far/far-class.h>" \
708     namespace "fst::script" nogil:
709
710   cdef cppclass FarReaderClass:
711
712     const string &ArcType()
713
714     bool Done()
715
716     bool Error()
717
718     bool Find(const string &)
719
720     const FstClass *GetFstClass()
721
722     const string &GetKey()
723
724     void Next()
725
726     void Reset()
727
728     FarType Type()
729
730     # For simplicity, we always use the multiple-file one.
731
732     @staticmethod
733     FarReaderClass *Open(const vector[string] &)
734
735   cdef cppclass FarWriterClass:
736
737     bool Add(const string &, const FstClass &)
738
739     bool Error()
740
741     const string &ArcType()
742
743     FarType Type()
744
745     @staticmethod
746     FarWriterClass *Create(const string &, const string &, FarType)