2008-05-14 11:57:45 +04:00
|
|
|
/*
|
|
|
|
* FeatureStats.h
|
2012-02-20 03:29:53 +04:00
|
|
|
* mert - Minimum Error Rate Training
|
2008-05-14 11:57:45 +04:00
|
|
|
*
|
|
|
|
* Created by Nicola Bertoldi on 13/05/08.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-02-20 04:46:08 +04:00
|
|
|
#ifndef MERT_FEATURE_STATS_H_
|
|
|
|
#define MERT_FEATURE_STATS_H_
|
2008-05-14 11:57:45 +04:00
|
|
|
|
2011-11-14 14:52:21 +04:00
|
|
|
#include <cstring>
|
2008-05-14 11:57:45 +04:00
|
|
|
#include <iostream>
|
2011-11-14 10:15:30 +04:00
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2014-07-21 14:04:43 +04:00
|
|
|
|
|
|
|
#include <boost/unordered_map.hpp>
|
2014-08-02 01:44:35 +04:00
|
|
|
#include "util/string_piece.hh"
|
2011-11-14 10:15:30 +04:00
|
|
|
#include "Types.h"
|
2008-05-14 11:57:45 +04:00
|
|
|
|
2012-06-30 23:23:45 +04:00
|
|
|
namespace MosesTuning
|
|
|
|
{
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2012-06-30 23:23:45 +04:00
|
|
|
|
2011-11-12 03:58:23 +04:00
|
|
|
// Minimal sparse vector
|
2013-05-29 21:16:15 +04:00
|
|
|
class SparseVector
|
|
|
|
{
|
2011-11-12 04:24:19 +04:00
|
|
|
public:
|
2012-05-10 02:51:05 +04:00
|
|
|
typedef std::map<std::size_t,FeatureStatsType> fvector_t;
|
|
|
|
typedef std::map<std::string, std::size_t> name2id_t;
|
2011-11-12 04:24:19 +04:00
|
|
|
typedef std::vector<std::string> id2name_t;
|
2011-10-14 11:40:53 +04:00
|
|
|
|
2011-11-14 09:00:47 +04:00
|
|
|
FeatureStatsType get(const std::string& name) const;
|
2012-05-10 02:51:05 +04:00
|
|
|
FeatureStatsType get(std::size_t id) const;
|
2011-11-14 09:00:47 +04:00
|
|
|
void set(const std::string& name, FeatureStatsType value);
|
2014-07-21 14:04:43 +04:00
|
|
|
void set(size_t id, FeatureStatsType value);
|
2011-11-12 04:24:19 +04:00
|
|
|
void clear();
|
2012-01-13 20:52:15 +04:00
|
|
|
void load(const std::string& file);
|
2013-05-29 21:16:15 +04:00
|
|
|
std::size_t size() const {
|
|
|
|
return m_fvector.size();
|
|
|
|
}
|
|
|
|
|
2011-11-12 04:24:19 +04:00
|
|
|
void write(std::ostream& out, const std::string& sep = " ") const;
|
2011-10-14 11:40:53 +04:00
|
|
|
|
2011-11-12 04:24:19 +04:00
|
|
|
SparseVector& operator-=(const SparseVector& rhs);
|
2014-07-21 14:04:43 +04:00
|
|
|
SparseVector& operator+=(const SparseVector& rhs);
|
2012-01-13 20:52:15 +04:00
|
|
|
FeatureStatsType inner_product(const SparseVector& rhs) const;
|
2011-10-14 11:40:53 +04:00
|
|
|
|
2012-05-29 21:38:57 +04:00
|
|
|
// Added by cherryc
|
|
|
|
std::vector<std::size_t> feats() const;
|
|
|
|
friend bool operator==(SparseVector const& item1, SparseVector const& item2);
|
|
|
|
friend std::size_t hash_value(SparseVector const& item);
|
|
|
|
static std::size_t encode(const std::string& feat);
|
|
|
|
static std::string decode(std::size_t feat);
|
|
|
|
// End added by cherryc
|
|
|
|
|
2011-11-12 04:24:19 +04:00
|
|
|
private:
|
2012-03-10 12:12:34 +04:00
|
|
|
static name2id_t m_name_to_id;
|
|
|
|
static id2name_t m_id_to_name;
|
|
|
|
fvector_t m_fvector;
|
2011-10-14 11:40:53 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
SparseVector operator-(const SparseVector& lhs, const SparseVector& rhs);
|
2012-01-13 20:52:15 +04:00
|
|
|
FeatureStatsType inner_product(const SparseVector& lhs, const SparseVector& rhs);
|
2011-10-14 11:40:53 +04:00
|
|
|
|
2008-05-14 11:57:45 +04:00
|
|
|
class FeatureStats
|
|
|
|
{
|
|
|
|
private:
|
2012-05-10 02:51:05 +04:00
|
|
|
std::size_t m_available_size;
|
|
|
|
std::size_t m_entries;
|
2011-11-12 17:04:22 +04:00
|
|
|
|
|
|
|
// TODO: Use smart pointer for exceptional-safety.
|
2012-03-10 12:12:34 +04:00
|
|
|
featstats_t m_array;
|
|
|
|
SparseVector m_map;
|
2011-02-24 15:42:19 +03:00
|
|
|
|
2008-05-14 11:57:45 +04:00
|
|
|
public:
|
2011-02-24 15:42:19 +03:00
|
|
|
FeatureStats();
|
2012-05-10 02:51:05 +04:00
|
|
|
explicit FeatureStats(const std::size_t size);
|
2011-11-12 04:51:27 +04:00
|
|
|
|
2011-11-12 13:12:07 +04:00
|
|
|
~FeatureStats();
|
|
|
|
|
2011-11-12 04:51:27 +04:00
|
|
|
// We intentionally allow copying.
|
2011-02-24 15:42:19 +03:00
|
|
|
FeatureStats(const FeatureStats &stats);
|
|
|
|
FeatureStats& operator=(const FeatureStats &stats);
|
|
|
|
|
2011-11-12 13:12:07 +04:00
|
|
|
void Copy(const FeatureStats &stats);
|
2011-02-24 15:42:19 +03:00
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
bool isfull() const {
|
|
|
|
return (m_entries < m_available_size) ? 0 : 1;
|
|
|
|
}
|
2011-02-24 15:42:19 +03:00
|
|
|
void expand();
|
|
|
|
void add(FeatureStatsType v);
|
2012-05-10 02:51:05 +04:00
|
|
|
void addSparse(const std::string& name, FeatureStatsType v);
|
2011-02-24 15:42:19 +03:00
|
|
|
|
2011-11-12 17:04:22 +04:00
|
|
|
void clear() {
|
2012-03-10 12:12:34 +04:00
|
|
|
memset((void*)m_array, 0, GetArraySizeWithBytes());
|
|
|
|
m_map.clear();
|
2011-02-24 15:42:19 +03:00
|
|
|
}
|
|
|
|
|
2011-11-12 17:04:22 +04:00
|
|
|
void reset() {
|
2012-03-10 12:12:34 +04:00
|
|
|
m_entries = 0;
|
2011-11-12 17:04:22 +04:00
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
FeatureStatsType get(std::size_t i) {
|
|
|
|
return m_array[i];
|
|
|
|
}
|
|
|
|
FeatureStatsType get(std::size_t i)const {
|
|
|
|
return m_array[i];
|
|
|
|
}
|
|
|
|
featstats_t getArray() const {
|
|
|
|
return m_array;
|
|
|
|
}
|
2012-03-10 14:27:52 +04:00
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
const SparseVector& getSparse() const {
|
|
|
|
return m_map;
|
|
|
|
}
|
2011-02-24 15:42:19 +03:00
|
|
|
|
2012-01-13 20:52:15 +04:00
|
|
|
void set(std::string &theString, const SparseVector& sparseWeights);
|
2011-02-24 15:42:19 +03:00
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
inline std::size_t bytes() const {
|
|
|
|
return GetArraySizeWithBytes();
|
|
|
|
}
|
2011-11-12 12:30:33 +04:00
|
|
|
|
2012-05-10 02:51:05 +04:00
|
|
|
std::size_t GetArraySizeWithBytes() const {
|
2012-03-10 12:12:34 +04:00
|
|
|
return m_entries * sizeof(FeatureStatsType);
|
2011-02-24 15:42:19 +03:00
|
|
|
}
|
2011-11-12 12:30:33 +04:00
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
std::size_t size() const {
|
|
|
|
return m_entries;
|
|
|
|
}
|
2011-11-12 12:30:33 +04:00
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
std::size_t available() const {
|
|
|
|
return m_available_size;
|
|
|
|
}
|
2011-02-24 15:42:19 +03:00
|
|
|
|
|
|
|
void savetxt(const std::string &file);
|
2012-03-10 14:04:43 +04:00
|
|
|
void savetxt(std::ostream* os);
|
|
|
|
void savebin(std::ostream* os);
|
|
|
|
void savetxt();
|
2011-02-24 15:42:19 +03:00
|
|
|
|
2012-05-25 00:11:35 +04:00
|
|
|
void loadtxt(std::istream* is, const SparseVector& sparseWeights);
|
2012-03-10 14:04:43 +04:00
|
|
|
void loadbin(std::istream* is);
|
2011-02-24 15:42:19 +03:00
|
|
|
|
2011-11-12 03:58:23 +04:00
|
|
|
/**
|
|
|
|
* Write the whole object to a stream.
|
|
|
|
*/
|
2012-05-10 02:51:05 +04:00
|
|
|
friend std::ostream& operator<<(std::ostream& o, const FeatureStats& e);
|
2008-05-27 20:50:52 +04:00
|
|
|
};
|
2008-05-14 11:57:45 +04:00
|
|
|
|
2011-12-12 17:48:42 +04:00
|
|
|
bool operator==(const FeatureStats& f1, const FeatureStats& f2);
|
2012-06-30 23:23:45 +04:00
|
|
|
|
|
|
|
}
|
2011-12-12 17:48:42 +04:00
|
|
|
|
2012-02-20 04:46:08 +04:00
|
|
|
#endif // MERT_FEATURE_STATS_H_
|