FST  openfst-1.8.3
OpenFst Library
replace.h
Go to the documentation of this file.
1 // Copyright 2005-2024 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/log.h>
27 #include <fst/fst.h>
28 #include <fst/mutable-fst.h>
29 #include <fst/properties.h>
30 #include <fst/replace-util.h>
31 #include <fst/replace.h>
32 #include <fst/util.h>
33 #include <fst/script/fst-class.h>
34 
35 namespace fst {
36 namespace script {
37 
39  const int64_t root; // Root rule for expansion.
40  const ReplaceLabelType call_label_type; // How to label call arc.
41  const ReplaceLabelType return_label_type; // How to label return arc.
42  const int64_t return_label; // Specifies return arc label.
43 
44  explicit ReplaceOptions(
45  int64_t root, ReplaceLabelType call_label_type = REPLACE_LABEL_INPUT,
46  ReplaceLabelType return_label_type = REPLACE_LABEL_NEITHER,
47  int64_t return_label = 0)
48  : root(root),
49  call_label_type(call_label_type),
50  return_label_type(return_label_type),
51  return_label(return_label) {}
52 };
53 
54 using FstReplaceArgs =
55  std::tuple<const std::vector<std::pair<int64_t, const FstClass *>> &,
57 
58 template <class Arc>
59 void Replace(FstReplaceArgs *args) {
60  // Now that we know the arc type, we construct a vector of
61  // std::pair<real label, real fst> that the real Replace will use.
62  const auto &untyped_pairs = std::get<0>(*args);
63  std::vector<std::pair<typename Arc::Label, const Fst<Arc> *>> typed_pairs;
64  typed_pairs.reserve(untyped_pairs.size());
65  for (const auto &untyped_pair : untyped_pairs) {
66  typed_pairs.emplace_back(untyped_pair.first, // Converts label.
67  untyped_pair.second->GetFst<Arc>());
68  }
69  MutableFst<Arc> *ofst = std::get<1>(*args)->GetMutableFst<Arc>();
70  const auto &opts = std::get<2>(*args);
71  ReplaceFstOptions<Arc> typed_opts(opts.root, opts.call_label_type,
72  opts.return_label_type, opts.return_label);
73  ReplaceFst<Arc> rfst(typed_pairs, typed_opts);
74  // Checks for cyclic dependencies before attempting expansion.
75  if (rfst.CyclicDependencies()) {
76  FSTERROR() << "Replace: Cyclic dependencies detected; cannot expand";
77  ofst->SetProperties(kError, kError);
78  return;
79  }
80  typed_opts.gc = true; // Caching options to speed up batch copy.
81  typed_opts.gc_limit = 0;
82  *ofst = rfst;
83 }
84 
85 void Replace(const std::vector<std::pair<int64_t, const FstClass *>> &pairs,
86  MutableFstClass *ofst, const ReplaceOptions &opts);
87 
88 } // namespace script
89 } // namespace fst
90 
91 #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:75
ReplaceLabelType
Definition: replace-util.h:48
constexpr uint64_t kError
Definition: properties.h:52
std::tuple< const std::vector< std::pair< int64_t, const FstClass * >> &, MutableFstClass *, const ReplaceOptions & > FstReplaceArgs
Definition: replace.h:56
bool CyclicDependencies() const
Definition: replace.h:1049
const ReplaceLabelType call_label_type
Definition: replace.h:40
#define FSTERROR()
Definition: util.h:56
const int64_t return_label
Definition: replace.h:42
virtual void SetProperties(uint64_t props, uint64_t mask)=0
const ReplaceLabelType return_label_type
Definition: replace.h:41
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:44
const int64_t root
Definition: replace.h:39