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