fst.pxd 16.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
# See www.openfst.org for extensive documentation on this weighted
# finite-state transducer library.


from libc.time cimport time_t
from libc.time cimport time

from libcpp cimport bool
from libcpp.vector cimport vector
from libcpp.utility cimport pair

from libcpp.string cimport string
from basictypes cimport int32
from basictypes cimport int64
from basictypes cimport uint32
from basictypes cimport uint64
from ios cimport istream
from ios cimport ostream


cdef extern from "<fst/util.h>" nogil:

  # Note that this is a copy, so it should be viewed as read-only.

  bool FLAGS_fst_error_fatal


cdef extern from "<fst/fstlib.h>" namespace "fst" nogil:

  # FST properties.
  const uint64 kExpanded
  const uint64 kMutable
  const uint64 kError
  const uint64 kAcceptor
  const uint64 kNotAcceptor
  const uint64 kIDeterministic
  const uint64 kNonIDeterministic
  const uint64 kODeterministic
  const uint64 kNonODeterministic
  const uint64 kEpsilons
  const uint64 kNoEpsilons
  const uint64 kIEpsilons
  const uint64 kNoIEpsilons
  const uint64 kOEpsilons
  const uint64 kNoOEpsilons
  const uint64 kILabelSorted
  const uint64 kNotILabelSorted
  const uint64 kOLabelSorted
  const uint64 kNotOLabelSorted
  const uint64 kWeighted
  const uint64 kUnweighted
  const uint64 kCyclic
  const uint64 kAcyclic
  const uint64 kInitialCyclic
  const uint64 kInitialAcyclic
  const uint64 kTopSorted
  const uint64 kNotTopSorted
  const uint64 kAccessible
  const uint64 kNotAccessible
  const uint64 kCoAccessible
  const uint64 kNotCoAccessible
  const uint64 kString
  const uint64 kNotString
  const uint64 kWeightedCycles
  const uint64 kUnweightedCycles
  const uint64 kNullProperties
  const uint64 kCopyProperties
  const uint64 kIntrinsicProperties
  const uint64 kExtrinsicProperties
  const uint64 kSetStartProperties
  const uint64 kSetFinalProperties
  const uint64 kAddStateProperties
  const uint64 kAddArcProperties
  const uint64 kSetArcProperties
  const uint64 kDeleteStatesProperties
  const uint64 kDeleteArcsProperties
  const uint64 kStateSortProperties
  const uint64 kArcSortProperties
  const uint64 kILabelInvariantProperties
  const uint64 kOLabelInvariantProperties
  const uint64 kWeightInvariantProperties
  const uint64 kAddSuperFinalProperties
  const uint64 kRmSuperFinalProperties
  const uint64 kBinaryProperties
  const uint64 kTrinaryProperties
  const uint64 kPosTrinaryProperties
  const uint64 kNegTrinaryProperties
  const uint64 kFstProperties

  # ArcIterator flags.
  const uint32 kArcILabelValue
  const uint32 kArcOLabelValue
  const uint32 kArcWeightValue
  const uint32 kArcNextStateValue
  const uint32 kArcNoCache
  const uint32 kArcValueFlags
  const uint32 kArcFlags

  # EncodeMapper flags.
  const uint32 kEncodeLabels
  const uint32 kEncodeWeights
  const uint32 kEncodeFlags

  # Default argument constants.
  const float kDelta
  const float kShortestDelta
  const int kNoLabel
  const int kNoStateId
  const int64 kNoSymbol

  enum ClosureType:
    CLOSURE_STAR
    CLOSURE_PLUS


  enum ComposeFilter:
    AUTO_FILTER
    NULL_FILTER
    SEQUENCE_FILTER
    ALT_SEQUENCE_FILTER
    MATCH_FILTER
    TRIVIAL_FILTER


  cdef cppclass ComposeOptions:

    ComposeOptions(bool, ComposeFilter)


  enum DeterminizeType:
    DETERMINIZE_FUNCTIONAL
    DETERMINIZE_NONFUNCTIONAL
    DETERMINIZE_DISAMBIGUATE


  enum EncodeType:
    DECODE
    ENCODE


  enum EpsNormalizeType:
    EPS_NORM_INPUT
    EPS_NORM_OUTPUT


  enum ProjectType:
    PROJECT_INPUT
    PROJECT_OUTPUT


  enum QueueType:
    TRIVIAL_QUEUE
    FIFO_QUEUE
    LIFO_QUEUE
    SHORTEST_FIRST_QUEUE
    TOP_ORDER_QUEUE
    STATE_ORDER_QUEUE
    SCC_QUEUE
    AUTO_QUEUE
    OTHER_QUEUE


  # This is a templated struct at the C++ level, but Cython does not support
  # templated structs unless we pretend they are full-blown classes.
  cdef cppclass RandGenOptions[RandArcSelection]:

    RandGenOptions(const RandArcSelection &, int32, int32, bool, bool)


  enum ReplaceLabelType:
    REPLACE_LABEL_NEITHER
    REPLACE_LABEL_INPUT
    REPLACE_LABEL_OUTPUT
    REPLACE_LABEL_BOTH


  enum ReweightType:
    REWEIGHT_TO_INITIAL
    REWEIGHT_TO_FINAL


  cdef cppclass SymbolTableTextOptions:

    SymbolTableTextOptions(bool)

  # Symbol tables.
  cdef cppclass SymbolTable:

    SymbolTable()

    SymbolTable(const string &)

    @staticmethod
    SymbolTable *Read(const string &)

    @staticmethod
    SymbolTable *ReadText(const string &, const SymbolTableTextOptions &)

    int64 AddSymbol(const string &, int64)

    int64 AddSymbol(const string &)

    SymbolTable *Copy()

    # Aliased for overload.
    string FindSymbol "Find"(int64)

    # Aliased for overload.
    int64 FindIndex "Find"(string)

    # Aliased for overload.
    bool MemberSymbol "Member"(string)

    # Aliased for overload.
    bool MemberIndex "Member"(int64)

    void AddTable(const SymbolTable &)

    int64 GetNthKey(ssize_t)

    const string &Name()

    void SetName(const string &)

    const string &CheckSum()

    const string &LabeledCheckSum()

    bool Write(const string &)

    bool WriteText(const string &)

    int64 AvailableKey()

    size_t NumSymbols()


  SymbolTable *CompactSymbolTable(const SymbolTable &syms)

  SymbolTable *MergeSymbolTable(const SymbolTable &, const SymbolTable &,
                                bool *)

  SymbolTable *FstReadSymbols(const string &, bool)


  cdef cppclass SymbolTableIterator:

    SymbolTableIterator(const SymbolTable &)

    bool Done()

    void Next()

    void Reset()

    string Symbol()

    int64 Value()


cdef extern from "<fst/script/fstscript.h>" namespace "fst::script" nogil:


  # Weights.
  cdef cppclass WeightClass:

    WeightClass()

    WeightClass(const WeightClass &)

    WeightClass(const string &, const string &)

    const string &Type()

    string ToString()

    @staticmethod
    const WeightClass &Zero(const string &)

    @staticmethod
    const WeightClass &One(const string &)

    @staticmethod
    const WeightClass &NoWeight(const string &)

  # Alias.
  cdef bool Eq "operator=="(const WeightClass &, const WeightClass &)

  # Alias.
  cdef bool Ne "operator!="(const WeightClass &, const WeightClass &)

  cdef WeightClass Plus(const WeightClass &, const WeightClass &)

  cdef WeightClass Times(const WeightClass &, const WeightClass &)

  cdef WeightClass Divide(const WeightClass &, const WeightClass &)

  cdef WeightClass Power(const WeightClass &, size_t)

  # Arcs.
  cdef cppclass ArcClass:

    ArcClass(const ArcClass &)

    ArcClass(int64, int64, const WeightClass &, int64)

    int64 ilabel
    int64 olabel
    WeightClass weight
    int64 nextstate


  # FSTs.


  cdef cppclass FstClass:

    FstClass(const FstClass &)

    @staticmethod
    FstClass *Read(const string &)

    # Aliased for overload.
    @staticmethod
    FstClass *ReadFromStream "Read"(istream &, const string &)

    int64 Start()

    WeightClass Final(int64)

    size_t NumArcs(int64)

    size_t NumInputEpsilons(int64)

    size_t NumOutputEpsilons(int64)

    const string &ArcType()

    const string &FstType()

    const SymbolTable *InputSymbols()

    const SymbolTable *OutputSymbols()

    const string &WeightType()

    bool Write(const string &)

    bool Write(ostream &, const string &)

    uint64 Properties(uint64, bool)

    bool ValidStateId(int64)


  cdef cppclass MutableFstClass(FstClass):

    bool AddArc(int64, const ArcClass &)

    int64 AddState()

    bool DeleteArcs(int64, size_t)

    bool DeleteArcs(int64)

    bool DeleteStates(const vector[int64] &)

    void DeleteStates()

    SymbolTable *MutableInputSymbols()

    SymbolTable *MutableOutputSymbols()

    int64 NumStates()

    bool ReserveArcs(int64, size_t)

    void ReserveStates(int64)

    bool SetStart(int64)

    bool SetFinal(int64, const WeightClass &)

    void SetInputSymbols(SymbolTable *)

    void SetOutputSymbols(SymbolTable *)

    void SetProperties(uint64, uint64)

  cdef cppclass VectorFstClass(MutableFstClass):

   VectorFstClass(const FstClass &)

   VectorFstClass(const string &)


  # EncodeMapper.
  cdef cppclass EncodeMapperClass:

    EncodeMapperClass(const string &, uint32, EncodeType)

    # Aliased to __call__ as Cython doesn't have good support for operator().
    ArcClass __call__ "operator()"(const ArcClass &)

    const string &ArcType()

    uint32 Flags()

    uint64 Properties(uint64)

    EncodeType Type()

    const SymbolTable *InputSymbols()

    const SymbolTable *OutputSymbols()

    void SetInputSymbols(const SymbolTable *)

    void SetOutputSymbols(const SymbolTable *)

    const string &WeightType()


  # Iterators.


  cdef cppclass ArcIteratorClass:

    ArcIteratorClass(const FstClass &, int64)

    bool Done()

    ArcClass Value()

    void Next()

    void Reset()

    void Seek(size_t)

    size_t Position()

    uint32 Flags()

    void SetFlags(uint32, uint32)

  cdef cppclass MutableArcIteratorClass:

    MutableArcIteratorClass(MutableFstClass *, int64)

    bool Done()

    ArcClass Value()

    void Next()

    void Reset()

    void Seek(size_t)

    void SetValue(const ArcClass &)

    size_t Position()

    uint32 Flags()

    void SetFlags(uint32, uint32)

  cdef cppclass StateIteratorClass:

    StateIteratorClass(const FstClass &)

    bool Done()

    int64 Value()

    void Next()

    void Reset()


ctypedef pair[int64, const FstClass *] LabelFstClassPair

ctypedef pair[int64, int64] LabelPair


cdef extern from "<fst/script/fstscript.h>" namespace "fst::script" nogil:

  enum ArcFilterType:
    ANY_ARC_FILTER
    EPSILON_ARC_FILTER
    INPUT_EPSILON_ARC_FILTER
    OUTPUT_EPSILON_ARC_FILTER

  enum ArcSortType:
    ILABEL_SORT
    OLABEL_SORT

  cdef void ArcSort(MutableFstClass *, ArcSortType)

  cdef ClosureType GetClosureType(bool)

  cdef void Closure(MutableFstClass *, ClosureType)

  cdef FstClass *CompileFstInternal(istream &, const string &,
                                    const string &, const string &,
                                    const SymbolTable *, const SymbolTable *,
                                    const SymbolTable*, bool, bool, bool, bool,
                                    bool)

  cdef void Compose(FstClass &, FstClass &, MutableFstClass *,
                    const ComposeOptions &)

  cdef void Concat(MutableFstClass *, const FstClass &)

  cdef void Connect(MutableFstClass *)

  cdef FstClass *Convert(const FstClass &, const string &)

  cdef void Decode(MutableFstClass *, const EncodeMapperClass &)

  cdef cppclass DeterminizeOptions:

    DeterminizeOptions(float, const WeightClass &, int64, int64,
                       DeterminizeType, bool)

  cdef void Determinize(const FstClass &, MutableFstClass *,
                        const DeterminizeOptions &)

  cdef cppclass DisambiguateOptions:

    DisambiguateOptions(float, const WeightClass &, int64, int64)

  cdef void Disambiguate(const FstClass &, MutableFstClass *,
                         const DisambiguateOptions &)

  cdef void Difference(const FstClass &, const FstClass &, MutableFstClass *,
                       const ComposeOptions &)

  cdef void DrawFst(const FstClass &fst, const SymbolTable *,
                    const SymbolTable *, const SymbolTable *, bool,
                    const string &, float, float, bool, bool, float, float, int,
                    int, const string &, bool, ostream *, const string &)

  cdef void Encode(MutableFstClass *, EncodeMapperClass *)

  cdef EpsNormalizeType GetEpsNormalizeType(bool)

  cdef void EpsNormalize(const FstClass &, MutableFstClass *, EpsNormalizeType)

  cdef bool Equal(const FstClass &, const FstClass &, float)

  cdef bool Equivalent(const FstClass &, const FstClass &, float)

  cdef void Intersect(const FstClass &, const FstClass &, MutableFstClass *,
                      const ComposeOptions &)

  cdef void Invert(MutableFstClass *fst)

  cdef bool Isomorphic(const FstClass &, const FstClass &, float)

  enum MapType:
    ARC_SUM_MAPPER
    IDENTITY_MAPPER
    INPUT_EPSILON_MAPPER
    INVERT_MAPPER
    OUTPUT_EPSILON_MAPPER
    PLUS_MAPPER
    QUANTIZE_MAPPER
    RMWEIGHT_MAPPER
    SUPERFINAL_MAPPER
    TIMES_MAPPER
    TO_LOG_MAPPER
    TO_LOG64_MAPPER
    TO_STD_MAPPER

  cdef FstClass *Map(const FstClass &, MapType, float, double,
                     const WeightClass &)

  cdef void Minimize(MutableFstClass *, MutableFstClass *, float, bool)

  cdef ProjectType GetProjectType(bool)

  cdef void Project(MutableFstClass *, ProjectType)

  cdef void PrintFst(const FstClass &, ostream &, const string &,
                     const SymbolTable *, const SymbolTable *,
                     const SymbolTable *, bool, bool, const string &)

  cdef void Prune(const FstClass &, MutableFstClass *, const WeightClass &,
                  int64, float)

  cdef void Prune(MutableFstClass *, const WeightClass &, int64, float)

  cdef void Push(const FstClass &, MutableFstClass *, uint32 flags,
                 ReweightType, float)

  cdef void Push(MutableFstClass *, ReweightType, float, bool)

  enum RandArcSelection:
    UNIFORM_ARC_SELECTOR
    LOG_PROB_ARC_SELECTOR
    FAST_LOG_PROB_ARC_SELECTOR

  cdef bool RandEquivalent(const FstClass &, const FstClass &, int32, float,
                           time_t, const RandGenOptions[RandArcSelection] &)

  cdef void RandGen(const FstClass &, MutableFstClass *, time_t,
                    const RandGenOptions[RandArcSelection] &)

  cdef void Relabel(MutableFstClass *, const SymbolTable *,
                    const SymbolTable *, const string &, bool,
                    const SymbolTable *, const SymbolTable *, const string &,
                    bool)

  cdef void Relabel(MutableFstClass *, const vector[LabelPair] &,
                    const vector[LabelPair] &)

  cdef cppclass ReplaceOptions:

     ReplaceOptions(int64, ReplaceLabelType, ReplaceLabelType, int64)

  cdef void Replace(const vector[LabelFstClassPair] &, MutableFstClass *,
                    const ReplaceOptions &)

  cdef void Reverse(const FstClass &, MutableFstClass *, bool)

  cdef void Reweight(MutableFstClass *, const vector[WeightClass] &,
                     ReweightType)

  cdef cppclass RmEpsilonOptions:

    RmEpsilonOptions(QueueType, bool, const WeightClass &, int64, float)

  cdef void RmEpsilon(MutableFstClass *, const RmEpsilonOptions &)

  cdef cppclass ShortestDistanceOptions:

    ShortestDistanceOptions(QueueType, ArcFilterType, int64, float)

  cdef void ShortestDistance(const FstClass &, vector[WeightClass] *,
                             const ShortestDistanceOptions &)

  cdef void ShortestDistance(const FstClass &, vector[WeightClass] *, bool,
                             float)

  cdef cppclass ShortestPathOptions:

    ShortestPathOptions(QueueType, int32, bool, float, const WeightClass &,
                        int64)

  cdef void ShortestPath(const FstClass &, MutableFstClass *,
                         const ShortestPathOptions &)

  cdef void Synchronize(const FstClass &, MutableFstClass *)

  cdef bool TopSort(MutableFstClass *)

  cdef void Union(MutableFstClass *, const FstClass &)

  cdef bool Verify(const FstClass &)


cdef extern from "<fst/script/getters.h>" namespace "fst::script" nogil:

  cdef bool GetArcSortType(const string &, ArcSortType *)

  cdef bool GetComposeFilter(const string &, ComposeFilter *)

  cdef bool GetDeterminizeType(const string &, DeterminizeType *)

  cdef uint32 GetEncodeFlags(bool, bool)

  cdef bool GetMapType(const string &, MapType *)

  cdef uint32 GetPushFlags(bool, bool, bool, bool)

  cdef bool GetQueueType(const string &, QueueType *)

  cdef bool GetRandArcSelection(const string &, RandArcSelection *)

  cdef bool GetReplaceLabelType(string, bool, ReplaceLabelType *)

  cdef ReweightType GetReweightType(bool)


cdef extern from "<fst/extensions/far/far.h>" namespace "fst" nogil:

  enum FarType:
    FAR_DEFAULT
    FAR_STTABLE
    FAR_STLIST
    FAR_FST
    FAR_SSTABLE

cdef extern from "<fst/extensions/far/getters.h>" \
    namespace "fst" nogil:

  string GetFarTypeString(FarType)


cdef extern from "<fst/extensions/far/getters.h>" \
    namespace "fst::script" nogil:

  FarType GetFarType(const string &)


cdef extern from "<fst/extensions/far/far-class.h>" \
    namespace "fst::script" nogil:

  cdef cppclass FarReaderClass:

    const string &ArcType()

    bool Done()

    bool Error()

    bool Find(const string &)

    const FstClass *GetFstClass()

    const string &GetKey()

    void Next()

    void Reset()

    FarType Type()

    # For simplicity, we always use the multiple-file one.

    @staticmethod
    FarReaderClass *Open(const vector[string] &)

  cdef cppclass FarWriterClass:

    bool Add(const string &, const FstClass &)

    bool Error()

    const string &ArcType()

    FarType Type()

    @staticmethod
    FarWriterClass *Create(const string &, const string &, FarType)