1// fstdifference.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// Subtracts an unweighted DFA from an FSA.
21//
22
23#include <fst/script/difference.h>
24#include <fst/script/connect.h>
25
26DEFINE_string(compose_filter, "auto",
27              "Composition filter, one of: \"alt_sequence\", \"auto\","
28              " \"match\", \"sequence\"");
29DEFINE_bool(connect, true, "Trim output");
30
31int main(int argc, char **argv) {
32  namespace s = fst::script;
33  using fst::script::FstClass;
34  using fst::script::MutableFstClass;
35  using fst::script::VectorFstClass;
36
37  string usage = "Subtracts an unweighted DFA from an FSA.\n\n  Usage: ";
38  usage += argv[0];
39  usage += " in1.fst in2.fst [out.fst]\n";
40
41  std::set_new_handler(FailedNewHandler);
42  SET_FLAGS(usage.c_str(), &argc, &argv, true);
43  if (argc < 3 || argc > 4) {
44    ShowUsage();
45    return 1;
46  }
47
48  string in1_name = strcmp(argv[1], "-") == 0 ? "" : argv[1];
49  string in2_name = strcmp(argv[2], "-") == 0 ? "" : argv[2];
50  string out_name = argc > 3 ? argv[3] : "";
51
52  if (in1_name.empty() && in2_name.empty()) {
53    LOG(ERROR) << argv[0] << ": Can't take both inputs from standard input.";
54    return 1;
55  }
56
57  FstClass *ifst1 = FstClass::Read(in1_name);
58  if (!ifst1) return 1;
59  FstClass *ifst2 = FstClass::Read(in2_name);
60  if (!ifst2) return 1;
61
62  VectorFstClass ofst(ifst1->ArcType());
63
64  fst::ComposeFilter cf;
65
66  if (FLAGS_compose_filter == "auto") {
67    cf = fst::AUTO_FILTER;
68  } else if (FLAGS_compose_filter == "sequence") {
69    cf = fst::SEQUENCE_FILTER;
70  } else if (FLAGS_compose_filter == "alt_sequence") {
71    cf = fst::ALT_SEQUENCE_FILTER;
72  } else if (FLAGS_compose_filter == "match") {
73    cf = fst::MATCH_FILTER;
74  } else {
75    LOG(ERROR) << argv[0] << ": Bad filter type \""
76               << FLAGS_compose_filter << "\"";
77    return 1;
78  }
79
80  fst::DifferenceOptions opts(FLAGS_connect, cf);
81
82  s::Difference(*ifst1, *ifst2, &ofst, opts);
83
84  ofst.Write(out_name);
85
86  return 0;
87}
88