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

FeatureData.cpp « mert - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 13b9b3a96dfd59fbf903ca53761449bb81318750 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 *  FeatureData.cpp
 *  mert - Minimum Error Rate Training
 *
 *  Created by Nicola Bertoldi on 13/05/08.
 *
 */

#include "FeatureData.h"

#include <limits>
#include "FileStream.h"
#include "Util.h"

using namespace std;

namespace MosesTuning
{



FeatureData::FeatureData()
  : m_num_features(0) {}

void FeatureData::save(ostream* os, bool bin)
{
  for (featdata_t::iterator i = m_array.begin(); i != m_array.end(); i++)
    i->save(os, bin);
}

void FeatureData::save(const string &file, bool bin)
{
  if (file.empty()) return;
  TRACE_ERR("saving the array into " << file << endl);
  ofstream ofs(file.c_str(), ios::out); // matches a stream with a file. Opens the file
  ostream* os = &ofs;
  save(os, bin);
  ofs.close();
}

void FeatureData::save(bool bin)
{
  save(&cout, bin);
}

void FeatureData::load(istream* is, const SparseVector& sparseWeights)
{
  FeatureArray entry;

  while (!is->eof()) {

    if (!is->good()) {
      cerr << "ERROR FeatureData::load inFile.good()" << endl;
    }

    entry.clear();
    entry.load(is, sparseWeights);

    if (entry.size() == 0)
      break;

    if (size() == 0)
      setFeatureMap(entry.Features());

    add(entry);
  }
}


void FeatureData::load(const string &file, const SparseVector& sparseWeights)
{
  TRACE_ERR("loading feature data from " << file << endl);
  inputfilestream input_stream(file); // matches a stream with a file. Opens the file
  if (!input_stream) {
    throw runtime_error("Unable to open feature file: " + file);
  }
  istream* is = &input_stream;
  load(is, sparseWeights);
  input_stream.close();
}

void FeatureData::add(FeatureArray& e)
{
  if (exists(e.getIndex())) { // array at position e.getIndex() already exists
    //enlarge array at position e.getIndex()
    size_t pos = getIndex(e.getIndex());
    m_array.at(pos).merge(e);
  } else {
    m_array.push_back(e);
    setIndex();
  }
}

void FeatureData::add(FeatureStats& e, int sent_idx)
{
  if (exists(sent_idx)) { // array at position e.getIndex() already exists
    //enlarge array at position e.getIndex()
    size_t pos = getIndex(sent_idx);
//              TRACE_ERR("Inserting " << e << " in array " << sent_idx << std::endl);
    m_array.at(pos).add(e);
  } else {
//              TRACE_ERR("Creating a new entry in the array and inserting " << e << std::endl);
    FeatureArray a;
    a.NumberOfFeatures(m_num_features);
    a.Features(m_features);
    a.setIndex(sent_idx);
    a.add(e);
    add(a);
  }
}

bool FeatureData::check_consistency() const
{
  if (m_array.size() == 0)
    return true;

  for (featdata_t::const_iterator i = m_array.begin(); i != m_array.end(); i++)
    if (!i->check_consistency()) return false;

  return true;
}

void FeatureData::setIndex()
{
  size_t j=0;
  for (featdata_t::iterator i = m_array.begin(); i !=m_array.end(); i++) {
    m_index_to_array_name[j]=(*i).getIndex();
    m_array_name_to_index[(*i).getIndex()] = j;
    j++;
  }
}

void FeatureData::setFeatureMap(const string& feat)
{
  m_num_features = 0;
  m_features = feat;

  vector<string> buf;
  Tokenize(feat.c_str(), ' ', &buf);
  for (vector<string>::const_iterator it = buf.begin();
       it != buf.end(); ++it) {
    const size_t size = m_index_to_feature_name.size();
    m_feature_name_to_index[*it] = size;
    m_index_to_feature_name[size] = *it;
    ++m_num_features;
  }
}

string FeatureData::ToString() const
{
  string res;

  {
    stringstream ss;
    ss << "number of features: " << m_num_features
       << ", features: " << m_features;
    res.append(ss.str());
  }

  res.append("feature_id_map = { ");
  for (map<string, size_t>::const_iterator it = m_feature_name_to_index.begin();
       it != m_feature_name_to_index.end(); ++it) {
    stringstream ss;
    ss << it->first << " => " << it->second << ", ";
    res.append(ss.str());
  }
  res.append("}");

  return res;
}

}