12 #ifndef TROPICALSPARSETUPLEWEIGHT_H_ 13 #define TROPICALSPARSETUPLEWEIGHT_H_ 15 #include <fst/fstlib.h> 22 class TropicalSparseTupleWeight:
public 23 SparsePowerWeight<TropicalWeightTpl<T> > {
32 typedef TropicalWeightTpl<T>
W;
34 using SparsePowerWeight<W>::Zero;
35 using SparsePowerWeight<W>::One;
36 using SparsePowerWeight<W>::Quantize;
37 using SparsePowerWeight<W>::Reverse;
38 using SparsePowerWeight<W>::Type;
39 using SparsePowerWeight<W>::Properties;
44 SparsePowerWeight<W>() {
45 this->SetDefaultValue (W::One() );
49 SparsePowerWeight<W> (sw) {
53 SparsePowerWeight<W> (w) {
57 int64 size =
sizeof (T);
58 if (size ==
sizeof (
float) ) {
63 Int64ToStr (size, &result);
67 static const std::string&
Type() {
68 static const std::string type =
"tropicalsparsetuple" 74 return W::Properties()
75 & (kLeftSemiring | kRightSemiring | kCommutative | kIdempotent
91 w.SetDefaultValue (this->DefaultValue() );
107 T result = w.DefaultValue().Value();
108 for (SparseTupleWeightIterator<TropicalWeightTpl<T>,
int> it (w); !it.Done();
114 }
else if (it.Value().first > int (vw.size() ) ) {
116 <<
"feature vector has a larger dimensionality than the parameters. " 117 <<
"Params: " << vw.size() <<
" Features: " 118 << it.Value().first << endl;
121 param = vw[it.Value().first - 1];
123 result += it.Value().second.Value() * param;
133 params.Push (index + 1, param);
143 return w1 < w2 ? vw1 : vw2;
149 const SparsePowerWeight<TropicalWeightTpl<T> >& spw1 = vw1;
150 const SparsePowerWeight<TropicalWeightTpl<T> >& spw2 = vw2;
159 SparseTupleWeightTimesMapper<TropicalWeightTpl<T>,
int> operator_mapper;
160 SparseTupleWeightMap (&ret, w1, w2, operator_mapper);
170 SparseTupleWeightDivideMapper<TropicalWeightTpl<T>,
int> operator_mapper (
172 SparseTupleWeightMap (&ret, w1, w2, operator_mapper);
176 template<
typename FromArc,
typename ToArc,
typename M>
184 ToArc operator() (
const FromArc& arc)
const {
185 if (arc.weight == FromArc::Weight::Zero() ) {
186 return ToArc (arc.ilabel, arc.olabel, ToArc::Weight::Zero(),
189 if (arc.weight == FromArc::Weight::One() ) {
190 return ToArc (arc.ilabel, arc.olabel, ToArc::Weight::One(),
193 return ToArc (arc.ilabel, arc.olabel, m (arc.weight), arc.nextstate);
197 return MAP_COPY_SYMBOLS;
200 return MAP_COPY_SYMBOLS;
203 return MAP_NO_SUPERFINAL;
206 return (props & kWeightInvariantProperties) | kUnweighted;
235 for (SparseTupleWeightIterator<TropicalWeight, int> it (w32); !it.Done();
237 result.Push (it.Value().first,
238 TropicalWeightTpl<double> (it.Value().second.Value() ) );
254 TropicalWeightTpl<T> operator() (
260 std::vector<T> param;
271 for (SparseTupleWeightIterator<TropicalWeightTpl<T>,
int> it (w);
272 !it.Done(); it.Next() ) {
273 if (it.Value().first - 1 == k) {
274 return it.Value().second;
277 return w.DefaultValue();
296 result.Push (k + 1, w.Value() );
GeneralMapper< ArcTpl< TropicalSparseTupleWeight< float > >, ArcTpl< TropicalSparseTupleWeight< double > >, Expand > ExpandMapper
MapSymbolsAction OutputSymbolsAction() const
bool ApproxEqual(const TropicalSparseTupleWeight< T > &vw1, const TropicalSparseTupleWeight< T > &vw2, float delta=kDelta)
ReverseWeight Reverse() const
TropicalSparseTupleWeight(W w)
TropicalSparseTupleWeight< T > Quantize(float delta=kDelta) const
static std::vector< T > & Params()
TropicalSparseTupleWeight(const SparsePowerWeight< W > &sw)
fst::GeneralMapper< fst::ArcTpl< fst::TropicalSparseTupleWeight< float > >, fst::ArcTpl< fst::TropicalWeightTpl< float > >, fst::VectorToStd< float > > SparseToStdMapper
TropicalSparseTupleWeight()
static uint64 Properties()
Implements Tropical Sparse tuple weight semiring, extending from openfst SparsePowerWeight class...
TropicalSparseTupleWeight< T > Divide(const TropicalSparseTupleWeight< T > &w1, const TropicalSparseTupleWeight< T > &w2, DivideType type=DIVIDE_ANY)
fst::GeneralMapper< fst::ArcTpl< fst::TropicalWeightTpl< float > >, fst::ArcTpl< fst::TropicalSparseTupleWeight< float > >, fst::StdToVector< float > > StdToSparseMapper
friend TropicalSparseTupleWeight< TT > Plus(const TropicalSparseTupleWeight< TT > &, const TropicalSparseTupleWeight< TT > &)
Functor to convert sparse tuple weight to tropical (single weight)
MapSymbolsAction InputSymbolsAction() const
DotProductMap(const std::vector< T > ¶m)
StdToTropicalSparseMapper(uint k_=1)
TropicalSparseTupleWeight< T > ReverseWeight
MapFinalAction FinalAction() const
Functor that converts tropical to sparse tuple weight.
TropicalSparseTupleWeight< T > Times(const TropicalSparseTupleWeight< T > &w1, const TropicalSparseTupleWeight< T > &w2)
static std::string GetPrecisionString()
uint Properties(uint props) const
static const TropicalSparseTupleWeight< T > & One()
static void AddElement(TropicalSparseTupleWeight< T > ¶ms, unsigned int index, T param)
Map functor used with generic weight mapper.
static const TropicalSparseTupleWeight< T > & Zero()
static const std::string & Type()
T DotProduct(const TropicalSparseTupleWeight< T > &w, const std::vector< T > &vw)
Implements Dot product of two vector weights.