Cambridge SMT System
global_funcs.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 
21 #ifndef GLOBAL_FUNCTIONS_HPP
22 #define GLOBAL_FUNCTIONS_HPP
23 
28 namespace ucam {
29 namespace util {
37 template <typename T>
38 inline std::string toString ( const T& x, uint pr = 2 ) {
39  std::stringstream aux;
40  aux << std::fixed << std::setprecision ( pr ) << x;
41  std::string ss;
42  aux >> ss;
43  USER_CHECK ( !aux.fail(), "Converting Number to string failed" );
44  return ss;
45 };
46 
55 template <typename T>
56 inline void toString ( const T& x, std::string& ss, uint pr = 2 ) {
57  std::stringstream aux;
58  if ( pr != std::numeric_limits<T>::max() ) aux << std::fixed <<
59  std::setprecision ( pr ) << x;
60  else aux << x ;
61  aux >> ss;
62  USER_CHECK ( !aux.fail(), "Converting Number to string failed" );
63 };
64 
71 template <typename T>
72 inline T toNumber ( const std::string& x ) {
73  std::stringstream aux;
74  aux << x;
75  T ss;
76  aux >> ss;
77  USER_CHECK ( !aux.fail() && aux.eof(), "Converting string to number failed" );
78  return ss;
79 };
80 
82 inline bool exists ( const std::string& source, const std::string& needle ) {
83  return ( source.find ( needle ) != std::string::npos );
84 };
85 
88 inline void find_and_replace ( std::string& haystack, const std::string& needle,
89  const std::string& replace ) {
90  std::size_t j = 0;
91  for ( ; ( j = haystack.find ( needle ) ) != std::string::npos ; ) {
92  haystack.replace ( j, needle.length(), replace );
93  }
94 };
95 
98 inline bool ends_with ( std::string const& haystack,
99  std::string const& needle ) {
100  if ( haystack.length() >= needle.length() )
101  return ( 0 == haystack.compare ( haystack.length() - needle.length(),
102  needle.length(), needle ) );
103  return false;
104 };
105 
107 inline uint count_needles ( const std::string& haystack, const char needle,
108  std::size_t start, std::size_t end ) {
109  uint count = 0;
110  for ( const char *c = haystack.c_str() + start; c < haystack.c_str() + end;
111  ++c )
112  if ( *c == needle ) ++count;
113  return count;
114 }
115 
117 inline void trim_spaces ( const std::string& input, std::string *output ) {
118  boost::regex pattern ( "\\s+",
119  boost::regex_constants::icase | boost::regex_constants::perl );
120  std::string replace ( " " );
121  *output = boost::regex_replace ( input, pattern, replace );
122  boost::algorithm::trim ( *output );
123 };
124 
127 
128 inline void trim_trailing_zeros ( std::string& snumber ) {
129  uint k;
130  for (k = snumber.size() - 1; k > 0 && snumber[k] == '0'; --k);
131  if (k == snumber.size() - 1 || !k) return;
132  uint j = k;
133  for (; j > 0 && snumber[j] != '.'; --j);
134  if (j != k) snumber = snumber.substr (0, k + 1);
135  else if (j > 0) snumber = snumber.substr (0, k);
136 }
137 
143 inline bool validate_source_sentence ( const std::string& s ) {
144  static const boost::regex e ( "^\\d+( \\d+)*$" );
145  return boost::regex_match ( s, e );
146 }
147 
149 inline std::string getTimestamp ( void ) {
150  time_t ltime;
151  time ( &ltime );
152  char time_buffer[128];
153  sprintf ( time_buffer, "%s", ctime ( &ltime ) );
154  time_buffer[strlen ( time_buffer ) - 1] = 0;
155  std::string times;
156  times.append ( time_buffer );
157  return times;
158 };
159 
162 inline bool DirName ( std::string& dirname, const std::string& filename ) {
163  std::string::size_type loc = filename.find_last_of ( "/", filename.size() );
164  if ( loc == std::string::npos ) return false;
165  dirname.append ( filename, 0, loc );
166  return true;
167 };
168 
171 inline bool fileExists ( const std::string& fileName ) {
172  std::fstream fin;
173  fin.open ( fileName.c_str(), std::ios::in );
174  if ( fin.is_open() ) {
175  fin.close();
176  return true;
177  }
178  fin.close();
179  return false;
180 };
181 
183 inline float dotproduct ( std::vector<float>& v1, std::vector<float>& v2 ) {
184  std::size_t lowertop = v1.size() > v2.size() ? v2.size() : v1.size();
185  float result = 0.0f;
186  for ( std::size_t k = 0; k < lowertop; ++k )
187  result += v1[k] * v2[k];
188  return result;
189 }
190 
193 template<class VecT>
194 class HashEqVec {
195  public:
197  bool operator() ( VecT const& v1,
198  VecT const& v2 ) const {
199  return v1 == v2;
200  }
201 };
202 
205 template<class VecT>
206 class HashFVec : public std::unary_function<VecT, std::size_t> {
207 public:
209  std::size_t operator() ( VecT const& v ) const {
210  std::size_t res = 0;
211  for ( unsigned int i = 0; i < v.size();
212  i++ ) res += ( std::size_t ) pow ( ( double ) v[i],
213  double ( ( ( i ) % 6 ) + 1 ) );
214  return res % HASH_MODULE;
215  }
216 };
217 
218 
221 
224 
225 template
226 < typename T
227  , template<typename ElemT, typename AllocT=std::allocator<ElemT> > class Container
228  >
229 inline std::ostream& operator<< (std::ostream& o, Container<T> const& container) {
230  typename Container<T>::const_iterator itx = container.begin();
231  while(itx != container.end()) {
232  o << *itx++ << " ";
233  }
234  return o;
235 };
236 
237 template
238 < typename T
239  , template<typename ElemT, typename AllocT=std::allocator<ElemT> > class Container
240  >
241 inline std::string printout(Container<T> const& container) {
242  std::ostringstream ss;
243  ss << container;
244  return ss.str();
245 };
246 
247 inline std::ostream& operator<< (std::ostream& o, std::basic_string<unsigned> const& container) {
248  std::basic_string<unsigned>::const_iterator itx = container.begin();
249  while(itx != container.end()) {
250  o << *itx++ << " ";
251  }
252  return o;
253 };
254 
255 }} // end namespaces
256 
257 #endif
bool operator()(VecT const &v1, VecT const &v2) const
Implements comparison.
std::string toString(const T &x, uint pr=2)
Converts an arbitrary type to string Converts to string integers, floats, doubles Quits execution if ...
void trim_spaces(const std::string &input, std::string *output)
Trims spaces at the edges (no spaces) and also between words (only one space)
HashFVec< std::vector< long long > > hashfvecint64
std::string getTimestamp(void)
Generates time stamp.
HashFVec< std::basic_string< unsigned > > hashfvecuint
void trim_trailing_zeros(std::string &snumber)
bool exists(const std::string &source, const std::string &needle)
Convenience function to find out whether a needle exists in a text.
bool DirName(std::string &dirname, const std::string &filename)
#define HASH_MODULE
HashEqVec< std::basic_string< unsigned > > hasheqvecuint
uint count_needles(const std::string &haystack, const char needle, std::size_t start, std::size_t end)
Convenience function that counts the number of times a needle appears.
float dotproduct(std::vector< float > &v1, std::vector< float > &v2)
Implements dot product.
bool fileExists(const std::string &fileName)
#define USER_CHECK(exp, comment)
Tests whether exp is true. If not, comment is printed and program ends.
T toNumber(const std::string &x)
Converts a string to an arbitrary number Converts strings to a number. Quits execution if conversion ...
void find_and_replace(std::string &haystack, const std::string &needle, const std::string &replace)
bool ends_with(std::string const &haystack, std::string const &needle)
Definition: bleu.hpp:14
std::string printout(Container< T > const &container)
HashEqVec< std::vector< long long > > hasheqvecint64
bool validate_source_sentence(const std::string &s)
Checks whether the sentence is in format ^\d+( \d+)*$.