1// fstprune.cc
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// Copyright 2005-2010 Google, Inc.
16// Author: allauzen@google.com (Cyril Allauzen)
17// Modified: jpr@google.com (Jake Ratkiewicz) to use FstClass
18//
19// \file
20// Prunes states and arcs of an FST w.r.t. the shortest path weight.
21//
22
23#include <fst/script/prune.h>
24
25DEFINE_double(delta, fst::kDelta, "Comparison/quantization delta");
26DEFINE_int64(nstate, fst::kNoStateId, "State number threshold");
27DEFINE_string(weight, "", "Weight threshold");
28
29
30int main(int argc, char **argv) {
31  namespace s = fst::script;
32  using fst::script::FstClass;
33  using fst::script::MutableFstClass;
34  using fst::script::WeightClass;
35
36  string usage = "Prunes states and arcs of an FST.\n\n  Usage: ";
37  usage += argv[0];
38  usage += " [in.fst [out.fst]]\n";
39
40  std::set_new_handler(FailedNewHandler);
41  SetFlags(usage.c_str(), &argc, &argv, true);
42  if (argc > 3) {
43    ShowUsage();
44    return 1;
45  }
46
47  string in_name = (argc > 1 && strcmp(argv[1], "-") != 0) ? argv[1] : "";
48  string out_name = argc > 2 ? argv[2] : "";
49
50  MutableFstClass *fst = MutableFstClass::Read(in_name, true);
51  if (!fst) return 1;
52
53  WeightClass weight_threshold = FLAGS_weight.empty() ?
54      WeightClass::Zero() :
55      WeightClass(fst->WeightType(), FLAGS_weight);
56
57  s::PruneOptions opts(weight_threshold, FLAGS_nstate, 0, FLAGS_delta);
58
59  s::Prune(fst, opts);
60
61  fst->Write(out_name);
62
63  return 0;
64}
65