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