From 96a32415ab43377cf1575bd3f4f2980f58028209 Mon Sep 17 00:00:00 2001 From: Determinant Date: Fri, 14 Aug 2015 11:51:42 +0800 Subject: add implementation for kaldi io (by ymz) --- .../src/tools/openfst/include/fst/script/prune.h | 153 +++++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100644 kaldi_io/src/tools/openfst/include/fst/script/prune.h (limited to 'kaldi_io/src/tools/openfst/include/fst/script/prune.h') diff --git a/kaldi_io/src/tools/openfst/include/fst/script/prune.h b/kaldi_io/src/tools/openfst/include/fst/script/prune.h new file mode 100644 index 0000000..7118ff1 --- /dev/null +++ b/kaldi_io/src/tools/openfst/include/fst/script/prune.h @@ -0,0 +1,153 @@ + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// Copyright 2005-2010 Google, Inc. +// Author: jpr@google.com (Jake Ratkiewicz) + +#ifndef FST_SCRIPT_PRUNE_H_ +#define FST_SCRIPT_PRUNE_H_ + +#include +using std::vector; + +#include +#include +#include +#include +#include + +namespace fst { +namespace script { + +struct PruneOptions { + WeightClass weight_threshold; + int64 state_threshold; + const vector *distance; + float delta; + + explicit PruneOptions(const WeightClass& w, int64 s, + vector *d = 0, float e = kDelta) + : weight_threshold(w), + state_threshold(s), + distance(d), + delta(e) {} + private: + PruneOptions(); // disallow +}; + +// converts a script::PruneOptions into a fst::PruneOptions. +// Notes: +// If the original opts.distance is not NULL, a new distance will be +// created with new; it's the client's responsibility to delete this. + +template +fst::PruneOptions > ConvertPruneOptions( + const PruneOptions &opts) { + typedef typename A::Weight Weight; + typedef typename A::StateId StateId; + + Weight weight_threshold = *(opts.weight_threshold.GetWeight()); + StateId state_threshold = opts.state_threshold; + vector *distance = 0; + + if (opts.distance) { + distance = new vector(opts.distance->size()); + for (unsigned i = 0; i < opts.distance->size(); ++i) { + (*distance)[i] = *((*opts.distance)[i].GetWeight()); + } + } + + return fst::PruneOptions >( + weight_threshold, state_threshold, AnyArcFilter(), distance, + opts.delta); +} + +// 1 +typedef args::Package PruneArgs1; + +template +void Prune(PruneArgs1 *args) { + MutableFst *ofst = args->arg1->GetMutableFst(); + + typedef typename Arc::Weight Weight; + typedef typename Arc::StateId StateId; + + fst::PruneOptions > opts = + ConvertPruneOptions(args->arg2); + Prune(ofst, opts); + delete opts.distance; +} + +// 2 +typedef args::Package PruneArgs2; + +template +void Prune(PruneArgs2 *args) { + const Fst& ifst = *(args->arg1.GetFst()); + MutableFst *ofst = args->arg2->GetMutableFst(); + + fst::PruneOptions > opts = + ConvertPruneOptions(args->arg3); + Prune(ifst, ofst, opts); + delete opts.distance; +} + +// 3 +typedef args::Package PruneArgs3; + +template +void Prune(PruneArgs3 *args) { + const Fst& ifst = *(args->arg1.GetFst()); + MutableFst *ofst = args->arg2->GetMutableFst(); + typename Arc::Weight w = *(args->arg3.GetWeight()); + + Prune(ifst, ofst, w, args->arg4, args->arg5); +} + +// 4 +typedef args::Package PruneArgs4; +template +void Prune(PruneArgs4 *args) { + MutableFst *fst = args->arg1->GetMutableFst(); + typename Arc::Weight w = *(args->arg2.GetWeight()); + Prune(fst, w, args->arg3, args->arg4); +} + + +// 1 +void Prune(MutableFstClass *fst, const PruneOptions &opts); + +// 2 +void Prune(const FstClass &ifst, MutableFstClass *fst, + const PruneOptions &opts); + +// 3 +void Prune(const FstClass &ifst, MutableFstClass *ofst, + const WeightClass &weight_threshold, + int64 state_threshold = kNoStateId, + float delta = kDelta); + +// 4 +void Prune(MutableFstClass *fst, const WeightClass& weight_threshold, + int64 state_threshold, float delta); + +} // namespace script +} // namespace fst + + + +#endif // FST_SCRIPT_PRUNE_H_ -- cgit v1.2.3-70-g09d2