00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <iostream>
00020 #include <sstream>
00021 #include <boost/functional/hash.hpp>
00022
00023 #include "util/file_piece.hh"
00024 #include "util/tokenize_piece.hh"
00025
00026 #include "FeatureArray.h"
00027 #include "FeatureDataIterator.h"
00028
00029
00030 using namespace std;
00031 using namespace util;
00032
00033 namespace MosesTuning
00034 {
00035
00036
00037 int ParseInt(const StringPiece& str )
00038 {
00039 char* errIndex;
00040
00041 int value = static_cast<int>(strtol(str.data(), &errIndex,10));
00042 if (errIndex == str.data()) {
00043 throw util::ParseNumberException(str);
00044 }
00045 return value;
00046 }
00047
00048 float ParseFloat(const StringPiece& str)
00049 {
00050 char* errIndex;
00051 float value = static_cast<float>(strtod(str.data(), &errIndex));
00052 if (errIndex == str.data()) {
00053 throw util::ParseNumberException(str);
00054 }
00055 return value;
00056 }
00057
00058 bool operator==(FeatureDataItem const& item1, FeatureDataItem const& item2)
00059 {
00060 return item1.dense==item1.dense && item1.sparse==item1.sparse;
00061 }
00062
00063 size_t hash_value(FeatureDataItem const& item)
00064 {
00065 size_t seed = 0;
00066 boost::hash_combine(seed,item.dense);
00067 boost::hash_combine(seed,item.sparse);
00068 return seed;
00069 }
00070
00071
00072 FeatureDataIterator::FeatureDataIterator() {}
00073
00074 FeatureDataIterator::FeatureDataIterator(const string& filename)
00075 {
00076 m_in.reset(new FilePiece(filename.c_str()));
00077 readNext();
00078 }
00079
00080 FeatureDataIterator::~FeatureDataIterator() {}
00081
00082 void FeatureDataIterator::readNext()
00083 {
00084 m_next.clear();
00085 try {
00086 StringPiece marker = m_in->ReadDelimited();
00087 if (marker != StringPiece(FEATURES_TXT_BEGIN)) {
00088 throw FileFormatException(m_in->FileName(), marker.as_string());
00089 }
00090
00091 m_in->ReadULong();
00092 size_t count = m_in->ReadULong();
00093 size_t length = m_in->ReadULong();
00094 m_in->ReadLine();
00095 for (size_t i = 0; i < count; ++i) {
00096 StringPiece line = m_in->ReadLine();
00097 m_next.push_back(FeatureDataItem());
00098 for (TokenIter<AnyCharacter, true> token(line, AnyCharacter(" \t")); token; ++token) {
00099 TokenIter<AnyCharacterLast,false> value(*token,AnyCharacterLast("="));
00100 if (!value) throw FileFormatException(m_in->FileName(), line.as_string());
00101 StringPiece first = *value;
00102 ++value;
00103 if (!value) {
00104
00105 float floatValue = ParseFloat(first);
00106 m_next.back().dense.push_back(floatValue);
00107 } else {
00108
00109 StringPiece second = *value;
00110 float floatValue = ParseFloat(second);
00111 m_next.back().sparse.set(first.as_string(),floatValue);
00112 }
00113 }
00114 if (length != m_next.back().dense.size()) {
00115 throw FileFormatException(m_in->FileName(), line.as_string());
00116 }
00117 }
00118 StringPiece line = m_in->ReadLine();
00119 if (line != StringPiece(FEATURES_TXT_END)) {
00120 throw FileFormatException(m_in->FileName(), line.as_string());
00121 }
00122 } catch (EndOfFileException &e) {
00123 m_in.reset();
00124 }
00125 }
00126
00127 void FeatureDataIterator::increment()
00128 {
00129 readNext();
00130 }
00131
00132 bool FeatureDataIterator::equal(const FeatureDataIterator& rhs) const
00133 {
00134 if (!m_in && !rhs.m_in) {
00135 return true;
00136 } else if (!m_in) {
00137 return false;
00138 } else if (!rhs.m_in) {
00139 return false;
00140 } else {
00141 return m_in->FileName() == rhs.m_in->FileName() &&
00142 m_in->Offset() == rhs.m_in->Offset();
00143 }
00144 }
00145
00146 const vector<FeatureDataItem>& FeatureDataIterator::dereference() const
00147 {
00148 return m_next;
00149 }
00150
00151 }
00152