Cambridge SMT System
params.h
Go to the documentation of this file.
1 //Copyright (c) 2012, University of Cambridge
2 //All rights reserved.
3 //
4 //Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met://
5 //
6 // * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7 // * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8 // * Neither the name of the University of Cambridge nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9 //
10 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
11 
12 #ifndef PARAMS_H_
13 #define PARAMS_H_
14 
15 #include <limits.h>
16 
17 namespace fst {
18 
19 template<typename T, typename Collection>
20 struct AE {
21  static void AddElement (Collection&, unsigned int, T);
22 };
23 
24 template<typename T>
25 struct AE<T, std::vector<T> > {
26  static void AddElement (std::vector<T>& params, unsigned int index, T param) {
27  if (params.size() == index) {
28  params.push_back (param);
29  } else if (params.size() < index) {
30  params.resize (index + 1, 0.0);
31  params[index] = param;
32  }
33  }
34 };
35 
36 template<typename T>
37 struct AE<T, std::map<unsigned int, T> > {
38  static void AddElement (std::map<unsigned int, T>& params , unsigned int index
39  , T param) {
40  params[index] = param;
41  }
42 };
43 
44 template<typename T, typename Collection >
45 Collection ParseParamString (const std::string& stringparams) {
46  Collection result;
47  std::stringstream strm (std::stringstream::in | std::stringstream::out);
48  strm << stringparams << noskipws;
49  char separator;
50  unsigned int index = 0;
51  while (strm.good() ) {
52  T w;
53  if (strm.peek() == '@') {
54  w = 1.0;
55  strm >> separator;
56  } else {
57  strm >> w;
58  if (strm.eof() ) {
59  separator = ' ';
60  } else {
61  strm >> separator;
62  }
63  }
64  if (separator == '@') {
65  strm >> index;
66  if (!strm.eof() ) {
67  strm >> separator;
68  }
69  }
70  AE<T, Collection>::AddElement (result, index, w);
71  ++index;
72  }
73  if (strm.fail() || strm.bad() ) {
74  cerr << "ERROR: Unable to parse params: " << stringparams << endl;
75  exit (1);
76  }
77  return result;
78 }
79 
80 template<typename T>
81 struct ParamsInit {
82  std::vector<T> params;
83 
85  std::string stringparams;
86  char *paramsfile = getenv ("PARAMS_FILE");
87  if (paramsfile) {
88  ifstream ifs (paramsfile);
89  if (!ifs.good() ) {
90  cerr << "ERROR: unable to open file " << paramsfile << '\n';
91  exit (1);
92  }
93  getline (ifs, stringparams);
94  } else {
95  char * pParams = getenv ("PARAMS");
96  if (!pParams) {
97  if (FLAGS_v > 0) {
98  cerr
99  << "Warning: cannot find parameter vector. Defaulting to flat parameters\n";
100  }
101  return;
102  }
103  stringparams = pParams;
104  }
105  params = ParseParamString<T, std::vector<T> > (stringparams);
106  if (FLAGS_v > 0) {
107  cerr << "Setting params to: ";
108  for (typename std::vector<T>::const_iterator it = params.begin();
109  it != params.end(); ++it) {
110  cerr << *it << ", ";
111  }
112  cerr << endl;
113  }
114  }
115 
116 };
117 
118 }
119 
120 #endif /* PARAMS_H_ */
static void AddElement(std::vector< T > &params, unsigned int index, T param)
Definition: params.h:26
Definition: fstio.hpp:27
Definition: params.h:20
Collection ParseParamString(const std::string &stringparams)
Definition: params.h:45
std::vector< T > params
Definition: params.h:82
static void AddElement(Collection &, unsigned int, T)
iszfstream & getline(iszfstream &izs, std::string &line)
Definition: szfstream.hpp:178
static void AddElement(std::map< unsigned int, T > &params, unsigned int index, T param)
Definition: params.h:38