00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "ScoreData.h"
00010
00011 #include <iostream>
00012 #include <fstream>
00013 #include "Scorer.h"
00014 #include "Util.h"
00015 #include "FileStream.h"
00016
00017 using namespace std;
00018
00019 namespace MosesTuning
00020 {
00021
00022
00023 ScoreData::ScoreData(Scorer* scorer) :
00024 m_scorer(scorer)
00025 {
00026 m_score_type = m_scorer->getName();
00027
00028 m_scorer->setScoreData(this);
00029 m_num_scores = m_scorer->NumberOfScores();
00030
00031 }
00032
00033 void ScoreData::save(ostream* os, bool bin)
00034 {
00035 for (scoredata_t::iterator i = m_array.begin();
00036 i != m_array.end(); ++i) {
00037 i->save(os, m_score_type, bin);
00038 }
00039 }
00040
00041 void ScoreData::save(const string &file, bool bin)
00042 {
00043 if (file.empty()) return;
00044 TRACE_ERR("saving the array into " << file << endl);
00045
00046
00047 ofstream ofs(file.c_str(), ios::out);
00048 ostream* os = &ofs;
00049 save(os, bin);
00050 ofs.close();
00051 }
00052
00053 void ScoreData::save(bool bin)
00054 {
00055 save(&cout, bin);
00056 }
00057
00058 void ScoreData::load(istream* is)
00059 {
00060 ScoreArray entry;
00061
00062 while (!is->eof()) {
00063 if (!is->good()) {
00064 cerr << "ERROR ScoreData::load inFile.good()" << endl;
00065 }
00066 entry.clear();
00067 entry.load(is);
00068 if (entry.size() == 0) {
00069 break;
00070 }
00071 add(entry);
00072 }
00073 }
00074
00075 void ScoreData::load(const string &file)
00076 {
00077 TRACE_ERR("loading score data from " << file << endl);
00078 inputfilestream input_stream(file);
00079 if (!input_stream) {
00080 throw runtime_error("Unable to open score file: " + file);
00081 }
00082 istream* is = &input_stream;
00083 load(is);
00084 input_stream.close();
00085 }
00086
00087 void ScoreData::add(ScoreArray& e)
00088 {
00089 if (exists(e.getIndex())) {
00090
00091 size_t pos = getIndex(e.getIndex());
00092 m_array.at(pos).merge(e);
00093 } else {
00094 m_array.push_back(e);
00095 setIndex();
00096 }
00097 }
00098
00099 void ScoreData::add(const ScoreStats& e, int sent_idx)
00100 {
00101 if (exists(sent_idx)) {
00102
00103 size_t pos = getIndex(sent_idx);
00104
00105 m_array.at(pos).add(e);
00106
00107 } else {
00108
00109 ScoreArray a;
00110 a.NumberOfScores(m_num_scores);
00111 a.add(e);
00112 a.setIndex(sent_idx);
00113 size_t idx = m_array.size();
00114 m_array.push_back(a);
00115 m_index_to_array_name[idx] = sent_idx;
00116 m_array_name_to_index[sent_idx]=idx;
00117
00118 }
00119 }
00120
00121 bool ScoreData::check_consistency() const
00122 {
00123 if (m_array.size() == 0)
00124 return true;
00125
00126 for (scoredata_t::const_iterator i = m_array.begin(); i != m_array.end(); ++i)
00127 if (!i->check_consistency()) return false;
00128
00129 return true;
00130 }
00131
00132 void ScoreData::setIndex()
00133 {
00134 size_t j = 0;
00135 for (scoredata_t::iterator i = m_array.begin(); i != m_array.end(); ++i) {
00136 m_index_to_array_name[j] = i->getIndex();
00137 m_array_name_to_index[i->getIndex()]=j;
00138 j++;
00139 }
00140 }
00141
00142 }