1f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// fstmap.cc
2f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
3f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Licensed under the Apache License, Version 2.0 (the "License");
4f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// you may not use this file except in compliance with the License.
5f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// You may obtain a copy of the License at
6f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//
7f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//     http://www.apache.org/licenses/LICENSE-2.0
8f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//
9f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Unless required by applicable law or agreed to in writing, software
10f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// distributed under the License is distributed on an "AS IS" BASIS,
11f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// See the License for the specific language governing permissions and
13f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// limitations under the License.
14f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//
15f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Copyright 2005-2010 Google, Inc.
16f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Author: riley@google.com (Michael Riley)
17f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Modified: jpr@google.com (Jake Ratkiewicz) to use FstClass
18f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//
19f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// \file
20f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Applies an operation to each arc of an FST.
21f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//
22f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
23f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#include <string>
24f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
25f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#include <fst/script/map.h>
26f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
27f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian HodsonDEFINE_double(delta, fst::kDelta, "Comparison/quantization delta");
28f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian HodsonDEFINE_string(map_type, "identity",
29f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson              "Map operation, one of: \"arc_sum\", \"identity\", \"invert\", "
30f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson              "\"plus (--weight)\", \"quantize (--delta)\", \"rmweight\", "
31f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson              "\"superfinal\", \"times (--weight)\", \"to_log\", \"to_log64\", "
32f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson              "\"to_standard\"");
33f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian HodsonDEFINE_string(weight, "", "Weight parameter");
34f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
35f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonint main(int argc, char **argv) {
36f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  namespace s = fst::script;
37f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  using fst::script::FstClass;
38f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  using fst::script::MutableFstClass;
39f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  using fst::script::VectorFstClass;
40f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
41f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  string usage = "Applies an operation to each arc of an FST.\n\n  Usage: ";
42f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  usage += argv[0];
43f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  usage += " [in.fst [out.fst]]\n";
44f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
45f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  std::set_new_handler(FailedNewHandler);
46dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin  SET_FLAGS(usage.c_str(), &argc, &argv, true);
47f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  if (argc > 3) {
48f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    ShowUsage();
49f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    return 1;
50f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
51f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
52f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  string in_name = (argc > 1 && strcmp(argv[1], "-") != 0) ? argv[1] : "";
53f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  string out_name = argc > 2 ? argv[2] : "";
54f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
55f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  FstClass *ifst = FstClass::Read(in_name);
56f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  if (!ifst) return 1;
57f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
58f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  s::WeightClass w = !FLAGS_weight.empty() ?
59f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson      s::WeightClass(ifst->WeightType(), FLAGS_weight) :
60f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson      (FLAGS_map_type == "times" ?
61f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson       s::WeightClass::One() : s::WeightClass::Zero());
62f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
63f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  s::MapType mt;
64f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  if (FLAGS_map_type == "arc_sum") {
65f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    mt = s::ARC_SUM_MAPPER;
66f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  } else if (FLAGS_map_type == "identity") {
67f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    mt = s::IDENTITY_MAPPER;
68f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  } else if (FLAGS_map_type == "invert") {
69f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    mt = s::INVERT_MAPPER;
70f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  } else if (FLAGS_map_type == "plus") {
71f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    mt = s::PLUS_MAPPER;
72f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  } else if (FLAGS_map_type == "quantize") {
73f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    mt = s::QUANTIZE_MAPPER;
74f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  } else if (FLAGS_map_type == "rmweight") {
75f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    mt = s::RMWEIGHT_MAPPER;
76f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  } else if (FLAGS_map_type == "superfinal") {
77f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    mt = s::SUPERFINAL_MAPPER;
78f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  } else if (FLAGS_map_type == "times") {
79f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    mt = s::TIMES_MAPPER;
80f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  } else if (FLAGS_map_type == "to_log") {
81f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    mt = s::TO_LOG_MAPPER;
82f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  } else if (FLAGS_map_type == "to_log64") {
83f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    mt = s::TO_LOG64_MAPPER;
84f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  } else if (FLAGS_map_type == "to_standard") {
85f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    mt = s::TO_STD_MAPPER;
86f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  } else {
87f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    LOG(ERROR) << argv[0] << ": Unknown map type \""
88f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson               << FLAGS_map_type << "\"\n";
89f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    return 1;
90f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
91f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
92f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  FstClass *ofst = s::Map(*ifst, mt, FLAGS_delta, w);
93f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
94f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  ofst->Write(out_name);
95f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
96f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  return 0;
97f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson}
98