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

FeatureData.h « mert - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 75c532896965dca7a5980197583a1bb294083c80 (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
/*
 *  FeatureData.h
 *  met - Minimum Error Training
 *
 *  Created by Nicola Bertoldi on 13/05/08.
 *
 */

#ifndef FEATURE_DATA_H
#define FEATURE_DATA_H

using namespace std;

#include <vector>
#include <iostream>
#include <stdexcept>
#include "FeatureArray.h"

class FeatureData
{
private:
  size_t number_of_features;
  std::string features;

  map<std::string, size_t> featname2idx_; // map from name to index of features
  map<size_t, std::string> idx2featname_; // map from index to name of features

protected:
  featdata_t array_;
  idx2name idx2arrayname_; // map from index to name of array
  name2idx arrayname2idx_; // map from name to index of array

public:
  FeatureData();
  ~FeatureData() {}

  inline void clear() {
    array_.clear();
  }

  inline FeatureArray get(const std::string& idx) {
    return array_.at(getIndex(idx));
  }
  inline FeatureArray& get(size_t idx) {
    return array_.at(idx);
  }
  inline const FeatureArray& get(size_t idx) const {
    return array_.at(idx);
  }

  inline bool exists(const std::string& sent_idx) {
    return exists(getIndex(sent_idx));
  }
  inline bool exists(int sent_idx) {
    return (sent_idx>-1 && sent_idx<(int) array_.size())?true:false;
  }

  inline FeatureStats& get(size_t i, size_t j) {
    return array_.at(i).get(j);
  }
  inline const FeatureStats&  get(size_t i, size_t j) const {
    return array_.at(i).get(j);
  }

  void add(FeatureArray& e);
  void add(FeatureStats& e, const std::string& sent_idx);

  inline size_t size() const {
    return array_.size();
  }
  inline size_t NumberOfFeatures() const {
    return number_of_features;
  }
  inline void NumberOfFeatures(size_t v) {
    number_of_features = v;
  }
  inline std::string Features() const {
    return features;
  }
  inline void Features(const std::string& f) {
    features = f;
  }

  void save(const std::string &file, bool bin=false);
  void save(ofstream& outFile, bool bin=false);
  inline void save(bool bin=false) {
    save("/dev/stdout", bin);
  }

  void load(ifstream& inFile, const SparseVector& sparseWeights);
  void load(const std::string &file, const SparseVector& sparseWeights);

  bool check_consistency() const;
  void setIndex();

  inline int getIndex(const std::string& idx) const {
    name2idx::const_iterator i = arrayname2idx_.find(idx);
    if (i != arrayname2idx_.end())
      return i->second;
    else
      return -1;
  }

  inline std::string getIndex(size_t idx) const {
    idx2name::const_iterator i = idx2arrayname_.find(idx);
    if (i != idx2arrayname_.end())
      throw runtime_error("there is no entry at index " + idx);
    return i->second;
  }

  bool existsFeatureNames() const {
    return (idx2featname_.size() > 0) ? true : false;
  }

  std::string getFeatureName(size_t idx) const {
    if (idx >= idx2featname_.size())
      throw runtime_error("Error: you required an too big index");
    map<size_t, std::string>::const_iterator it = idx2featname_.find(idx);
    if (it == idx2featname_.end()) {
      throw runtime_error("Error: specified id is unknown: " + idx);
    } else {
      return it->second;
    }
  }

  size_t getFeatureIndex(const std::string& name) const {
    map<std::string, size_t>::const_iterator it = featname2idx_.find(name);
    if (it == featname2idx_.end())
      throw runtime_error("Error: feature " + name + " is unknown");
    return it->second;
  }

  void setFeatureMap(const std::string& feat);
};

#endif  // FEATURE_DATA_H