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   Fst<Arc> *fst = NULL;
     63   Fst<LogArc> *lfst = NULL;
     64   Fst<Log64Arc> *l64fst = NULL;
     65   Fst<StdArc> *sfst = NULL;
     66   if (map_type == ARC_SUM_MAPPER) {
     67     args->retval = new FstClass(*(fst =
     68         script::StateMap(ifst, ArcSumMapper<Arc>(ifst))));
     69   } else if (map_type == IDENTITY_MAPPER) {
     70     args->retval = new FstClass(*(fst =
     71         script::ArcMap(ifst, IdentityArcMapper<Arc>())));
     72   } else if (map_type == INVERT_MAPPER) {
     73     args->retval = new FstClass(*(fst =
     74         script::ArcMap(ifst, InvertWeightMapper<Arc>())));
     75   } else if (map_type == PLUS_MAPPER) {
     76     args->retval = new FstClass(*(fst =
     77         script::ArcMap(ifst, PlusMapper<Arc>(w))));
     78   } else if (map_type == QUANTIZE_MAPPER) {
     79     args->retval = new FstClass(*(fst =
     80         script::ArcMap(ifst, QuantizeMapper<Arc>(delta))));
     81   } else if (map_type == RMWEIGHT_MAPPER) {
     82     args->retval = new FstClass(*(fst =
     83         script::ArcMap(ifst, RmWeightMapper<Arc>())));
     84   } else if (map_type == SUPERFINAL_MAPPER) {
     85     args->retval = new FstClass(*(fst =
     86         script::ArcMap(ifst, SuperFinalMapper<Arc>())));
     87   } else if (map_type == TIMES_MAPPER) {
     88     args->retval = new FstClass(*(fst =
     89         script::ArcMap(ifst, TimesMapper<Arc>(w))));
     90   } else if (map_type == TO_LOG_MAPPER) {
     91     args->retval = new FstClass(*(lfst =
     92         script::ArcMap(ifst, WeightConvertMapper<Arc, LogArc>())));
     93   } else if (map_type == TO_LOG64_MAPPER) {
     94     args->retval = new FstClass(*(l64fst =
     95         script::ArcMap(ifst, WeightConvertMapper<Arc, Log64Arc>())));
     96   } else if (map_type == TO_STD_MAPPER) {
     97     args->retval = new FstClass(*(sfst =
     98         script::ArcMap(ifst, WeightConvertMapper<Arc, StdArc>())));
     99   } else {
    100     FSTERROR() << "Error: unknown/unsupported mapper type: "
    101                << map_type;
    102     VectorFst<Arc> *ofst = new VectorFst<Arc>;
    103     ofst->SetProperties(kError, kError);
    104     args->retval = new FstClass(*(fst =ofst));
    105   }
    106   delete sfst;
    107   delete l64fst;
    108   delete lfst;
    109   delete fst;
    110 }
    111 
    112 
    113 #ifdef SWIG
    114 %newobject Map;
    115 #endif
    116 FstClass *Map(const FstClass& f, MapType map_type,
    117          float delta = fst::kDelta,
    118          const WeightClass &w = fst::script::WeightClass::Zero());
    119 
    120 }  // namespace script
    121 }  // namespace fst
    122 
    123 #endif  // FST_SCRIPT_MAP_H_
    124