Cambridge SMT System
params.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use these files except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 // Copyright 2012 - Gonzalo Iglesias, AdriĆ  de Gispert, William Byrne
14 
15 #ifndef PARAMS_HPP_
16 #define PARAMS_HPP_
17 
27 // To avoid bloating, functions in this file do not use need to
28 // use logger or any boost library, etc.
29 #ifndef LINFO
30 #define LINFO(x) std::cerr << "INFO:: " << x << std::endl;
31 #endif
32 #ifndef LDEBUG
33 #define LDEBUG(x) std::cerr << "DEBUG:: " << x << std::endl;
34 #endif
35 
36 #ifndef LERROR
37 #define LERROR(x) std::cerr << "ERROR:: " << x << std::endl;
38 #endif
39 #ifndef LWARN
40 #define LWARN(x) std::cerr << "WARNING:: " << x << std::endl;
41 #endif
42 
43 namespace ucam {
44 namespace util {
45 
50 template<typename T>
51 inline std::vector<T> ParseParamString ( const std::string& stringparams ,
52  size_t pos = 0 ) {
53  std::vector<T> result;
54  std::stringstream strm ( std::stringstream::in | std::stringstream::out );
55  if ( pos ) strm << stringparams.substr ( pos ) << std::noskipws;
56  else strm << stringparams << std::noskipws;
57  char separator;
58  while ( strm.good() ) {
59  if ( result.size() > 0 ) {
60  strm >> separator;
61  }
62  T w;
63  strm >> w;
64  result.push_back ( w );
65  }
66  if ( strm.fail() || strm.bad() || strm.fail() ) {
67  LERROR("Unable to parse params : " << stringparams.substr ( pos ) );
68  for ( unsigned k = 0; k < result.size(); ++k )
69  std::cerr << result[k] << std::endl;
70  exit ( EXIT_FAILURE );
71  }
72  return result;
73 }
74 
76 template<typename T>
77 inline void ParseParamString ( const std::string& stringparams
78  , std::vector<T>& params
79  , size_t pos = 0
80  , size_t span = 0 ) {
81  using namespace std;
82  stringstream strm ( stringstream::in | stringstream::out );
83  if ( pos)
84  if (span > 0) {
85  strm << stringparams.substr ( pos , span) << noskipws;
86  LDEBUG("Passing in: [" << strm.str() << "]");
87  } else strm << stringparams.substr ( pos ) << noskipws;
88  else strm << stringparams << noskipws;
89  // not efficient.
90  span = strm.str().size();
91  char separator;
92  while ( strm.good() ) {
93  if ( params.size() > 0 ) {
94  strm >> separator;
95  }
96  T w;
97  strm >> w;
98  params.push_back ( w );
99  }
100  if ( strm.fail() || strm.bad() ) {
101  LERROR("Unable to parse params : " << stringparams.substr ( pos , span) );
102  for ( unsigned k = 0; k < params.size(); ++k )
103  cerr << params[k] << endl;
104  exit ( EXIT_FAILURE );
105  }
106 }
107 
111 inline void WriteParamFile(const std::string& filename, std::vector<float> params_) {
112  std::ofstream ofs ( filename.c_str() );
113  if ( !ofs.good() ) {
114  LERROR("Can't write to " << filename);
115  exit ( EXIT_FAILURE );
116  }
117  LINFO("Writing final Lambda to " << filename );
118  float ev = params_.back();
119  params_.pop_back();
120  for (size_t i=0; i<params_.size(); i++) {
121  ofs << params_[i] << ",";
122  }
123  ofs << ev << std::endl;
124  ofs.close();
125 }
126 
130 template<typename T>
131 struct ParamsInit {
132  std::vector<T> params;
133 
135  std::string stringparams;
136  char *paramsfile = getenv ( "TUPLEARC_WEIGHT_VECTOR_FILE" );
137  if ( paramsfile ) {
138  std::ifstream ifs ( paramsfile );
139  if ( !ifs.good() ) {
140  LERROR( "Unable to open file " << paramsfile );
141  exit ( EXIT_FAILURE);
142  }
143  getline ( ifs, stringparams );
144  } else {
145  char * pParams = getenv ( "TUPLEARC_WEIGHT_VECTOR" );
146  if ( !pParams ) {
147  LWARN("Cannot find parameter vector. Defaulting to flat parameters");
148  return;
149  }
150  stringparams = pParams;
151  }
152  params = ParseParamString<T> ( stringparams );
153 #ifdef PRINTDEBUG
154  std::stringstream ss;
155  for ( typename std::vector<T>::const_iterator it = params.begin();
156  it != params.end(); ++it ) {
157  ss << *it << ", ";
158  }
159  std::cerr << "Setting params to: " << ss.str() << endl;
160 #endif
161  }
162 
163 };
164 
165 }
166 } // end namespaces
167 #endif /* PARAMS_HPP_ */
#define LERROR(x)
Definition: params.hpp:37
std::vector< T > params
Definition: params.hpp:132
Initializes a set of parameters from environment variables PARAMS_FILE or PARAMS. ...
Definition: params.hpp:131
std::vector< T > ParseParamString(const std::string &stringparams, size_t pos=0)
Function to parse string of parameters, e.g. separated by commas.
Definition: params.hpp:51
void WriteParamFile(const std::string &filename, std::vector< float > params_)
Write parameter vector to a file, with comma separators.
Definition: params.hpp:111
#define LDEBUG(x)
Definition: params.hpp:33
iszfstream & getline(iszfstream &izs, std::string &line)
Definition: szfstream.hpp:178
#define LWARN(x)
Definition: params.hpp:40
#define LINFO(x)
Definition: params.hpp:30
Definition: bleu.hpp:14