1// fstconnect.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: riley@google.com (Michael Riley)
17// Modified: jpr@google.com (Jake Ratkiewicz) to use FstClass
18//
19// \file
20// Removes useless (inaccessible or non-coaccessible) states and arcs
21// from an FST.
22//
23
24#include <fst/script/connect.h>
25
26int main(int argc, char **argv) {
27  namespace s = fst::script;
28  using fst::script::FstClass;
29  using fst::script::MutableFstClass;
30
31  string usage = "Removes useless states and arcs from an FST.\n\n  Usage: ";
32  usage += argv[0];
33  usage += " [in.fst [out.fst]]\n";
34
35  std::set_new_handler(FailedNewHandler);
36  SET_FLAGS(usage.c_str(), &argc, &argv, true);
37  if (argc > 3) {
38    ShowUsage();
39    return 1;
40  }
41
42  string in_name = (argc > 1 && strcmp(argv[1], "-") != 0) ? argv[1] : "";
43  string out_name = argc > 2 ? argv[2] : "";
44
45  MutableFstClass *fst = MutableFstClass::Read(in_name, true);
46  if (!fst) return 1;
47
48  s::Connect(fst);
49  fst->Write(out_name);
50
51  return 0;
52}
53