12 #ifndef FUNCTION_WEIGHT_H_ 13 #define FUNCTION_WEIGHT_H_ 15 #include <fst/fstlib.h> 22 typedef long long Wid;
31 x (-std::numeric_limits<double>::infinity() ),
y (0.0),
m (0.0) {
35 x (-std::numeric_limits<double>::infinity() ), y (y), m (m) {
76 class cl_iterator: std::iterator<std::input_iterator_tag, MertLine> {
129 this->values = values;
141 fst::DivideType
t = fst::DIVIDE_ANY);
143 std::size_t Hash()
const;
147 ReverseWeight Reverse()
const;
149 std::istream& Read (std::istream& strm);
151 std::ostream& Write (std::ostream& strm)
const;
153 fst::TropicalWeightTpl<F>
Map (
F)
const;
156 return fst::kLeftSemiring | fst::kRightSemiring | fst::kCommutative |
167 static const std::string&
Type() {
168 static const std::string type =
"Function";
185 fst::TropicalWeightTpl<F>
Map (
double);
194 ilabel (i), olabel (o), weight (w), nextstate (s) {
200 static const std::string&
Type() {
201 static const std::string type =
"Function";
211 template<
class W,
unsigned int n>
212 F DotProduct (
const W&,
const fst::TupleWeight<fst::TropicalWeightTpl<F>, n>&);
214 template<
unsigned int n>
215 F DotProduct (
const fst::TupleWeight<fst::TropicalWeightTpl<F>, n>& features
216 ,
const std::vector<F>& params) {
218 for (
unsigned int i = 0; i < features.Length(); ++i) {
219 result += features.Value (i).Value() * params[i];
224 template<
class FromArc,
class T>
229 typedef typename FromArc::Weight
FW;
232 const std::vector<T>& initial) :
234 direction (direction), initial (initial) {
236 ToArc operator() (
const FromArc& arc)
const {
238 FW features = arc.weight;
239 if (features == FW::Zero() ) {
240 return ToArc (arc.ilabel, arc.olabel, TW::Zero(), arc.nextstate);
242 if (features == FW::One() ) {
243 return ToArc (arc.ilabel, arc.olabel, TW::One(), arc.nextstate);
248 mlist.push_back (
MertLine (y, m, arc.ilabel) );
251 return ToArc (arc.ilabel, arc.olabel, mapped, arc.nextstate);
254 return fst::MAP_COPY_SYMBOLS;
257 return fst::MAP_COPY_SYMBOLS;
260 return fst::MAP_NO_SUPERFINAL;
268 const std::vector<T>& direction;
270 const std::vector<T>& initial;
278 typedef fst::ArcTpl<fst::TropicalWeightTpl<double> >
ToArc;
280 typedef ToArc::Weight
TW;
284 ToArc operator() (
const FromArc& arc)
const {
292 if (
function.size() != 1) {
293 cout <<
"Function arc has more than one function";
296 TW mapped (
function.front().
m * gamma +
function.front().
y);
300 return fst::MAP_COPY_SYMBOLS;
303 return fst::MAP_COPY_SYMBOLS;
306 return fst::MAP_NO_SUPERFINAL;
FunctionWeight Plus(const FunctionWeight &, const FunctionWeight &)
static uint64 Properties()
fst::ArcTpl< fst::TropicalWeightTpl< double > > ToArc
bool operator==(const MertLine &, const MertLine &)
MertList::iterator MertIter
uint Properties(uint props) const
const MertLine oneLine(0, 0, 0)
bool ApproxEqual(const FunctionWeight &, const FunctionWeight &, float)
fst::TropicalWeightTpl< F > Map(double)
fst::MapFinalAction FinalAction() const
static const FunctionWeight & Zero()
fst::MapSymbolsAction OutputSymbolsAction() const
fst::MapFinalAction FinalAction() const
VectorToFunctionMapper(const std::vector< T > &direction, const std::vector< T > &initial)
FunctionArc(Label i, Label o, Weight w, StateId s)
const MertLine zeroLine(std::numeric_limits< double >::infinity(), 0, 0)
fst::MapSymbolsAction OutputSymbolsAction() const
const MertList & Value() const
FunctionWeight ReverseWeight
TropicalSparseTupleWeight< T > Divide(const TropicalSparseTupleWeight< T > &w1, const TropicalSparseTupleWeight< T > &w2, DivideType type=DIVIDE_ANY)
static const FunctionWeight & One()
static const std::string & Type()
fst::MapSymbolsAction InputSymbolsAction() const
FunctionWeight(MertList &values)
std::ostream & operator<<(std::ostream &, const MertLine &)
fst::MapSymbolsAction InputSymbolsAction() const
static const std::string & Type()
std::list< MertLine > MertList
uint Properties(uint props) const
std::vector< Wid > SentenceIdx
FunctionWeight Times(const FunctionWeight &, const FunctionWeight &)
CompositeList::cl_iterator CompIter
FunctionToStdMapper(double gamma)
std::istream & operator>>(std::istream &, MertList &)
MertLine(double y, double m, Wid word)
F DotProduct(const W &, const fst::TupleWeight< fst::TropicalWeightTpl< F >, n > &)
FunctionWeight(const MertList &values)