Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FeatureStats.h « mert - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 39bffa93e734fe104c127032d8b56ab55e1cdbdd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 *  FeatureStats.h
 *  met - Minimum Error Training
 *
 *  Created by Nicola Bertoldi on 13/05/08.
 *
 */

#ifndef FEATURE_STATS_H
#define FEATURE_STATS_H

#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include "Types.h"

using namespace std;

// Minimal sparse vector
class SparseVector {
public:
  typedef std::map<size_t,FeatureStatsType> fvector_t;
  typedef std::map<std::string, size_t> name2id_t;
  typedef std::vector<std::string> id2name_t;

  FeatureStatsType get(const std::string& name) const;
  FeatureStatsType get(size_t id) const;
  void set(const std::string& name, FeatureStatsType value);
  void clear();
  void load(const std::string& file);
  size_t size() const {
    return fvector_.size();
  }

  void write(std::ostream& out, const std::string& sep = " ") const;

  SparseVector& operator-=(const SparseVector& rhs);
  FeatureStatsType inner_product(const SparseVector& rhs) const;

private:
  static name2id_t name2id_;
  static id2name_t id2name_;
  fvector_t fvector_;
};

SparseVector operator-(const SparseVector& lhs, const SparseVector& rhs);
FeatureStatsType inner_product(const SparseVector& lhs, const SparseVector& rhs);

class FeatureStats
{
private:
  size_t available_;
  size_t entries_;

  // TODO: Use smart pointer for exceptional-safety.
  featstats_t array_;
  SparseVector map_;

public:
  FeatureStats();
  explicit FeatureStats(const size_t size);

  ~FeatureStats();

  // We intentionally allow copying.
  FeatureStats(const FeatureStats &stats);
  FeatureStats& operator=(const FeatureStats &stats);

  void Copy(const FeatureStats &stats);

  bool isfull() const {
    return (entries_ < available_) ? 0 : 1;
  }
  void expand();
  void add(FeatureStatsType v);
  void addSparse(const string& name, FeatureStatsType v);

  void clear() {
    memset((void*)array_, 0, GetArraySizeWithBytes());
    map_.clear();
  }

  void reset() {
    entries_ = 0;
    clear();
  }

  inline FeatureStatsType get(size_t i) {
    return array_[i];
  }
  inline FeatureStatsType get(size_t i)const {
    return array_[i];
  }
  inline featstats_t getArray() const {
    return array_;
  }
  inline const SparseVector& getSparse() const {
    return map_;
  }

  void set(std::string &theString, const SparseVector& sparseWeights);

  inline size_t bytes() const {
    return GetArraySizeWithBytes();
  }

  size_t GetArraySizeWithBytes() const {
    return entries_ * sizeof(FeatureStatsType);
  }

  inline size_t size() const {
    return entries_;
  }

  inline size_t available() const {
    return available_;
  }

  void savetxt(const std::string &file);
  void savetxt(ofstream& outFile);
  void savebin(ofstream& outFile);
  inline void savetxt() {
    savetxt("/dev/stdout");
  }

  void loadtxt(ifstream& inFile, const SparseVector& sparseWeights);
  void loadbin(ifstream& inFile);

  /**
   * Write the whole object to a stream.
   */
  friend ostream& operator<<(ostream& o, const FeatureStats& e);
};

#endif  // FEATURE_STATS_H