5b4a11d43916f9284ede0ec6d339a2e842a8c78e
[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 kNoStateId
107
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   cdef WeightClass Plus(const WeightClass &, const WeightClass &)
281
282   cdef WeightClass Times(const WeightClass &, const WeightClass &)
283
284   cdef WeightClass Divide(const WeightClass &, const WeightClass &)
285
286   cdef WeightClass Power(const WeightClass &, size_t)
287
288   # Arcs.
289   cdef cppclass ArcClass:
290
291     ArcClass(const ArcClass &)
292
293     ArcClass(int64, int64, const WeightClass &, int64)
294
295     int64 ilabel
296     int64 olabel
297     WeightClass weight
298     int64 nextstate
299
300
301   # FSTs.
302
303
304   cdef cppclass FstClass:
305
306     FstClass(const FstClass &)
307
308     @staticmethod
309     FstClass *Read(const string &)
310
311     @staticmethod
312     FstClass *ReadFromString(const string &)
313
314     int64 Start()
315
316     WeightClass Final(int64)
317
318     size_t NumArcs(int64)
319
320     size_t NumInputEpsilons(int64)
321
322     size_t NumOutputEpsilons(int64)
323
324     const string &ArcType()
325
326     const string &FstType()
327
328     const SymbolTable *InputSymbols()
329
330     const SymbolTable *OutputSymbols()
331
332     const string &WeightType()
333
334     bool Write(const string &)
335
336     const string WriteToString()
337
338     uint64 Properties(uint64, bool)
339
340     bool ValidStateId(int64)
341
342
343   cdef cppclass MutableFstClass(FstClass):
344
345     bool AddArc(int64, const ArcClass &)
346
347     int64 AddState()
348
349     bool DeleteArcs(int64, size_t)
350
351     bool DeleteArcs(int64)
352
353     bool DeleteStates(const vector[int64] &)
354
355     void DeleteStates()
356
357     SymbolTable *MutableInputSymbols()
358
359     SymbolTable *MutableOutputSymbols()
360
361     int64 NumStates()
362
363     bool ReserveArcs(int64, size_t)
364
365     void ReserveStates(int64)
366
367     bool SetStart(int64)
368
369     bool SetFinal(int64, const WeightClass &)
370
371     void SetInputSymbols(SymbolTable *)
372
373     void SetOutputSymbols(SymbolTable *)
374
375     void SetProperties(uint64, uint64)
376
377   cdef cppclass VectorFstClass(MutableFstClass):
378
379    VectorFstClass(const FstClass &)
380
381    VectorFstClass(const string &)
382
383
384   # EncodeMapper.
385   cdef cppclass EncodeMapperClass:
386
387     EncodeMapperClass(const string &, uint32, EncodeType)
388
389     # Aliases this to "__call__", since Cython doesn't have good support for
390     # this C++ operator.
391     ArcClass __call__ "operator()"(const ArcClass &)
392
393     const string &ArcType()
394
395     uint32 Flags()
396
397     uint64 Properties(uint64)
398
399     EncodeType Type()
400
401     const SymbolTable *InputSymbols()
402
403     const SymbolTable *OutputSymbols()
404
405     void SetInputSymbols(const SymbolTable *)
406
407     void SetOutputSymbols(const SymbolTable *)
408
409     const string &WeightType()
410
411
412   # Iterators.
413
414
415   cdef cppclass ArcIteratorClass:
416
417     ArcIteratorClass(const FstClass &, int64)
418
419     bool Done()
420
421     ArcClass Value()
422
423     void Next()
424
425     void Reset()
426
427     void Seek(size_t)
428
429     size_t Position()
430
431     uint32 Flags()
432
433     void SetFlags(uint32, uint32)
434
435   cdef cppclass MutableArcIteratorClass:
436
437     MutableArcIteratorClass(MutableFstClass *, int64)
438
439     bool Done()
440
441     ArcClass Value()
442
443     void Next()
444
445     void Reset()
446
447     void Seek(size_t)
448
449     void SetValue(const ArcClass &)
450
451     size_t Position()
452
453     uint32 Flags()
454
455     void SetFlags(uint32, uint32)
456
457   cdef cppclass StateIteratorClass:
458
459     StateIteratorClass(const FstClass &)
460
461     bool Done()
462
463     int64 Value()
464
465     void Next()
466
467     void Reset()
468
469
470 ctypedef pair[int64, const FstClass *] LabelFstClassPair
471
472 ctypedef pair[int64, int64] LabelPair
473
474
475 cdef extern from "<fst/script/fstscript.h>" namespace "fst::script" nogil:
476
477   enum ArcFilterType:
478     ANY_ARC_FILTER
479     EPSILON_ARC_FILTER
480     INPUT_EPSILON_ARC_FILTER
481     OUTPUT_EPSILON_ARC_FILTER
482
483   enum ArcSortType:
484     ILABEL_SORT
485     OLABEL_SORT
486
487   cdef void ArcSort(MutableFstClass *, ArcSortType)
488
489   cdef ClosureType GetClosureType(bool)
490
491   cdef void Closure(MutableFstClass *, ClosureType)
492
493   cdef FstClass *CompileFstInternal(istream &, const string &,
494                                     const string &, const string &,
495                                     const SymbolTable *, const SymbolTable *,
496                                     const SymbolTable*, bool, bool, bool, bool,
497                                     bool)
498   cdef cppclass ComposeOptions:
499
500     ComposeOptions(bool, ComposeFilter)
501
502   cdef void Compose(FstClass &, FstClass &, MutableFstClass *,
503                     const ComposeOptions &)
504
505   cdef void Concat(MutableFstClass *, const FstClass &)
506
507   cdef void Connect(MutableFstClass *)
508
509   cdef FstClass *Convert(const FstClass &, const string &)
510
511   cdef void Decode(MutableFstClass *, const EncodeMapperClass &)
512
513   cdef cppclass DeterminizeOptions:
514
515     DeterminizeOptions(float, const WeightClass &, int64, int64,
516                        DeterminizeType, bool)
517
518   cdef void Determinize(const FstClass &, MutableFstClass *,
519                         const DeterminizeOptions &)
520
521   cdef cppclass DisambiguateOptions:
522
523     DisambiguateOptions(float, const WeightClass &, int64, int64)
524
525   cdef void Disambiguate(const FstClass &, MutableFstClass *,
526                          const DisambiguateOptions &)
527
528   cdef void Difference(const FstClass &, const FstClass &, MutableFstClass *,
529                        const ComposeOptions &)
530
531   cdef void DrawFst(const FstClass &fst, const SymbolTable *,
532                     const SymbolTable *, const SymbolTable *, bool,
533                     const string &, float, float, bool, bool, float, float, int,
534                     int, const string &, bool, ostream *, const string &)
535
536   cdef void Encode(MutableFstClass *, EncodeMapperClass *)
537
538   cdef EpsNormalizeType GetEpsNormalizeType(bool)
539
540   cdef void EpsNormalize(const FstClass &, MutableFstClass *, EpsNormalizeType)
541
542   cdef bool Equal(const FstClass &, const FstClass &, float)
543
544   cdef bool Equivalent(const FstClass &, const FstClass &, float, bool *)
545
546   cdef void Intersect(const FstClass &, const FstClass &, MutableFstClass *,
547                       const ComposeOptions &)
548
549   cdef void Invert(MutableFstClass *fst)
550
551   cdef bool Isomorphic(const FstClass &, const FstClass &, float)
552
553   enum MapType:
554     ARC_SUM_MAPPER
555     IDENTITY_MAPPER
556     INPUT_EPSILON_MAPPER
557     INVERT_MAPPER
558     OUTPUT_EPSILON_MAPPER
559     PLUS_MAPPER
560     QUANTIZE_MAPPER
561     RMWEIGHT_MAPPER
562     SUPERFINAL_MAPPER
563     TIMES_MAPPER
564     TO_LOG_MAPPER
565     TO_LOG64_MAPPER
566     TO_STD_MAPPER
567
568   cdef FstClass *Map(const FstClass &, MapType, float, const WeightClass &)
569
570   cdef void Minimize(MutableFstClass *, MutableFstClass *, float, bool)
571
572   cdef ProjectType GetProjectType(bool)
573
574   cdef void Project(MutableFstClass *, ProjectType)
575
576   cdef void PrintFst(const FstClass &, ostream &, const string &,
577                      const SymbolTable *, const SymbolTable *,
578                      const SymbolTable *, bool, bool, const string &)
579
580   cdef void Prune(const FstClass &, MutableFstClass *, const WeightClass &,
581                   int64, float)
582
583   cdef void Prune(MutableFstClass *, const WeightClass &, int64, float)
584
585   cdef void Push(const FstClass &, MutableFstClass *, uint32 flags,
586                  ReweightType, float)
587
588   cdef void Push(MutableFstClass *, ReweightType, float, bool)
589
590   enum RandArcSelection:
591     UNIFORM_ARC_SELECTOR
592     LOG_PROB_ARC_SELECTOR
593     FAST_LOG_PROB_ARC_SELECTOR
594
595   cdef bool RandEquivalent(const FstClass &, const FstClass &, int32, float,
596                            time_t, const RandGenOptions[RandArcSelection] &,
597                            bool *)
598
599   cdef void RandGen(const FstClass &, MutableFstClass *, time_t,
600                     const RandGenOptions[RandArcSelection] &)
601
602   cdef void Relabel(MutableFstClass *,
603       const SymbolTable *, const SymbolTable *, const string &, bool,
604       const SymbolTable *, const SymbolTable *, const string &, bool)
605
606   cdef void Relabel(MutableFstClass *, const vector[LabelPair] &,
607                     const vector[LabelPair] &)
608
609   cdef cppclass ReplaceOptions:
610
611      ReplaceOptions(int64, ReplaceLabelType, ReplaceLabelType, int64)
612
613   cdef void Replace(const vector[LabelFstClassPair] &, MutableFstClass *,
614                     const ReplaceOptions &)
615
616   cdef void Reverse(const FstClass &, MutableFstClass *, bool)
617
618   cdef void Reweight(MutableFstClass *, const vector[WeightClass] &,
619                      ReweightType)
620
621   cdef cppclass RmEpsilonOptions:
622
623     RmEpsilonOptions(QueueType, float, bool, const WeightClass &, int64)
624
625   cdef void RmEpsilon(const FstClass &, MutableFstClass *, bool,
626                       const RmEpsilonOptions &)
627
628   cdef void RmEpsilon(MutableFstClass *, bool, const WeightClass &, int64,
629                       float)
630
631   cdef cppclass ShortestDistanceOptions:
632
633     ShortestDistanceOptions(QueueType, ArcFilterType, int64, float)
634
635   cdef void ShortestDistance(const FstClass &, vector[WeightClass] *,
636                              const ShortestDistanceOptions &)
637
638   cdef void ShortestDistance(const FstClass &, vector[WeightClass] *, bool,
639                              float)
640
641   cdef cppclass ShortestPathOptions:
642
643     ShortestPathOptions(QueueType, int32, bool, bool, float, bool,
644                         const WeightClass &, int64)
645
646   cdef void ShortestPath(const FstClass &, MutableFstClass *,
647                          vector[WeightClass] *, const ShortestPathOptions &)
648
649   cdef void Synchronize(const FstClass &, MutableFstClass *)
650
651   cdef bool TopSort(MutableFstClass *)
652
653   cdef void Union(MutableFstClass *, const FstClass &)
654
655   cdef bool Verify(const FstClass &)
656
657
658 cdef extern from "nlp/fst/script/getters.h" namespace "fst::script" nogil:
659
660   cdef bool GetArcSortType(const string &, ArcSortType *)
661
662   cdef bool GetComposeFilter(const string &, ComposeFilter *)
663
664   cdef bool GetDeterminizeType(const string &, DeterminizeType *)
665
666   cdef uint32 GetEncodeFlags(bool, bool)
667
668   cdef bool GetMapType(const string &, MapType *)
669
670   cdef uint32 GetPushFlags(bool, bool, bool, bool)
671
672   cdef bool GetQueueType(const string &, QueueType *)
673
674   cdef bool GetRandArcSelection(const string &, RandArcSelection *)
675
676   cdef bool GetReplaceLabelType(string, bool, ReplaceLabelType *)
677
678   cdef ReweightType GetReweightType(bool)
679
680
681 cdef extern from "<fst/extensions/far/far.h>" namespace "fst" nogil:
682
683   enum FarType:
684     FAR_DEFAULT
685     FAR_STTABLE
686     FAR_STLIST
687     FAR_FST
688     FAR_SSTABLE
689
690 cdef extern from "<fst/extensions/far/getters.h>" \
691     namespace "fst" nogil:
692
693   string GetFarTypeString(FarType)
694
695
696 cdef extern from "<fst/extensions/far/getters.h>" \
697     namespace "fst::script" nogil:
698
699   FarType GetFarType(const string &)
700
701
702 cdef extern from "<fst/extensions/far/far-class.h>" \
703     namespace "fst::script" nogil:
704
705   cdef cppclass FarReaderClass:
706
707     const string &ArcType()
708
709     bool Done()
710
711     bool Error()
712
713     bool Find(const string &)
714
715     const FstClass *GetFstClass()
716
717     const string &GetKey()
718
719     void Next()
720
721     void Reset()
722
723     FarType Type()
724
725     # For simplicity, we always use the multiple-file one.
726
727     @staticmethod
728     FarReaderClass *Open(const vector[string] &)
729
730   cdef cppclass FarWriterClass:
731
732     bool Add(const string &, const FstClass &)
733
734     bool Error()
735
736     const string &ArcType()
737
738     FarType Type()
739
740     @staticmethod
741     FarWriterClass *Create(const string &, const string &, FarType)