FST  openfst-1.7.9
OpenFst Library
replace.h
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 #ifndef FST_SCRIPT_REPLACE_H_
5 #define FST_SCRIPT_REPLACE_H_
6 
7 #include <tuple>
8 #include <utility>
9 #include <vector>
10 
11 #include <fst/types.h>
12 #include <fst/replace.h>
13 #include <fst/script/fst-class.h>
14 
15 namespace fst {
16 namespace script {
17 
19  const int64 root; // Root rule for expansion.
20  const ReplaceLabelType call_label_type; // How to label call arc.
21  const ReplaceLabelType return_label_type; // How to label return arc.
22  const int64 return_label; // Specifies return arc label.
23 
24  explicit ReplaceOptions(
25  int64 root, ReplaceLabelType call_label_type = REPLACE_LABEL_INPUT,
26  ReplaceLabelType return_label_type = REPLACE_LABEL_NEITHER,
27  int64 return_label = 0)
28  : root(root),
29  call_label_type(call_label_type),
30  return_label_type(return_label_type),
31  return_label(return_label) {}
32 };
33 
34 using ReplaceArgs =
35  std::tuple<const std::vector<std::pair<int64, const FstClass *>> &,
37 
38 template <class Arc>
39 void Replace(ReplaceArgs *args) {
40  // Now that we know the arc type, we construct a vector of
41  // std::pair<real label, real fst> that the real Replace will use.
42  const auto &untyped_pairs = std::get<0>(*args);
43  std::vector<std::pair<typename Arc::Label, const Fst<Arc> *>> typed_pairs;
44  typed_pairs.reserve(untyped_pairs.size());
45  for (const auto &untyped_pair : untyped_pairs) {
46  typed_pairs.emplace_back(untyped_pair.first, // Converts label.
47  untyped_pair.second->GetFst<Arc>());
48  }
49  MutableFst<Arc> *ofst = std::get<1>(*args)->GetMutableFst<Arc>();
50  const auto &opts = std::get<2>(*args);
51  ReplaceFstOptions<Arc> typed_opts(opts.root, opts.call_label_type,
52  opts.return_label_type, opts.return_label);
53  ReplaceFst<Arc> rfst(typed_pairs, typed_opts);
54  // Checks for cyclic dependencies before attempting expansion.
55  if (rfst.CyclicDependencies()) {
56  FSTERROR() << "Replace: Cyclic dependencies detected; cannot expand";
57  ofst->SetProperties(kError, kError);
58  return;
59  }
60  typed_opts.gc = true; // Caching options to speed up batch copy.
61  typed_opts.gc_limit = 0;
62  *ofst = rfst;
63 }
64 
65 void Replace(const std::vector<std::pair<int64, const FstClass *>> &pairs,
66  MutableFstClass *ofst, const ReplaceOptions &opts);
67 
68 } // namespace script
69 } // namespace fst
70 
71 #endif // FST_SCRIPT_REPLACE_H_
const int64 return_label
Definition: replace.h:22
ReplaceLabelType
Definition: replace-util.h:29
void Replace(ReplaceArgs *args)
Definition: replace.h:39
ReplaceOptions(int64 root, ReplaceLabelType call_label_type=REPLACE_LABEL_INPUT, ReplaceLabelType return_label_type=REPLACE_LABEL_NEITHER, int64 return_label=0)
Definition: replace.h:24
bool CyclicDependencies() const
Definition: replace.h:1004
const ReplaceLabelType call_label_type
Definition: replace.h:20
int64_t int64
Definition: types.h:27
#define FSTERROR()
Definition: util.h:36
std::tuple< const std::vector< std::pair< int64, const FstClass * >> &, MutableFstClass *, const ReplaceOptions & > ReplaceArgs
Definition: replace.h:36
const ReplaceLabelType return_label_type
Definition: replace.h:21
constexpr uint64 kError
Definition: properties.h:36
virtual void SetProperties(uint64 props, uint64 mask)=0