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