Home | History | Annotate | Download | only in script
      1 
      2 // Licensed under the Apache License, Version 2.0 (the "License");
      3 // you may not use this file except in compliance with the License.
      4 // You may obtain a copy of the License at
      5 //
      6 //     http://www.apache.org/licenses/LICENSE-2.0
      7 //
      8 // Unless required by applicable law or agreed to in writing, software
      9 // distributed under the License is distributed on an "AS IS" BASIS,
     10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     11 // See the License for the specific language governing permissions and
     12 // limitations under the License.
     13 //
     14 // Copyright 2005-2010 Google, Inc.
     15 // Author: jpr (at) google.com (Jake Ratkiewicz)
     16 
     17 #ifndef FST_SCRIPT_MAP_H_
     18 #define FST_SCRIPT_MAP_H_
     19 
     20 #include <fst/script/arg-packs.h>
     21 #include <fst/script/fst-class.h>
     22 #include <fst/script/weight-class.h>
     23 #include <fst/arc-map.h>
     24 #include <fst/state-map.h>
     25 
     26 namespace fst {
     27 namespace script {
     28 
     29 template <class M>
     30 Fst<typename M::ToArc> *ArcMap(const Fst<typename M::FromArc> &fst,
     31                             const M &mapper) {
     32   typedef typename M::ToArc ToArc;
     33   VectorFst<ToArc> *ofst = new VectorFst<ToArc>;
     34   ArcMap(fst, ofst, mapper);
     35   return ofst;
     36 }
     37 
     38 template <class M>
     39 Fst<typename M::ToArc> *StateMap(const Fst<typename M::FromArc> &fst,
     40                                  const M &mapper) {
     41   typedef typename M::ToArc ToArc;
     42   VectorFst<ToArc> *ofst = new VectorFst<ToArc>;
     43   StateMap(fst, ofst, mapper);
     44   return ofst;
     45 }
     46 
     47 enum MapType { ARC_SUM_MAPPER, IDENTITY_MAPPER, INVERT_MAPPER, PLUS_MAPPER,
     48                QUANTIZE_MAPPER, RMWEIGHT_MAPPER, SUPERFINAL_MAPPER,
     49                TIMES_MAPPER, TO_LOG_MAPPER, TO_LOG64_MAPPER, TO_STD_MAPPER };
     50 
     51 typedef args::Package<const FstClass&, MapType, float,
     52                       const WeightClass &> MapInnerArgs;
     53 typedef args::WithReturnValue<FstClass*, MapInnerArgs> MapArgs;
     54 
     55 template <class Arc>
     56 void Map(MapArgs *args) {
     57   const Fst<Arc> &ifst = *(args->args.arg1.GetFst<Arc>());
     58   MapType map_type = args->args.arg2;
     59   float delta =  args->args.arg3;
     60   typename Arc::Weight w = *(args->args.arg4.GetWeight<typename Arc::Weight>());
     61 
     62   if (map_type == ARC_SUM_MAPPER) {
     63     args->retval = new FstClass(
     64         script::StateMap(ifst, ArcSumMapper<Arc>(ifst)));
     65   } else if (map_type == IDENTITY_MAPPER) {
     66     args->retval = new FstClass(
     67         script::ArcMap(ifst, IdentityArcMapper<Arc>()));
     68   } else if (map_type == INVERT_MAPPER) {
     69     args->retval = new FstClass(
     70         script::ArcMap(ifst, InvertWeightMapper<Arc>()));
     71   } else if (map_type == PLUS_MAPPER) {
     72     args->retval = new FstClass(
     73         script::ArcMap(ifst, PlusMapper<Arc>(w)));
     74   } else if (map_type == QUANTIZE_MAPPER) {
     75     args->retval = new FstClass(
     76         script::ArcMap(ifst, QuantizeMapper<Arc>(delta)));
     77   } else if (map_type == RMWEIGHT_MAPPER) {
     78     args->retval = new FstClass(
     79         script::ArcMap(ifst, RmWeightMapper<Arc>()));
     80   } else if (map_type == SUPERFINAL_MAPPER) {
     81     args->retval = new FstClass(
     82         script::ArcMap(ifst, SuperFinalMapper<Arc>()));
     83   } else if (map_type == TIMES_MAPPER) {
     84     args->retval = new FstClass(
     85         script::ArcMap(ifst, TimesMapper<Arc>(w)));
     86   } else if (map_type == TO_LOG_MAPPER) {
     87     args->retval = new FstClass(
     88         script::ArcMap(ifst, WeightConvertMapper<Arc, LogArc>()));
     89   } else if (map_type == TO_LOG64_MAPPER) {
     90     args->retval = new FstClass(
     91         script::ArcMap(ifst, WeightConvertMapper<Arc, Log64Arc>()));
     92   } else if (map_type == TO_STD_MAPPER) {
     93     args->retval = new FstClass(
     94         script::ArcMap(ifst, WeightConvertMapper<Arc, StdArc>()));
     95   } else {
     96     FSTERROR() << "Error: unknown/unsupported mapper type: "
     97                << map_type;
     98     VectorFst<Arc> *ofst = new VectorFst<Arc>;
     99     ofst->SetProperties(kError, kError);
    100     args->retval = new FstClass(ofst);
    101   }
    102 }
    103 
    104 
    105 #ifdef SWIG
    106 %newobject Map;
    107 #endif
    108 FstClass *Map(const FstClass& f, MapType map_type,
    109          float delta = fst::kDelta,
    110          const WeightClass &w = fst::script::WeightClass::Zero());
    111 
    112 }  // namespace script
    113 }  // namespace fst
    114 
    115 #endif  // FST_SCRIPT_MAP_H_
    116