FST  openfst-1.7.3
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 <fst/script/fst-class.h>
9 
10 #include <istream>
11 
12 #include <fst/log.h>
13 #include <fst/equal.h>
14 #include <fst/fst-decl.h>
15 #include <fst/reverse.h>
16 #include <fst/union.h>
17 #include <fst/script/register.h>
18 
19 namespace fst {
20 namespace script {
21 
22 // Registration.
23 
27 
28 // Helper functions.
29 
30 namespace {
31 
32 template <class F>
33 F *ReadFstClass(std::istream &istrm, const std::string &fname) {
34  if (!istrm) {
35  LOG(ERROR) << "ReadFstClass: Can't open file: " << fname;
36  return nullptr;
37  }
38  FstHeader hdr;
39  if (!hdr.Read(istrm, fname)) return nullptr;
40  const FstReadOptions read_options(fname, &hdr);
41  const auto &arc_type = hdr.ArcType();
42  static const auto *io_register = IORegistration<F>::Register::GetRegister();
43  const auto reader = io_register->GetReader(arc_type);
44  if (!reader) {
45  LOG(ERROR) << "ReadFstClass: Unknown arc type: " << arc_type;
46  return nullptr;
47  }
48  return reader(istrm, read_options);
49 }
50 
51 template <class F>
52 FstClassImplBase *CreateFstClass(const std::string &arc_type) {
53  static const auto *io_register =
55  auto creator = io_register->GetCreator(arc_type);
56  if (!creator) {
57  FSTERROR() << "CreateFstClass: Unknown arc type: " << arc_type;
58  return nullptr;
59  }
60  return creator();
61 }
62 
63 template <class F>
64 FstClassImplBase *ConvertFstClass(const FstClass &other) {
65  static const auto *io_register =
67  auto converter = io_register->GetConverter(other.ArcType());
68  if (!converter) {
69  FSTERROR() << "ConvertFstClass: Unknown arc type: " << other.ArcType();
70  return nullptr;
71  }
72  return converter(other);
73 }
74 
75 } // namespace
76 
77 
78 // FstClass methods.
79 
80 FstClass *FstClass::Read(const std::string &fname) {
81  if (!fname.empty()) {
82  std::ifstream istrm(fname, std::ios_base::in | std::ios_base::binary);
83  return ReadFstClass<FstClass>(istrm, fname);
84  } else {
85  return ReadFstClass<FstClass>(std::cin, "standard input");
86  }
87 }
88 
89 FstClass *FstClass::Read(std::istream &istrm, const std::string &source) {
90  return ReadFstClass<FstClass>(istrm, source);
91 }
92 
94  const std::string &op_name) const {
95  if (WeightType() != weight.Type()) {
96  FSTERROR() << "FST and weight with non-matching weight types passed to "
97  << op_name << ": " << WeightType() << " and " << weight.Type();
98  return false;
99  }
100  return true;
101 }
102 
103 // MutableFstClass methods.
104 
105 MutableFstClass *MutableFstClass::Read(const std::string &fname, bool convert) {
106  if (convert == false) {
107  if (!fname.empty()) {
108  std::ifstream in(fname, std::ios_base::in | std::ios_base::binary);
109  return ReadFstClass<MutableFstClass>(in, fname);
110  } else {
111  return ReadFstClass<MutableFstClass>(std::cin, "standard input");
112  }
113  } else { // Converts to VectorFstClass if not mutable.
114  std::unique_ptr<FstClass> ifst(FstClass::Read(fname));
115  if (!ifst) return nullptr;
116  if (ifst->Properties(kMutable, false) == kMutable) {
117  return static_cast<MutableFstClass *>(ifst.release());
118  } else {
119  return new VectorFstClass(*ifst.release());
120  }
121  }
122 }
123 
124 // VectorFstClass methods.
125 
126 VectorFstClass *VectorFstClass::Read(const std::string &fname) {
127  if (!fname.empty()) {
128  std::ifstream in(fname, std::ios_base::in | std::ios_base::binary);
129  return ReadFstClass<VectorFstClass>(in, fname);
130  } else {
131  return ReadFstClass<VectorFstClass>(std::cin, "standard input");
132  }
133 }
134 
135 VectorFstClass::VectorFstClass(const std::string &arc_type)
136  : MutableFstClass(CreateFstClass<VectorFstClass>(arc_type)) {}
137 
139  : MutableFstClass(ConvertFstClass<VectorFstClass>(other)) {}
140 
141 } // namespace script
142 } // namespace fst
bool WeightTypesMatch(const WeightClass &weight, const std::string &op_name) const
Definition: fst-class.cc:93
static VectorFstClass * Read(const std::string &fname)
Definition: fst-class.cc:126
const std::string & Type() const
Definition: weight-class.h:144
#define LOG(type)
Definition: log.h:46
ArcTpl< TropicalWeight > StdArc
Definition: arc.h:59
const std::string & WeightType() const final
Definition: fst-class.h:334
#define FSTERROR()
Definition: util.h:35
static FstClass * Read(const std::string &fname)
Definition: fst-class.cc:80
static MutableFstClass * Read(const std::string &fname, bool convert=false)
Definition: fst-class.cc:105
VectorFstClass(FstClassImplBase *impl)
Definition: fst-class.h:510
REGISTER_FST_CLASSES(StdArc)
ArcTpl< Log64Weight > Log64Arc
Definition: arc.h:61
constexpr uint64 kMutable
Definition: properties.h:31
ArcTpl< LogWeight > LogArc
Definition: arc.h:60