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: 04c6430cea96bf798ea69f63161d7850e524d750 (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
/*
 *  FeatureData.cpp
 *  met - Minimum Error Training
 *
 *  Created by Nicola Bertoldi on 13/05/08.
 *
 */

#include "FeatureData.h"

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

static const float MIN_FLOAT=-1.0*numeric_limits<float>::max();
static const float MAX_FLOAT=numeric_limits<float>::max();

FeatureData::FeatureData()
    : number_of_features(0)
      {}

void FeatureData::save(std::ofstream& outFile, bool bin)
{
  for (featdata_t::iterator i = array_.begin(); i !=array_.end(); i++)
    i->save(outFile, bin);
}

void FeatureData::save(const std::string &file, bool bin)
{
  if (file.empty()) return;

//  TRACE_ERR("saving the array into " << file << std::endl);

  std::ofstream outFile(file.c_str(), std::ios::out); // matches a stream with a file. Opens the file

  save(outFile, bin);

  outFile.close();
}

void FeatureData::load(ifstream& inFile, const SparseVector& sparseWeights)
{
  FeatureArray entry;

  while (!inFile.eof()) {

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

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

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

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

    add(entry);
  }
}


void FeatureData::load(const std::string &file, const SparseVector& sparseWeights)
{
  TRACE_ERR("loading feature data from " << file << std::endl);

  inputfilestream inFile(file); // matches a stream with a file. Opens the file

  if (!inFile) {
    throw runtime_error("Unable to open feature file: " + file);
  }

  load((ifstream&) inFile, sparseWeights);

  inFile.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());
    array_.at(pos).merge(e);
  } else {
    array_.push_back(e);
    setIndex();
  }
}

void FeatureData::add(FeatureStats& e, const std::string& 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);
    array_.at(pos).add(e);
  } else {
//              TRACE_ERR("Creating a new entry in the array and inserting " << e << std::endl);
    FeatureArray a;
    a.NumberOfFeatures(number_of_features);
    a.Features(features);
    a.setIndex(sent_idx);
    a.add(e);
    add(a);
  }
}

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

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

  return true;
}

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

void FeatureData::setFeatureMap(const std::string& feat)
{
  number_of_features = 0;
  features = feat;

  std::string substring, stringBuf;
  stringBuf = features;
  while (!stringBuf.empty()) {
    getNextPound(stringBuf, substring);

    featname2idx_[substring] = idx2featname_.size();
    idx2featname_[idx2featname_.size()] = substring;
    number_of_features++;
  }
}