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