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

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

#include "FeatureArray.h"
#include "FileStream.h"
#include "Util.h"


FeatureArray::FeatureArray()
    : idx(""), number_of_features(0) {}

FeatureArray::~FeatureArray() {}

void FeatureArray::savetxt(std::ofstream& outFile)
{
  outFile << FEATURES_TXT_BEGIN << " " << idx << " " << array_.size()
          << " " << number_of_features << " " << features << std::endl;
  for (featarray_t::iterator i = array_.begin(); i !=array_.end(); i++) {
    i->savetxt(outFile);
    outFile << std::endl;
  }
  outFile << FEATURES_TXT_END << std::endl;
}

void FeatureArray::savebin(std::ofstream& outFile)
{
  outFile << FEATURES_BIN_BEGIN << " " << idx << " " << array_.size()
          << " " << number_of_features << " " << features << std::endl;
  for (featarray_t::iterator i = array_.begin(); i !=array_.end(); i++)
    i->savebin(outFile);

  outFile << FEATURES_BIN_END << std::endl;
}


void FeatureArray::save(std::ofstream& inFile, bool bin)
{
  if (size()>0)
    (bin)?savebin(inFile):savetxt(inFile);
}

void FeatureArray::save(const std::string &file, bool bin)
{

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

  save(outFile);

  outFile.close();
}

void FeatureArray::loadbin(ifstream& inFile, size_t n)
{
  FeatureStats entry(number_of_features);

  for (size_t i=0 ; i < n; i++) {
    entry.loadbin(inFile);
    add(entry);
  }
}

void FeatureArray::loadtxt(ifstream& inFile, const SparseVector& sparseWeights, size_t n)
{
  FeatureStats entry(number_of_features);

  for (size_t i=0 ; i < n; i++) {
    entry.loadtxt(inFile, sparseWeights);
    add(entry);
  }
}

void FeatureArray::load(ifstream& inFile, const SparseVector& sparseWeights)
{
  size_t number_of_entries=0;
  bool binmode=false;

  std::string substring, stringBuf;
  std::string::size_type loc;

  std::getline(inFile, stringBuf);
  if (!inFile.good()) {
    return;
  }

  if (!stringBuf.empty()) {
    if ((loc = stringBuf.find(FEATURES_TXT_BEGIN)) == 0) {
      binmode=false;
    } else if ((loc = stringBuf.find(FEATURES_BIN_BEGIN)) == 0) {
      binmode=true;
    } else {
      TRACE_ERR("ERROR: FeatureArray::load(): Wrong header");
      return;
    }
    getNextPound(stringBuf, substring);
    getNextPound(stringBuf, substring);
    idx = substring;
    getNextPound(stringBuf, substring);
    number_of_entries = atoi(substring.c_str());
    getNextPound(stringBuf, substring);
    number_of_features = atoi(substring.c_str());
    features = stringBuf;
  }

  (binmode)?loadbin(inFile, number_of_entries):loadtxt(inFile, sparseWeights, number_of_entries);

  std::getline(inFile, stringBuf);
  if (!stringBuf.empty()) {
    if ((loc = stringBuf.find(FEATURES_TXT_END)) != 0 && (loc = stringBuf.find(FEATURES_BIN_END)) != 0) {
      TRACE_ERR("ERROR: FeatureArray::load(): Wrong footer");
      return;
    }
  }
}

void FeatureArray::merge(FeatureArray& e)
{
  //dummy implementation
  for (size_t i=0; i<e.size(); i++)
    add(e.get(i));
}

bool FeatureArray::check_consistency() const
{
  const size_t sz = NumberOfFeatures();
  if (sz == 0)
    return true;

  for (featarray_t::const_iterator i = array_.begin(); i != array_.end(); i++) {
    if (i->size() != sz)
      return false;
  }
  return true;
}