1
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6//     http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13//
14// Copyright 2005-2010 Google, Inc.
15// Author: jpr@google.com (Jake Ratkiewicz)
16
17#ifndef FST_SCRIPT_PRUNE_H_
18#define FST_SCRIPT_PRUNE_H_
19
20#include <vector>
21using std::vector;
22
23#include <fst/script/arg-packs.h>
24#include <fst/script/fst-class.h>
25#include <fst/script/weight-class.h>
26#include <fst/prune.h>
27#include <fst/arcfilter.h>
28
29namespace fst {
30namespace script {
31
32struct PruneOptions {
33  WeightClass weight_threshold;
34  int64 state_threshold;
35  const vector<WeightClass> *distance;
36  float delta;
37
38  explicit PruneOptions(const WeightClass& w, int64 s,
39                        vector<WeightClass> *d = 0, float e = kDelta)
40      : weight_threshold(w),
41        state_threshold(s),
42        distance(d),
43        delta(e) {}
44 private:
45  PruneOptions();  // disallow
46};
47
48// converts a script::PruneOptions into a fst::PruneOptions.
49// Notes:
50//  If the original opts.distance is not NULL, a new distance will be
51//  created with new; it's the client's responsibility to delete this.
52
53template<class A>
54fst::PruneOptions<A, AnyArcFilter<A> > ConvertPruneOptions(
55    const PruneOptions &opts) {
56  typedef typename A::Weight Weight;
57  typedef typename A::StateId StateId;
58
59  Weight weight_threshold = *(opts.weight_threshold.GetWeight<Weight>());
60  StateId state_threshold = opts.state_threshold;
61  vector<Weight> *distance = 0;
62
63  if (opts.distance) {
64    distance = new vector<Weight>(opts.distance->size());
65    for (unsigned i = 0; i < opts.distance->size(); ++i) {
66      (*distance)[i] = *((*opts.distance)[i].GetWeight<Weight>());
67    }
68  }
69
70  return fst::PruneOptions<A, AnyArcFilter<A> >(
71      weight_threshold, state_threshold, AnyArcFilter<A>(), distance,
72      opts.delta);
73}
74
75// 1
76typedef args::Package<MutableFstClass *, const PruneOptions &> PruneArgs1;
77
78template<class Arc>
79void Prune(PruneArgs1 *args) {
80  MutableFst<Arc> *ofst = args->arg1->GetMutableFst<Arc>();
81
82  typedef typename Arc::Weight Weight;
83  typedef typename Arc::StateId StateId;
84
85  fst::PruneOptions<Arc, AnyArcFilter<Arc> > opts =
86      ConvertPruneOptions<Arc>(args->arg2);
87  Prune(ofst, opts);
88  delete opts.distance;
89}
90
91// 2
92typedef args::Package<const FstClass &, MutableFstClass *,
93                      const PruneOptions &> PruneArgs2;
94
95template<class Arc>
96void Prune(PruneArgs2 *args) {
97  const Fst<Arc>& ifst = *(args->arg1.GetFst<Arc>());
98  MutableFst<Arc> *ofst = args->arg2->GetMutableFst<Arc>();
99
100  fst::PruneOptions<Arc, AnyArcFilter<Arc> > opts =
101      ConvertPruneOptions<Arc>(args->arg3);
102  Prune(ifst, ofst, opts);
103  delete opts.distance;
104}
105
106// 3
107typedef args::Package<const FstClass &,
108                      MutableFstClass *,
109                      const WeightClass &, int64, float> PruneArgs3;
110
111template<class Arc>
112void Prune(PruneArgs3 *args) {
113  const Fst<Arc>& ifst = *(args->arg1.GetFst<Arc>());
114  MutableFst<Arc> *ofst = args->arg2->GetMutableFst<Arc>();
115  typename Arc::Weight w = *(args->arg3.GetWeight<typename Arc::Weight>());
116
117  Prune(ifst, ofst, w, args->arg4, args->arg5);
118}
119
120// 4
121typedef args::Package<MutableFstClass *, const WeightClass&,
122                      int64, float> PruneArgs4;
123template<class Arc>
124void Prune(PruneArgs4 *args) {
125  MutableFst<Arc> *fst = args->arg1->GetMutableFst<Arc>();
126  typename Arc::Weight w = *(args->arg2.GetWeight<typename Arc::Weight>());
127  Prune(fst, w, args->arg3, args->arg4);
128}
129
130
131// 1
132void Prune(MutableFstClass *fst, const PruneOptions &opts);
133
134// 2
135void Prune(const FstClass &ifst, MutableFstClass *fst,
136           const PruneOptions &opts);
137
138// 3
139void Prune(const FstClass &ifst, MutableFstClass *ofst,
140           const WeightClass &weight_threshold,
141           int64 state_threshold = kNoStateId,
142           float delta = kDelta);
143
144// 4
145void Prune(MutableFstClass *fst, const WeightClass& weight_threshold,
146           int64 state_threshold, float delta);
147
148}  // namespace script
149}  // namespace fst
150
151
152
153#endif  // FST_SCRIPT_PRUNE_H_
154