FST  openfst-1.6.1
OpenFst Library
fst-class.cc
Go to the documentation of this file.
1 // See www.openfst.org for extensive documentation on this weighted
2 // finite-state transducer library.
3 //
4 // These classes are only recommended for use in high-level scripting
5 // applications. Most users should use the lower-level templated versions
6 // corresponding to these classes.
7 
8 #include <istream>
9 
10 #include <fst/log.h>
11 
12 #include <fst/equal.h>
13 #include <fst/fst-decl.h>
14 #include <fst/reverse.h>
15 #include <fst/union.h>
16 #include <fst/script/fst-class.h>
17 #include <fst/script/register.h>
18 
19 namespace fst {
20 namespace script {
21 
22 // Registration.
23 
27 
28 // FstClass methods.
29 
30 template <class FstT>
31 FstT *ReadFst(std::istream &istrm, const string &fname) {
32  if (!istrm) {
33  LOG(ERROR) << "ReadFst: Can't open file: " << fname;
34  return nullptr;
35  }
36  FstHeader hdr;
37  if (!hdr.Read(istrm, fname)) return nullptr;
38  FstReadOptions read_options(fname, &hdr);
39  const auto arc_type = hdr.ArcType();
40  const auto reader =
41  IORegistration<FstT>::Register::GetRegister()->GetReader(arc_type);
42  if (!reader) {
43  LOG(ERROR) << "ReadFst: Unknown arc type: " << arc_type;
44  return nullptr;
45  }
46  return reader(istrm, read_options);
47 }
48 
49 FstClass *FstClass::Read(const string &fname) {
50  if (!fname.empty()) {
51  std::ifstream istrm(fname.c_str(),
52  std::ios_base::in | std::ios_base::binary);
53  return ReadFst<FstClass>(istrm, fname);
54  } else {
55  return ReadFst<FstClass>(std::cin, "standard input");
56  }
57 }
58 
59 FstClass *FstClass::Read(std::istream &istrm, const string &source) {
60  return ReadFst<FstClass>(istrm, source);
61 }
62 
63 FstClass *FstClass::ReadFromString(const string &fst_string) {
64  std::istringstream istrm(fst_string);
65  return ReadFst<FstClass>(istrm, "StringToFst");
66 }
67 
68 const string FstClass::WriteToString() const {
69  std::ostringstream ostrm;
70  Write(ostrm, FstWriteOptions("StringToFst"));
71  return ostrm.str();
72 }
73 
75  const string &op_name) const {
76  if (WeightType() != weight.Type()) {
77  FSTERROR() << "FST and weight with non-matching weight types passed to "
78  << op_name << ": " << WeightType() << " and " << weight.Type();
79  return false;
80  }
81  return true;
82 }
83 
84 // MutableFstClass methods.
85 
86 MutableFstClass *MutableFstClass::Read(const string &fname, bool convert) {
87  if (convert == false) {
88  if (!fname.empty()) {
89  std::ifstream in(fname.c_str(),
90  std::ios_base::in | std::ios_base::binary);
91  return ReadFst<MutableFstClass>(in, fname);
92  } else {
93  return ReadFst<MutableFstClass>(std::cin, "standard input");
94  }
95  } else { // Converts to VectorFstClass if not mutable.
96  FstClass *ifst = FstClass::Read(fname);
97  if (!ifst) return nullptr;
98  if (ifst->Properties(fst::kMutable, false)) {
99  return static_cast<MutableFstClass *>(ifst);
100  } else {
101  MutableFstClass *ofst = new VectorFstClass(*ifst);
102  delete ifst;
103  return ofst;
104  }
105  }
106 }
107 
108 // VectorFstClass methods.
109 
110 VectorFstClass *VectorFstClass::Read(const string &fname) {
111  if (!fname.empty()) {
112  std::ifstream in(fname.c_str(),
113  std::ios_base::in | std::ios_base::binary);
114  return ReadFst<VectorFstClass>(in, fname);
115  } else {
116  return ReadFst<VectorFstClass>(std::cin, "standard input");
117  }
118 }
119 
121  const string &arc_type) {
123  arc_type);
124 }
125 
126 VectorFstClass::VectorFstClass(const string &arc_type)
127  : MutableFstClass(GetVFSTRegisterEntry(arc_type).creator()) {
128  if (Properties(kError, true) == kError) {
129  FSTERROR() << "VectorFstClass: Unknown arc type: " << arc_type;
130  }
131 }
132 
134  : MutableFstClass(GetVFSTRegisterEntry(other.ArcType()).converter(other)) {}
135 
136 } // namespace script
137 } // namespace fst
uint64 Properties(uint64 mask, bool test) const override
Definition: fst-class.h:311
bool WeightTypesMatch(const WeightClass &weight, const string &op_name) const
Definition: fst-class.cc:74
bool Write(const string &fname) const override
Definition: fst-class.h:334
static FstClass * ReadFromString(const string &fst_string)
Definition: fst-class.cc:63
const string & ArcType() const override
Definition: fst-class.h:289
#define LOG(type)
Definition: log.h:48
ArcTpl< TropicalWeight > StdArc
Definition: arc.h:52
const string WriteToString() const
Definition: fst-class.cc:68
#define FSTERROR()
Definition: util.h:32
IORegistration< VectorFstClass >::Entry GetVFSTRegisterEntry(const string &arc_type)
Definition: fst-class.cc:120
const string & ArcType() const
Definition: fst.h:121
static VectorFstClass * Read(const string &fname)
Definition: fst-class.cc:110
bool Read(std::istream &strm, const string &source, bool rewind=false)
Definition: fst.cc:86
VectorFstClass(FstClassImplBase *impl)
Definition: fst-class.h:512
REGISTER_FST_CLASSES(StdArc)
ArcTpl< Log64Weight > Log64Arc
Definition: arc.h:54
constexpr uint64 kError
Definition: properties.h:33
FstT * ReadFst(std::istream &istrm, const string &fname)
Definition: fst-class.cc:31
static MutableFstClass * Read(const string &fname, bool convert=false)
Definition: fst-class.cc:86
const string & Type() const
Definition: weight-class.h:141
constexpr uint64 kMutable
Definition: properties.h:30
const string & WeightType() const override
Definition: fst-class.h:327
static FstClass * Read(const string &fname)
Definition: fst-class.cc:49
ArcTpl< LogWeight > LogArc
Definition: arc.h:53