Home | History | Annotate | Download | only in bin
      1 // fstmap.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 (at) google.com (Michael Riley)
     17 // Modified: jpr (at) google.com (Jake Ratkiewicz) to use FstClass
     18 //
     19 // \file
     20 // Applies an operation to each arc of an FST.
     21 //
     22 
     23 #include <string>
     24 
     25 #include <fst/script/map.h>
     26 
     27 DEFINE_double(delta, fst::kDelta, "Comparison/quantization delta");
     28 DEFINE_string(map_type, "identity",
     29               "Map operation, one of: \"arc_sum\", \"identity\", \"invert\", "
     30               "\"plus (--weight)\", \"quantize (--delta)\", \"rmweight\", "
     31               "\"superfinal\", \"times (--weight)\", \"to_log\", \"to_log64\", "
     32               "\"to_standard\"");
     33 DEFINE_string(weight, "", "Weight parameter");
     34 
     35 int main(int argc, char **argv) {
     36   namespace s = fst::script;
     37   using fst::script::FstClass;
     38   using fst::script::MutableFstClass;
     39   using fst::script::VectorFstClass;
     40 
     41   string usage = "Applies an operation to each arc of an FST.\n\n  Usage: ";
     42   usage += argv[0];
     43   usage += " [in.fst [out.fst]]\n";
     44 
     45   std::set_new_handler(FailedNewHandler);
     46   SetFlags(usage.c_str(), &argc, &argv, true);
     47   if (argc > 3) {
     48     ShowUsage();
     49     return 1;
     50   }
     51 
     52   string in_name = (argc > 1 && strcmp(argv[1], "-") != 0) ? argv[1] : "";
     53   string out_name = argc > 2 ? argv[2] : "";
     54 
     55   FstClass *ifst = FstClass::Read(in_name);
     56   if (!ifst) return 1;
     57 
     58   s::WeightClass w = !FLAGS_weight.empty() ?
     59       s::WeightClass(ifst->WeightType(), FLAGS_weight) :
     60       (FLAGS_map_type == "times" ?
     61        s::WeightClass::One() : s::WeightClass::Zero());
     62 
     63   s::MapType mt;
     64   if (FLAGS_map_type == "arc_sum") {
     65     mt = s::ARC_SUM_MAPPER;
     66   } else if (FLAGS_map_type == "identity") {
     67     mt = s::IDENTITY_MAPPER;
     68   } else if (FLAGS_map_type == "invert") {
     69     mt = s::INVERT_MAPPER;
     70   } else if (FLAGS_map_type == "plus") {
     71     mt = s::PLUS_MAPPER;
     72   } else if (FLAGS_map_type == "quantize") {
     73     mt = s::QUANTIZE_MAPPER;
     74   } else if (FLAGS_map_type == "rmweight") {
     75     mt = s::RMWEIGHT_MAPPER;
     76   } else if (FLAGS_map_type == "superfinal") {
     77     mt = s::SUPERFINAL_MAPPER;
     78   } else if (FLAGS_map_type == "times") {
     79     mt = s::TIMES_MAPPER;
     80   } else if (FLAGS_map_type == "to_log") {
     81     mt = s::TO_LOG_MAPPER;
     82   } else if (FLAGS_map_type == "to_log64") {
     83     mt = s::TO_LOG64_MAPPER;
     84   } else if (FLAGS_map_type == "to_standard") {
     85     mt = s::TO_STD_MAPPER;
     86   } else {
     87     LOG(ERROR) << argv[0] << ": Unknown map type \""
     88                << FLAGS_map_type << "\"\n";
     89     return 1;
     90   }
     91 
     92   FstClass *ofst = s::Map(*ifst, mt, FLAGS_delta, w);
     93 
     94   ofst->Write(out_name);
     95 
     96   return 0;
     97 }
     98