00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "ScoreArray.h"
00010 #include "Util.h"
00011 #include "FileStream.h"
00012
00013 using namespace std;
00014
00015 namespace MosesTuning
00016 {
00017
00018
00019 ScoreArray::ScoreArray()
00020 : m_num_scores(0), m_index(0) {}
00021
00022 void ScoreArray::savetxt(ostream* os, const string& sctype)
00023 {
00024 *os << SCORES_TXT_BEGIN << " " << m_index << " " << m_array.size()
00025 << " " << m_num_scores << " " << sctype << endl;
00026 for (scorearray_t::iterator i = m_array.begin(); i !=m_array.end(); i++) {
00027 i->savetxt(os);
00028 *os << endl;
00029 }
00030 *os << SCORES_TXT_END << endl;
00031 }
00032
00033 void ScoreArray::savebin(ostream* os, const string& score_type)
00034 {
00035 *os << SCORES_BIN_BEGIN << " " << m_index << " " << m_array.size()
00036 << " " << m_num_scores << " " << score_type << endl;
00037 for (scorearray_t::iterator i = m_array.begin();
00038 i != m_array.end(); i++) {
00039 i->savebin(os);
00040 }
00041 *os << SCORES_BIN_END << endl;
00042 }
00043
00044 void ScoreArray::save(ostream* os, const string& score_type, bool bin)
00045 {
00046 if (size() <= 0) return;
00047 if (bin) {
00048 savebin(os, score_type);
00049 } else {
00050 savetxt(os, score_type);
00051 }
00052 }
00053
00054 void ScoreArray::save(const string &file, const string& score_type, bool bin)
00055 {
00056 ofstream ofs(file.c_str(), ios::out);
00057 if (!ofs) {
00058 cerr << "Failed to open " << file << endl;
00059 exit(1);
00060 }
00061 ostream* os = &ofs;
00062 save(os, score_type, bin);
00063 ofs.close();
00064 }
00065
00066 void ScoreArray::save(const string& score_type, bool bin)
00067 {
00068 save(&cout, score_type, bin);
00069 }
00070
00071 void ScoreArray::loadbin(istream* is, size_t n)
00072 {
00073 ScoreStats entry(m_num_scores);
00074 for (size_t i = 0; i < n; i++) {
00075 entry.loadbin(is);
00076 add(entry);
00077 }
00078 }
00079
00080 void ScoreArray::loadtxt(istream* is, size_t n)
00081 {
00082 ScoreStats entry(m_num_scores);
00083 for (size_t i = 0; i < n; i++) {
00084 entry.loadtxt(is);
00085 add(entry);
00086 }
00087 }
00088
00089 void ScoreArray::load(istream* is)
00090 {
00091 size_t number_of_entries = 0;
00092 bool binmode = false;
00093
00094 string substring, stringBuf;
00095 string::size_type loc;
00096
00097 getline(*is, stringBuf);
00098 if (!is->good()) {
00099 return;
00100 }
00101
00102 if (!stringBuf.empty()) {
00103 if ((loc = stringBuf.find(SCORES_TXT_BEGIN)) == 0) {
00104 binmode=false;
00105 } else if ((loc = stringBuf.find(SCORES_BIN_BEGIN)) == 0) {
00106 binmode=true;
00107 } else {
00108 TRACE_ERR("ERROR: ScoreArray::load(): Wrong header");
00109 return;
00110 }
00111 getNextPound(stringBuf, substring);
00112 getNextPound(stringBuf, substring);
00113 m_index = atoi(substring.c_str());
00114 getNextPound(stringBuf, substring);
00115 number_of_entries = atoi(substring.c_str());
00116 getNextPound(stringBuf, substring);
00117 m_num_scores = atoi(substring.c_str());
00118 getNextPound(stringBuf, substring);
00119 m_score_type = substring;
00120 }
00121
00122 if (binmode) {
00123 loadbin(is, number_of_entries);
00124 } else {
00125 loadtxt(is, number_of_entries);
00126 }
00127
00128 getline(*is, stringBuf);
00129 if (!stringBuf.empty()) {
00130 if ((loc = stringBuf.find(SCORES_TXT_END)) != 0 &&
00131 (loc = stringBuf.find(SCORES_BIN_END)) != 0) {
00132 TRACE_ERR("ERROR: ScoreArray::load(): Wrong footer");
00133 return;
00134 }
00135 }
00136 }
00137
00138 void ScoreArray::load(const string &file)
00139 {
00140 TRACE_ERR("loading data from " << file << endl);
00141 inputfilestream input_stream(file);
00142 istream* is = &input_stream;
00143 load(is);
00144 input_stream.close();
00145 }
00146
00147
00148 void ScoreArray::merge(ScoreArray& e)
00149 {
00150
00151 for (size_t i=0; i<e.size(); i++)
00152 add(e.get(i));
00153 }
00154
00155 bool ScoreArray::check_consistency() const
00156 {
00157 const size_t sz = NumberOfScores();
00158 if (sz == 0)
00159 return true;
00160
00161 for (scorearray_t::const_iterator i = m_array.begin();
00162 i != m_array.end(); ++i) {
00163 if (i->size() != sz)
00164 return false;
00165 }
00166 return true;
00167 }
00168
00169 }