// fst.h
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright 2005-2010 Google, Inc.
// Author: riley@google.com (Michael Riley)
//
// \file
// Finite-State Transducer (FST) - abstract base class definition,
// state and arc iterator interface, and suggested base implementation.
//
#ifndef FST_LIB_FST_H__
#define FST_LIB_FST_H__
#include <stddef.h>
#include <sys/types.h>
#include <cmath>
#include <string>
#include <fst/compat.h>
#include <fst/types.h>
#include <fst/arc.h>
#include <fst/properties.h>
#include <fst/register.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <fst/symbol-table.h>
#include <fst/util.h>
DECLARE_bool(fst_align);
namespace fst {
bool IsFstHeader(istream &, const string &);
class FstHeader;
template <class A> class StateIteratorData;
template <class A> class ArcIteratorData;
template <class A> class MatcherBase;
struct FstReadOptions {
// FileReadMode(s) are advisory, there are many conditions than prevent a
// file from being mapped, READ mode will be selected in these cases with
// a warning indicating why it was chosen.
enum FileReadMode { READ, MAP };
string source; // Where you're reading from
const FstHeader *header; // Pointer to Fst header. If non-zero, use
// this info (don't read a stream header)
const SymbolTable* isymbols; // Pointer to input symbols. If non-zero, use
// this info (read and skip stream isymbols)
const SymbolTable* osymbols; // Pointer to output symbols. If non-zero, use
// this info (read and skip stream osymbols)
FileReadMode mode; // Read or map files (advisory, if possible)
explicit FstReadOptions(const string& src = "<unspecified>",
const FstHeader *hdr = 0,
const SymbolTable* isym = 0,
const SymbolTable* osym = 0);
explicit FstReadOptions(const string& src,
const SymbolTable* isym,
const SymbolTable* osym = 0);
// Helper function to convert strings FileReadModes into their enum value.
static FileReadMode ReadMode(const string &mode);
};
struct FstWriteOptions {
string source; // Where you're writing to
bool write_header; // Write the header?
bool write_isymbols; // Write input symbols?
bool write_osymbols; // Write output symbols?
bool align; // Write data aligned where appropriate;
// this may fail on pipes
explicit FstWriteOptions(const string& src = "<unspecifed>",
bool hdr = true, bool isym = true,
bool osym = true, bool alig = FLAGS_fst_align)
: source(src), write_header(hdr),
write_isymbols(isym), write_osymbols(osym), align(alig) {}
};
//
// Fst HEADER CLASS
//
// This is the recommended Fst file header representation.
//
class FstHeader {
public:
enum {
HAS_ISYMBOLS = 0x1, // Has input symbol table
HAS_OSYMBOLS = 0x2, // Has output symbol table
IS_ALIGNED = 0x4, // Memory-aligned (where appropriate)
} Flags;
FstHeader() : version_(0), flags_(0), properties_(0), start_(-1),
numstates_(0), numarcs_(0) {}
const string &FstType() const { return fsttype_; }
const string &ArcType() const { return arctype_; }
int32 Version() const { return version_; }
int32 GetFlags() const { return flags_; }
uint64 Properties() const { return properties_; }
int64 Start() const { return start_; }
int64 NumStates() const { return numstates_; }
int64 NumArcs() const { return numarcs_; }
void SetFstType(const string& type) { fsttype_ = type; }
void SetArcType(const string& type) { arctype_ = type; }
void SetVersion(int32 version) { version_ = version; }
void SetFlags(int32 flags) { flags_ = flags; }
void SetProperties(uint64 properties) { properties_ = properties; }
void SetStart(int64 start) { start_ = start; }
void SetNumStates(int64 numstates) { numstates_ = numstates; }
void SetNumArcs(int64 numarcs) { numarcs_ = numarcs; }
bool Read(istream &strm, const string &source, bool rewind = false);
bool Write(ostream &strm, const string