FST  openfst-1.8.2.post1
OpenFst Library
replace.h
Go to the documentation of this file.
1 // Copyright 2005-2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the 'License');
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an 'AS IS' BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // See www.openfst.org for extensive documentation on this weighted
16 // finite-state transducer library.
17 
18 #ifndef FST_SCRIPT_REPLACE_H_
19 #define FST_SCRIPT_REPLACE_H_
20 
21 #include <cstdint>
22 #include <tuple>
23 #include <utility>
24 #include <vector>
25 
26 #include <fst/replace.h>
27 #include <fst/script/fst-class.h>
28 
29 namespace fst {
30 namespace script {
31 
33  const int64_t root; // Root rule for expansion.
34  const ReplaceLabelType call_label_type; // How to label call arc.
35  const ReplaceLabelType return_label_type; // How to label return arc.
36  const int64_t return_label; // Specifies return arc label.
37 
38  explicit ReplaceOptions(
39  int64_t root, ReplaceLabelType call_label_type = REPLACE_LABEL_INPUT,
40  ReplaceLabelType return_label_type = REPLACE_LABEL_NEITHER,
41  int64_t return_label = 0)
42  : root(root),
43  call_label_type(call_label_type),
44  return_label_type(return_label_type),
45  return_label(return_label) {}
46 };
47 
48 using FstReplaceArgs =
49  std::tuple<const std::vector<std::pair<int64_t, const FstClass *>> &,
51 
52 template <class Arc>
53 void Replace(FstReplaceArgs *args) {
54  // Now that we know the arc type, we construct a vector of
55  // std::pair<real label, real fst> that the real Replace will use.
56  const auto &untyped_pairs = std::get<0>(*args);
57  std::vector<std::pair<typename Arc::Label, const Fst<Arc> *>> typed_pairs;
58  typed_pairs.reserve(untyped_pairs.size());
59  for (const auto &untyped_pair : untyped_pairs) {
60  typed_pairs.emplace_back(untyped_pair.first, // Converts label.
61  untyped_pair.second->GetFst<Arc>());
62  }
63  MutableFst<Arc> *ofst = std::get<1>(*args)->GetMutableFst<Arc>();
64  const auto &opts = std::get<2>(*args);
65  ReplaceFstOptions<Arc> typed_opts(opts.root, opts.call_label_type,
66  opts.return_label_type, opts.return_label);
67  ReplaceFst<Arc> rfst(typed_pairs, typed_opts);
68  // Checks for cyclic dependencies before attempting expansion.
69  if (rfst.CyclicDependencies()) {
70  FSTERROR() << "Replace: Cyclic dependencies detected; cannot expand";
71  ofst->SetProperties(kError, kError);
72  return;
73  }
74  typed_opts.gc = true; // Caching options to speed up batch copy.
75  typed_opts.gc_limit = 0;
76  *ofst = rfst;
77 }
78 
79 void Replace(const std::vector<std::pair<int64_t, const FstClass *>> &pairs,
80  MutableFstClass *ofst, const ReplaceOptions &opts);
81 
82 } // namespace script
83 } // namespace fst
84 
85 #endif // FST_SCRIPT_REPLACE_H_
void Replace(const std::vector< std::pair< int64_t, const FstClass * >> &pairs, MutableFstClass *ofst, std::vector< std::pair< int64_t, int64_t >> *parens, int64_t root, PdtParserType parser_type, int64_t start_paren_labels, const std::string &left_paren_prefix, const std::string &right_paren_prefix)
Definition: pdtscript.cc:72
ReplaceLabelType
Definition: replace-util.h:43
constexpr uint64_t kError
Definition: properties.h:51
std::tuple< const std::vector< std::pair< int64_t, const FstClass * >> &, MutableFstClass *, const ReplaceOptions & > FstReplaceArgs
Definition: replace.h:50
bool CyclicDependencies() const
Definition: replace.h:1040
const ReplaceLabelType call_label_type
Definition: replace.h:34
#define FSTERROR()
Definition: util.h:53
const int64_t return_label
Definition: replace.h:36
virtual void SetProperties(uint64_t props, uint64_t mask)=0
const ReplaceLabelType return_label_type
Definition: replace.h:35
ReplaceOptions(int64_t root, ReplaceLabelType call_label_type=REPLACE_LABEL_INPUT, ReplaceLabelType return_label_type=REPLACE_LABEL_NEITHER, int64_t return_label=0)
Definition: replace.h:38
const int64_t root
Definition: replace.h:33