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