1 # See www.openfst.org for extensive documentation on this weighted
2 # finite-state transducer library.
5 from libc.time cimport time_t
6 from libc.time cimport time
8 from libcpp cimport bool
9 from libcpp.vector cimport vector
10 from libcpp.utility cimport pair
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
21 cdef extern from "<fst/util.h>" nogil:
23 # Note that this is a copy, so it should be viewed as read-only.
25 bool FLAGS_fst_error_fatal
28 cdef extern from "<fst/fstlib.h>" namespace "fst" nogil:
31 const uint64 kExpanded
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
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
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
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
100 const uint32 kEncodeLabels
101 const uint32 kEncodeWeights
102 const uint32 kEncodeFlags
104 # Default argument constants.
107 const int64 kNoStateId
123 enum DeterminizeType:
124 DETERMINIZE_FUNCTIONAL
125 DETERMINIZE_NONFUNCTIONAL
126 DETERMINIZE_DISAMBIGUATE
134 enum EpsNormalizeType:
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]:
160 RandGenOptions(const RandArcSelection &, int32, int32, bool, bool)
163 enum ReplaceLabelType:
164 REPLACE_LABEL_NEITHER
175 cdef cppclass SymbolTableTextOptions:
177 SymbolTableTextOptions(bool)
181 cdef cppclass SymbolTable:
185 SymbolTable(const string &)
188 SymbolTable *Read(const string &)
191 SymbolTable *ReadText(const string &, const SymbolTableTextOptions &)
193 int64 AddSymbol(const string &, int64)
195 int64 AddSymbol(const string &)
199 # Aliased so the Cython transpiler can sort out the overloading.
201 string FindSymbol "Find"(int64)
203 int64 FindIndex "Find"(string)
205 # Aliased so the Cython transpiler can sort out the overloading.
207 bool MemberSymbol "Member"(string)
209 bool MemberIndex "Member"(int64)
211 void AddTable(const SymbolTable &)
213 int64 GetNthKey(ssize_t)
217 void SetName(const string &)
221 string LabeledCheckSum()
223 bool Write(const string &)
225 bool WriteText(const string &)
232 SymbolTable *CompactSymbolTable(const SymbolTable &syms)
234 SymbolTable *MergeSymbolTable(const SymbolTable &, const SymbolTable &,
237 SymbolTable *FstReadSymbols(const string &, bool)
240 cdef cppclass SymbolTableIterator:
242 SymbolTableIterator(const SymbolTable &)
255 cdef extern from "<fst/script/fstscript.h>" namespace "fst::script" nogil:
259 cdef cppclass WeightClass:
263 WeightClass(const WeightClass &)
265 WeightClass(const string &, const string &)
272 const WeightClass &Zero(const string &)
275 const WeightClass &One(const string &)
278 const WeightClass &NoWeight(const string &)
280 # Aliased so the Cython transpiler can sort out the overloading.
282 cdef bool Eq "operator=="(const WeightClass &, const WeightClass &)
284 cdef bool Ne "operator!="(const WeightClass &, const WeightClass &)
286 cdef WeightClass Plus(const WeightClass &, const WeightClass &)
288 cdef WeightClass Times(const WeightClass &, const WeightClass &)
290 cdef WeightClass Divide(const WeightClass &, const WeightClass &)
292 cdef WeightClass Power(const WeightClass &, size_t)
295 cdef cppclass ArcClass:
297 ArcClass(const ArcClass &)
299 ArcClass(int64, int64, const WeightClass &, int64)
310 cdef cppclass FstClass:
312 FstClass(const FstClass &)
315 FstClass *Read(const string &)
318 FstClass *ReadFromString(const string &)
322 WeightClass Final(int64)
324 size_t NumArcs(int64)
326 size_t NumInputEpsilons(int64)
328 size_t NumOutputEpsilons(int64)
330 const string &ArcType()
332 const string &FstType()
334 const SymbolTable *InputSymbols()
336 const SymbolTable *OutputSymbols()
338 const string &WeightType()
340 bool Write(const string &)
342 const string WriteToString()
344 uint64 Properties(uint64, bool)
346 bool ValidStateId(int64)
349 cdef cppclass MutableFstClass(FstClass):
351 bool AddArc(int64, const ArcClass &)
355 bool DeleteArcs(int64, size_t)
357 bool DeleteArcs(int64)
359 bool DeleteStates(const vector[int64] &)
363 SymbolTable *MutableInputSymbols()
365 SymbolTable *MutableOutputSymbols()
369 bool ReserveArcs(int64, size_t)
371 void ReserveStates(int64)
375 bool SetFinal(int64, const WeightClass &)
377 void SetInputSymbols(SymbolTable *)
379 void SetOutputSymbols(SymbolTable *)
381 void SetProperties(uint64, uint64)
383 cdef cppclass VectorFstClass(MutableFstClass):
385 VectorFstClass(const FstClass &)
387 VectorFstClass(const string &)
391 cdef cppclass EncodeMapperClass:
393 EncodeMapperClass(const string &, uint32, EncodeType)
395 # Aliases this to "__call__", since Cython doesn't have good support for
397 ArcClass __call__ "operator()"(const ArcClass &)
399 const string &ArcType()
403 uint64 Properties(uint64)
407 const SymbolTable *InputSymbols()
409 const SymbolTable *OutputSymbols()
411 void SetInputSymbols(const SymbolTable *)
413 void SetOutputSymbols(const SymbolTable *)
415 const string &WeightType()
421 cdef cppclass ArcIteratorClass:
423 ArcIteratorClass(const FstClass &, int64)
439 void SetFlags(uint32, uint32)
441 cdef cppclass MutableArcIteratorClass:
443 MutableArcIteratorClass(MutableFstClass *, int64)
455 void SetValue(const ArcClass &)
461 void SetFlags(uint32, uint32)
463 cdef cppclass StateIteratorClass:
465 StateIteratorClass(const FstClass &)
476 ctypedef pair[int64, const FstClass *] LabelFstClassPair
478 ctypedef pair[int64, int64] LabelPair
481 cdef extern from "<fst/script/fstscript.h>" namespace "fst::script" nogil:
486 INPUT_EPSILON_ARC_FILTER
487 OUTPUT_EPSILON_ARC_FILTER
493 cdef void ArcSort(MutableFstClass *, ArcSortType)
495 cdef ClosureType GetClosureType(bool)
497 cdef void Closure(MutableFstClass *, ClosureType)
499 cdef FstClass *CompileFstInternal(istream &, const string &,
500 const string &, const string &,
501 const SymbolTable *, const SymbolTable *,
502 const SymbolTable*, bool, bool, bool, bool,
504 cdef cppclass ComposeOptions:
506 ComposeOptions(bool, ComposeFilter)
508 cdef void Compose(FstClass &, FstClass &, MutableFstClass *,
509 const ComposeOptions &)
511 cdef void Concat(MutableFstClass *, const FstClass &)
513 cdef void Connect(MutableFstClass *)
515 cdef FstClass *Convert(const FstClass &, const string &)
517 cdef void Decode(MutableFstClass *, const EncodeMapperClass &)
519 cdef cppclass DeterminizeOptions:
521 DeterminizeOptions(float, const WeightClass &, int64, int64,
522 DeterminizeType, bool)
524 cdef void Determinize(const FstClass &, MutableFstClass *,
525 const DeterminizeOptions &)
527 cdef cppclass DisambiguateOptions:
529 DisambiguateOptions(float, const WeightClass &, int64, int64)
531 cdef void Disambiguate(const FstClass &, MutableFstClass *,
532 const DisambiguateOptions &)
534 cdef void Difference(const FstClass &, const FstClass &, MutableFstClass *,
535 const ComposeOptions &)
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 &)
542 cdef void Encode(MutableFstClass *, EncodeMapperClass *)
544 cdef EpsNormalizeType GetEpsNormalizeType(bool)
546 cdef void EpsNormalize(const FstClass &, MutableFstClass *, EpsNormalizeType)
548 cdef bool Equal(const FstClass &, const FstClass &, float)
550 cdef bool Equivalent(const FstClass &, const FstClass &, float, bool *)
552 cdef void Intersect(const FstClass &, const FstClass &, MutableFstClass *,
553 const ComposeOptions &)
555 cdef void Invert(MutableFstClass *fst)
557 cdef bool Isomorphic(const FstClass &, const FstClass &, float)
564 OUTPUT_EPSILON_MAPPER
574 cdef FstClass *Map(const FstClass &, MapType, float, const WeightClass &)
576 cdef void Minimize(MutableFstClass *, MutableFstClass *, float, bool)
578 cdef ProjectType GetProjectType(bool)
580 cdef void Project(MutableFstClass *, ProjectType)
582 cdef void PrintFst(const FstClass &, ostream &, const string &,
583 const SymbolTable *, const SymbolTable *,
584 const SymbolTable *, bool, bool, const string &)
586 cdef void Prune(const FstClass &, MutableFstClass *, const WeightClass &,
589 cdef void Prune(MutableFstClass *, const WeightClass &, int64, float)
591 cdef void Push(const FstClass &, MutableFstClass *, uint32 flags,
594 cdef void Push(MutableFstClass *, ReweightType, float, bool)
596 enum RandArcSelection:
598 LOG_PROB_ARC_SELECTOR
599 FAST_LOG_PROB_ARC_SELECTOR
601 cdef bool RandEquivalent(const FstClass &, const FstClass &, int32, float,
602 time_t, const RandGenOptions[RandArcSelection] &,
605 cdef void RandGen(const FstClass &, MutableFstClass *, time_t,
606 const RandGenOptions[RandArcSelection] &)
608 cdef void Relabel(MutableFstClass *,
609 const SymbolTable *, const SymbolTable *, const string &, bool,
610 const SymbolTable *, const SymbolTable *, const string &, bool)
612 cdef void Relabel(MutableFstClass *, const vector[LabelPair] &,
613 const vector[LabelPair] &)
615 cdef cppclass ReplaceOptions:
617 ReplaceOptions(int64, ReplaceLabelType, ReplaceLabelType, int64)
619 cdef void Replace(const vector[LabelFstClassPair] &, MutableFstClass *,
620 const ReplaceOptions &)
622 cdef void Reverse(const FstClass &, MutableFstClass *, bool)
624 cdef void Reweight(MutableFstClass *, const vector[WeightClass] &,
627 cdef cppclass RmEpsilonOptions:
629 RmEpsilonOptions(QueueType, float, bool, const WeightClass &, int64)
631 cdef void RmEpsilon(const FstClass &, MutableFstClass *, bool,
632 const RmEpsilonOptions &)
634 cdef void RmEpsilon(MutableFstClass *, bool, const WeightClass &, int64,
637 cdef cppclass ShortestDistanceOptions:
639 ShortestDistanceOptions(QueueType, ArcFilterType, int64, float)
641 cdef void ShortestDistance(const FstClass &, vector[WeightClass] *,
642 const ShortestDistanceOptions &)
644 cdef void ShortestDistance(const FstClass &, vector[WeightClass] *, bool,
647 cdef cppclass ShortestPathOptions:
649 ShortestPathOptions(QueueType, int32, bool, bool, float, bool,
650 const WeightClass &, int64)
652 cdef void ShortestPath(const FstClass &, MutableFstClass *,
653 vector[WeightClass] *, const ShortestPathOptions &)
655 cdef void Synchronize(const FstClass &, MutableFstClass *)
657 cdef bool TopSort(MutableFstClass *)
659 cdef void Union(MutableFstClass *, const FstClass &)
661 cdef bool Verify(const FstClass &)
664 cdef extern from "<fst/script/getters.h>" namespace "fst::script" nogil:
666 cdef bool GetArcSortType(const string &, ArcSortType *)
668 cdef bool GetComposeFilter(const string &, ComposeFilter *)
670 cdef bool GetDeterminizeType(const string &, DeterminizeType *)
672 cdef uint32 GetEncodeFlags(bool, bool)
674 cdef bool GetMapType(const string &, MapType *)
676 cdef uint32 GetPushFlags(bool, bool, bool, bool)
678 cdef bool GetQueueType(const string &, QueueType *)
680 cdef bool GetRandArcSelection(const string &, RandArcSelection *)
682 cdef bool GetReplaceLabelType(string, bool, ReplaceLabelType *)
684 cdef ReweightType GetReweightType(bool)
687 cdef extern from "<fst/extensions/far/far.h>" namespace "fst" nogil:
696 cdef extern from "<fst/extensions/far/getters.h>" \
697 namespace "fst" nogil:
699 string GetFarTypeString(FarType)
702 cdef extern from "<fst/extensions/far/getters.h>" \
703 namespace "fst::script" nogil:
705 FarType GetFarType(const string &)
708 cdef extern from "<fst/extensions/far/far-class.h>" \
709 namespace "fst::script" nogil:
711 cdef cppclass FarReaderClass:
713 const string &ArcType()
719 bool Find(const string &)
721 const FstClass *GetFstClass()
723 const string &GetKey()
731 # For simplicity, we always use the multiple-file one.
734 FarReaderClass *Open(const vector[string] &)
736 cdef cppclass FarWriterClass:
738 bool Add(const string &, const FstClass &)
742 const string &ArcType()
747 FarWriterClass *Create(const string &, const string &, FarType)