37 #ifndef RERANKER_FEATURE_VECTOR_H_
38 #define RERANKER_FEATURE_VECTOR_H_
43 #include <unordered_map>
44 #include <unordered_set>
56 using std::unordered_map;
57 using std::unordered_set;
61 static const T
Get() {
return T(); }
66 static const int Get() {
return -1; }
71 static const double Get() {
return -1.0; }
76 static const string Get() {
return ""; }
88 static const T &
Get(
const T &uid) {
return uid; }
97 static const string &
Get(
const string &uid) {
110 template <
typename K,
typename V,
113 typename Map = unordered_map<K, V> >
136 template <
typename MapType>
139 for (
typename MapType::const_iterator it = features.begin();
140 it != features.end(); ++it) {
141 features_.insert(*it);
164 return features_.begin();
170 return features_.end();
182 for (
const_iterator it = features_.begin(); it != features_.end(); ++it) {
183 set.insert(it->first);
197 for (
const_iterator it = features_.begin(); it != features_.end(); ++it) {
198 set.erase(it->first);
211 unordered_set<K> &set)
const {
212 for (
const_iterator it = features_.begin(); it != features_.end(); ++it) {
213 const K &uid = it->first;
214 if (set.find(uid) != set.end() &&
229 return feature_it == features_.end() ? V() : feature_it->second;
240 size_t size()
const {
return features_.size(); }
242 bool empty()
const {
return features_.empty(); }
248 bool this_is_smaller =
size() < other.
size();
249 const FeatureVector &small = this_is_smaller ? *
this : other;
250 const FeatureVector &large = this_is_smaller ? other : *
this;
253 it != small.features_.end();
255 dot_product += it->second * large.
GetWeight(it->first);
274 bool no_increase = by == default_val;
275 iterator feature_it = features_.find(canonical_uid);
276 if (feature_it == features_.end()) {
280 features_[canonical_uid] = by;
284 return feature_it->second;
286 V new_weight = feature_it->second + by;
287 if (new_weight == default_val) {
288 features_.erase(canonical_uid);
290 feature_it->second = new_weight;
314 if (new_weight == V()) {
315 features_.erase(uid);
317 features_[uid] = new_weight;
332 for (iterator it = features_.begin(); it != features_.end(); ++it) {
333 it->second = it->second * scalar;
372 template <
typename Collection>
377 for (
typename Collection::const_iterator it = feature_uids.begin();
378 it != feature_uids.end();
392 template <
typename Collection>
395 for (
typename Collection::const_iterator it = feature_uids.begin();
396 it != feature_uids.end();
413 friend ostream &operator<<(ostream &os, const FeatureVector<K,V> &fv) {
416 if (it != fv.features_.end()) {
417 os << it->first <<
"=" << it->second;
420 for ( ; it != fv.features_.end(); ++it) {
421 os <<
" " << it->first <<
"=" << it->second;
429 typedef typename FeatureMap::iterator iterator;
FeatureVector< K, V > & AddScaledVector(const FeatureVector< K, V > &other, V scalar)
Modifies this vector so that it equals this vector plus the scaled specified vector.
size_t size() const
Returns the number of non-zero feature components of this feature vector.
A simple class that provides a layer of abstraction when retrieving objects to represent unique ident...
unordered_set< K > & RemoveEqualFeatures(const FeatureVector< K, V > &other, unordered_set< K > &set) const
Removes from the specified set the uid's of feature with weights equal in this vector to their weight...
V GetWeight(const K &uid) const
Returns the weight of the feature with the specified uid, where crucially features not "present" in t...
FeatureVector< K, V > & Scale(V scalar)
Multiplies this vector, in situ, by the specified scalar.
FeatureVector()
Create an empty feature vector.
static const T & Get(const T &uid)
Gets the canonical uid object for the specified uid object.
const_iterator end() const
Returns a const iterator pointing to the end of the feature-value pairs of this feature vector...
const_iterator begin() const
Returns a const iterator pointing to the first of the feature-value pairs of this feature vector...
static const string & Get(const string &s)
Returns the canonical string for the specified string.
V IncrementValue(const K &uid, V by)
Increments the value of the specified vector component by the specified amount.
V SetValue(const K &uid, V new_value)
Synonym for SetWeight.
V mapped_type
The type of values/feature weights in this vector.
static const string & Get(const string &uid)
Gets the canonical uid string for the specified uid string.
FeatureVector< K, V > & AddScaledSubvector(const Collection &feature_uids, const FeatureVector< K, V > &feature_vector, V scalar)
Modifies this vector so that it equals this vector plus the scaled specified subvector.
FeatureMap::const_iterator const_iterator
The type of const iterator for the feature-weight pairs in this vector.
K key_type
The type of vector component/feature uid's in this vector.
static const string Get()
Map FeatureMap
The underlying type that this class stores the mapping of feature uid's to their weights.
V SetWeight(const K &uid, V new_weight)
Sets the weight of the specified feature to the specified value.
FeatureVector< K, V > & IncrementWeights(const Collection &feature_uids, V increment)
Increments the weights for the specified collection of features.
V IncrementWeight(const K &uid, V by)
Increments the weight of the specified feature by the specified amount.
V GetValue(const K &uid) const
Synonymous with GetWeight.
V Dot(const FeatureVector< K, V > &other) const
Computes the dot product of this feature vector with the specified FeatureVector. ...
unordered_set< K > & GetNonZeroFeatures(unordered_set< K > &set) const
Inserts the uid's of features with non-zero weights into the specified set.
Provides the reranker::StringCanonicalizer class.
static const double Get()
void clear()
Sets all feature weights to zero and, because this is a sparse vector, clears all storage...
unordered_set< K > & RemoveNonZeroFeatures(unordered_set< K > &set) const
Removes the uid's of features with non-zero weights from the specified set.
FeatureVector(const Map &features)
Copy features from the type of map used internally.
FeatureVector(const MapType &features)
Copy features from any map, or any collection of (feature,value) pairs, for that matter.
A class to represent a feature vector, where features are represented by unique identifiers, and feature values are represented by the template type.