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

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

#include "Util.h"
#include "ScoreStats.h"
#include <fstream>
#include <iostream>

using namespace std;

namespace {
const int kAvailableSize = 8;
} // namespace

namespace MosesTuning
{
  

ScoreStats::ScoreStats()
    : m_available_size(kAvailableSize), m_entries(0),
      m_array(new ScoreStatsType[m_available_size]) {}

ScoreStats::ScoreStats(const size_t size)
    : m_available_size(size), m_entries(size),
      m_array(new ScoreStatsType[m_available_size])
{
  memset(m_array, 0, GetArraySizeWithBytes());
}

ScoreStats::~ScoreStats()
{
  if (m_array) {
    delete [] m_array;
    m_array = NULL;
  }
}

void ScoreStats::Copy(const ScoreStats &stats)
{
  m_available_size = stats.available();
  m_entries = stats.size();
  m_array = new ScoreStatsType[m_available_size];
  memcpy(m_array, stats.getArray(), GetArraySizeWithBytes());
}

ScoreStats::ScoreStats(const ScoreStats &stats)
{
  Copy(stats);
}

ScoreStats& ScoreStats::operator=(const ScoreStats &stats)
{
  delete [] m_array;
  Copy(stats);
  return *this;
}

void ScoreStats::expand()
{
  m_available_size *= 2;
  scorestats_t buf = new ScoreStatsType[m_available_size];
  memcpy(buf, m_array, GetArraySizeWithBytes());
  delete [] m_array;
  m_array = buf;
}

void ScoreStats::add(ScoreStatsType v)
{
  if (isfull()) expand();
  m_array[m_entries++]=v;
}

void ScoreStats::set(const string& str)
{
  reset();
  vector<string> out;
  Tokenize(str.c_str(), ' ', &out);
  for (vector<string>::const_iterator it = out.begin();
       it != out.end(); ++it) {
    add(ConvertStringToScoreStatsType(*it));
  }
}

void ScoreStats::loadbin(istream* is)
{
  is->read(reinterpret_cast<char*>(m_array),
           static_cast<streamsize>(GetArraySizeWithBytes()));
}

void ScoreStats::loadtxt(istream* is)
{
  string line;
  getline(*is, line);
  set(line);
}

void ScoreStats::loadtxt(const string &file)
{
  ifstream ifs(file.c_str(), ios::in); // matches a stream with a file. Opens the file
  if (!ifs) {
    cerr << "Failed to open " << file << endl;
    exit(1);
  }
  istream* is = &ifs;
  loadtxt(is);
}


void ScoreStats::savetxt(const string &file)
{
  ofstream ofs(file.c_str(), ios::out); // matches a stream with a file. Opens the file
  ostream* os = &ofs;
  savetxt(os);
}

void ScoreStats::savetxt(ostream* os)
{
  *os << *this;
}

void ScoreStats::savetxt() {
  savetxt(&cout);
}

void ScoreStats::savebin(ostream* os)
{
  os->write(reinterpret_cast<char*>(m_array),
            static_cast<streamsize>(GetArraySizeWithBytes()));
}

ostream& operator<<(ostream& o, const ScoreStats& e)
{
  for (size_t i=0; i< e.size(); i++)
    o << e.get(i) << " ";
  return o;
}

bool operator==(const ScoreStats& s1, const ScoreStats& s2) {
  size_t size = s1.size();

  if (size != s2.size())
    return false;

  for (size_t k=0; k < size; k++) {
    if (s1.get(k) != s2.get(k))
      return false;
  }

  return true;
}

}