FST  openfst-1.8.3
OpenFst Library
prune.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_PRUNE_H_
19 #define FST_SCRIPT_PRUNE_H_
20 
21 #include <cstdint>
22 #include <tuple>
23 #include <utility>
24 
25 #include <fst/log.h>
26 #include <fst/fst.h>
27 #include <fst/mutable-fst.h>
28 #include <fst/properties.h>
29 #include <fst/prune.h>
30 #include <fst/util.h>
31 #include <fst/weight.h>
32 #include <fst/script/fst-class.h>
34 
35 namespace fst {
36 namespace script {
37 
38 using FstPruneArgs1 = std::tuple<const FstClass &, MutableFstClass *,
39  const WeightClass &, int64_t, float>;
40 
41 template <class Arc>
42 void Prune(FstPruneArgs1 *args) {
43  using Weight = typename Arc::Weight;
44  const Fst<Arc> &ifst = *std::get<0>(*args).GetFst<Arc>();
45  MutableFst<Arc> *ofst = std::get<1>(*args)->GetMutableFst<Arc>();
46  if constexpr (IsPath<Weight>::value) {
47  const auto weight_threshold = *std::get<2>(*args).GetWeight<Weight>();
48  Prune(ifst, ofst, weight_threshold, std::get<3>(*args), std::get<4>(*args));
49  } else {
50  FSTERROR() << "Prune: Weight must have path property: " << Weight::Type();
51  ofst->SetProperties(kError, kError);
52  }
53 }
54 
55 using FstPruneArgs2 =
56  std::tuple<MutableFstClass *, const WeightClass &, int64_t, float>;
57 
58 template <class Arc>
59 void Prune(FstPruneArgs2 *args) {
60  using Weight = typename Arc::Weight;
61  MutableFst<Arc> *fst = std::get<0>(*args)->GetMutableFst<Arc>();
62  if constexpr (IsPath<Weight>::value) {
63  const auto weight_threshold = *std::get<1>(*args).GetWeight<Weight>();
64  Prune(fst, weight_threshold, std::get<2>(*args), std::get<3>(*args));
65  } else {
66  FSTERROR() << "Prune: Weight must have path property: " << Weight::Type();
68  }
69 }
70 
71 void Prune(const FstClass &ifst, MutableFstClass *ofst,
72  const WeightClass &weight_threshold,
73  int64_t state_threshold = kNoStateId, float delta = kDelta);
74 
75 void Prune(MutableFstClass *fst, const WeightClass &weight_threshold,
76  int64_t state_threshold = kNoStateId, float delta = kDelta);
77 
78 } // namespace script
79 } // namespace fst
80 
81 #endif // FST_SCRIPT_PRUNE_H_
std::tuple< MutableFstClass *, const WeightClass &, int64_t, float > FstPruneArgs2
Definition: prune.h:56
constexpr uint64_t kError
Definition: properties.h:52
constexpr int kNoStateId
Definition: fst.h:196
#define FSTERROR()
Definition: util.h:56
virtual void SetProperties(uint64_t props, uint64_t mask)=0
std::tuple< const FstClass &, MutableFstClass *, const WeightClass &, int64_t, float > FstPruneArgs1
Definition: prune.h:39
constexpr float kDelta
Definition: weight.h:133
void Prune(FstPruneArgs1 *args)
Definition: prune.h:42
std::bool_constant<(W::Properties()&kPath)!=0 > IsPath
Definition: weight.h:162