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