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.
106 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 cdef WeightClass Plus(const WeightClass &, const WeightClass &)
282 cdef WeightClass Times(const WeightClass &, const WeightClass &)
284 cdef WeightClass Divide(const WeightClass &, const WeightClass &)
286 cdef WeightClass Power(const WeightClass &, size_t)
289 cdef cppclass ArcClass:
291 ArcClass(const ArcClass &)
293 ArcClass(int64, int64, const WeightClass &, int64)
304 cdef cppclass FstClass:
306 FstClass(const FstClass &)
309 FstClass *Read(const string &)
312 FstClass *ReadFromString(const string &)
316 WeightClass Final(int64)
318 size_t NumArcs(int64)
320 size_t NumInputEpsilons(int64)
322 size_t NumOutputEpsilons(int64)
324 const string &ArcType()
326 const string &FstType()
328 const SymbolTable *InputSymbols()
330 const SymbolTable *OutputSymbols()
332 const string &WeightType()
334 bool Write(const string &)
336 const string WriteToString()
338 uint64 Properties(uint64, bool)
340 bool ValidStateId(int64)
343 cdef cppclass MutableFstClass(FstClass):
345 bool AddArc(int64, const ArcClass &)
349 bool DeleteArcs(int64, size_t)
351 bool DeleteArcs(int64)
353 bool DeleteStates(const vector[int64] &)
357 SymbolTable *MutableInputSymbols()
359 SymbolTable *MutableOutputSymbols()
363 bool ReserveArcs(int64, size_t)
365 void ReserveStates(int64)
369 bool SetFinal(int64, const WeightClass &)
371 void SetInputSymbols(SymbolTable *)
373 void SetOutputSymbols(SymbolTable *)
375 void SetProperties(uint64, uint64)
377 cdef cppclass VectorFstClass(MutableFstClass):
379 VectorFstClass(const FstClass &)
381 VectorFstClass(const string &)
385 cdef cppclass EncodeMapperClass:
387 EncodeMapperClass(const string &, uint32, EncodeType)
389 # Aliases this to "__call__", since Cython doesn't have good support for
391 ArcClass __call__ "operator()"(const ArcClass &)
393 const string &ArcType()
397 uint64 Properties(uint64)
401 const SymbolTable *InputSymbols()
403 const SymbolTable *OutputSymbols()
405 void SetInputSymbols(const SymbolTable *)
407 void SetOutputSymbols(const SymbolTable *)
409 const string &WeightType()
415 cdef cppclass ArcIteratorClass:
417 ArcIteratorClass(const FstClass &, int64)
433 void SetFlags(uint32, uint32)
435 cdef cppclass MutableArcIteratorClass:
437 MutableArcIteratorClass(MutableFstClass *, int64)
449 void SetValue(const ArcClass &)
455 void SetFlags(uint32, uint32)
457 cdef cppclass StateIteratorClass:
459 StateIteratorClass(const FstClass &)
470 ctypedef pair[int64, const FstClass *] LabelFstClassPair
472 ctypedef pair[int64, int64] LabelPair
475 cdef extern from "<fst/script/fstscript.h>" namespace "fst::script" nogil:
480 INPUT_EPSILON_ARC_FILTER
481 OUTPUT_EPSILON_ARC_FILTER
487 cdef void ArcSort(MutableFstClass *, ArcSortType)
489 cdef ClosureType GetClosureType(bool)
491 cdef void Closure(MutableFstClass *, ClosureType)
493 cdef FstClass *CompileFstInternal(istream &, const string &,
494 const string &, const string &,
495 const SymbolTable *, const SymbolTable *,
496 const SymbolTable*, bool, bool, bool, bool,
498 cdef cppclass ComposeOptions:
500 ComposeOptions(bool, ComposeFilter)
502 cdef void Compose(FstClass &, FstClass &, MutableFstClass *,
503 const ComposeOptions &)
505 cdef void Concat(MutableFstClass *, const FstClass &)
507 cdef void Connect(MutableFstClass *)
509 cdef FstClass *Convert(const FstClass &, const string &)
511 cdef void Decode(MutableFstClass *, const EncodeMapperClass &)
513 cdef cppclass DeterminizeOptions:
515 DeterminizeOptions(float, const WeightClass &, int64, int64,
516 DeterminizeType, bool)
518 cdef void Determinize(const FstClass &, MutableFstClass *,
519 const DeterminizeOptions &)
521 cdef cppclass DisambiguateOptions:
523 DisambiguateOptions(float, const WeightClass &, int64, int64)
525 cdef void Disambiguate(const FstClass &, MutableFstClass *,
526 const DisambiguateOptions &)
528 cdef void Difference(const FstClass &, const FstClass &, MutableFstClass *,
529 const ComposeOptions &)
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 &)
536 cdef void Encode(MutableFstClass *, EncodeMapperClass *)
538 cdef EpsNormalizeType GetEpsNormalizeType(bool)
540 cdef void EpsNormalize(const FstClass &, MutableFstClass *, EpsNormalizeType)
542 cdef bool Equal(const FstClass &, const FstClass &, float)
544 cdef bool Equivalent(const FstClass &, const FstClass &, float, bool *)
546 cdef void Intersect(const FstClass &, const FstClass &, MutableFstClass *,
547 const ComposeOptions &)
549 cdef void Invert(MutableFstClass *fst)
551 cdef bool Isomorphic(const FstClass &, const FstClass &, float)
558 OUTPUT_EPSILON_MAPPER
568 cdef FstClass *Map(const FstClass &, MapType, float, const WeightClass &)
570 cdef void Minimize(MutableFstClass *, MutableFstClass *, float, bool)
572 cdef ProjectType GetProjectType(bool)
574 cdef void Project(MutableFstClass *, ProjectType)
576 cdef void PrintFst(const FstClass &, ostream &, const string &,
577 const SymbolTable *, const SymbolTable *,
578 const SymbolTable *, bool, bool, const string &)
580 cdef void Prune(const FstClass &, MutableFstClass *, const WeightClass &,
583 cdef void Prune(MutableFstClass *, const WeightClass &, int64, float)
585 cdef void Push(const FstClass &, MutableFstClass *, uint32 flags,
588 cdef void Push(MutableFstClass *, ReweightType, float, bool)
590 enum RandArcSelection:
592 LOG_PROB_ARC_SELECTOR
593 FAST_LOG_PROB_ARC_SELECTOR
595 cdef bool RandEquivalent(const FstClass &, const FstClass &, int32, float,
596 time_t, const RandGenOptions[RandArcSelection] &,
599 cdef void RandGen(const FstClass &, MutableFstClass *, time_t,
600 const RandGenOptions[RandArcSelection] &)
602 cdef void Relabel(MutableFstClass *,
603 const SymbolTable *, const SymbolTable *, const string &, bool,
604 const SymbolTable *, const SymbolTable *, const string &, bool)
606 cdef void Relabel(MutableFstClass *, const vector[LabelPair] &,
607 const vector[LabelPair] &)
609 cdef cppclass ReplaceOptions:
611 ReplaceOptions(int64, ReplaceLabelType, ReplaceLabelType, int64)
613 cdef void Replace(const vector[LabelFstClassPair] &, MutableFstClass *,
614 const ReplaceOptions &)
616 cdef void Reverse(const FstClass &, MutableFstClass *, bool)
618 cdef void Reweight(MutableFstClass *, const vector[WeightClass] &,
621 cdef cppclass RmEpsilonOptions:
623 RmEpsilonOptions(QueueType, float, bool, const WeightClass &, int64)
625 cdef void RmEpsilon(const FstClass &, MutableFstClass *, bool,
626 const RmEpsilonOptions &)
628 cdef void RmEpsilon(MutableFstClass *, bool, const WeightClass &, int64,
631 cdef cppclass ShortestDistanceOptions:
633 ShortestDistanceOptions(QueueType, ArcFilterType, int64, float)
635 cdef void ShortestDistance(const FstClass &, vector[WeightClass] *,
636 const ShortestDistanceOptions &)
638 cdef void ShortestDistance(const FstClass &, vector[WeightClass] *, bool,
641 cdef cppclass ShortestPathOptions:
643 ShortestPathOptions(QueueType, int32, bool, bool, float, bool,
644 const WeightClass &, int64)
646 cdef void ShortestPath(const FstClass &, MutableFstClass *,
647 vector[WeightClass] *, const ShortestPathOptions &)
649 cdef void Synchronize(const FstClass &, MutableFstClass *)
651 cdef bool TopSort(MutableFstClass *)
653 cdef void Union(MutableFstClass *, const FstClass &)
655 cdef bool Verify(const FstClass &)
658 cdef extern from "nlp/fst/script/getters.h" namespace "fst::script" nogil:
660 cdef bool GetArcSortType(const string &, ArcSortType *)
662 cdef bool GetComposeFilter(const string &, ComposeFilter *)
664 cdef bool GetDeterminizeType(const string &, DeterminizeType *)
666 cdef uint32 GetEncodeFlags(bool, bool)
668 cdef bool GetMapType(const string &, MapType *)
670 cdef uint32 GetPushFlags(bool, bool, bool, bool)
672 cdef bool GetQueueType(const string &, QueueType *)
674 cdef bool GetRandArcSelection(const string &, RandArcSelection *)
676 cdef bool GetReplaceLabelType(string, bool, ReplaceLabelType *)
678 cdef ReweightType GetReweightType(bool)
681 cdef extern from "<fst/extensions/far/far.h>" namespace "fst" nogil:
690 cdef extern from "<fst/extensions/far/getters.h>" \
691 namespace "fst" nogil:
693 string GetFarTypeString(FarType)
696 cdef extern from "<fst/extensions/far/getters.h>" \
697 namespace "fst::script" nogil:
699 FarType GetFarType(const string &)
702 cdef extern from "<fst/extensions/far/far-class.h>" \
703 namespace "fst::script" nogil:
705 cdef cppclass FarReaderClass:
707 const string &ArcType()
713 bool Find(const string &)
715 const FstClass *GetFstClass()
717 const string &GetKey()
725 # For simplicity, we always use the multiple-file one.
728 FarReaderClass *Open(const vector[string] &)
730 cdef cppclass FarWriterClass:
732 bool Add(const string &, const FstClass &)
736 const string &ArcType()
741 FarWriterClass *Create(const string &, const string &, FarType)