File size: 8,456 Bytes
158b61b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
#pragma once
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <fstream>
#include <iostream>
#include <string>
#include <limits>
#include <sstream>
#include <vector>
#include <queue>
#include <cmath>
#include <stdlib.h>
#include "../TypeDef.h"
#include "util/exception.hh"
namespace Moses2
{
#ifdef TRACE_ERR
#undef TRACE_ERR
#endif
#ifdef TRACE_ENABLE
#define TRACE_ERR(str) do { std::cerr << str; } while (false)
#else
#define TRACE_ERR(str) do {} while (false)
#endif
////////////////////////////////////////////////////
template<typename T>
class UnorderedComparer
{
public:
size_t operator()(const T* obj) const {
return obj->hash();
}
bool operator()(const T* a, const T* b) const {
return a->hash() == b->hash();
}
};
////////////////////////////////////////////////////
template<typename T>
void Init(T arr[], size_t size, const T &val)
{
for (size_t i = 0; i < size; ++i) {
arr[i] = val;
}
}
//! delete white spaces at beginning and end of string
inline std::string Trim(const std::string& str, const std::string dropChars =
" \t\n\r")
{
std::string res = str;
res.erase(str.find_last_not_of(dropChars) + 1);
return res.erase(0, res.find_first_not_of(dropChars));
}
//! convert string to variable of type T. Used to reading floats, int etc from files
template<typename T>
inline T Scan(const std::string &input)
{
std::stringstream stream(input);
T ret;
stream >> ret;
return ret;
}
//! just return input
template<>
inline std::string Scan<std::string>(const std::string &input)
{
return input;
}
template<>
inline SCORE Scan<SCORE>(const std::string &input)
{
SCORE ret = atof(input.c_str());
return ret;
}
//! Specialisation to understand yes/no y/n true/false 0/1
template<>
bool Scan<bool>(const std::string &input);
template<>
inline S2TParsingAlgorithm Scan<S2TParsingAlgorithm>(const std::string &input)
{
return (S2TParsingAlgorithm) Scan<size_t>(input);
}
template<>
inline SourceLabelOverlap Scan<SourceLabelOverlap>(const std::string &input)
{
return (SourceLabelOverlap) Scan<size_t>(input);
}
template<>
inline SearchAlgorithm Scan<SearchAlgorithm>(const std::string &input)
{
return (SearchAlgorithm) Scan<size_t>(input);
}
template<>
inline XmlInputType Scan<XmlInputType>(const std::string &input)
{
XmlInputType ret;
if (input=="exclusive") ret = XmlExclusive;
else if (input=="inclusive") ret = XmlInclusive;
else if (input=="constraint") ret = XmlConstraint;
else if (input=="ignore") ret = XmlIgnore;
else if (input=="pass-through") ret = XmlPassThrough;
else {
UTIL_THROW2("Unknown XML input type");
}
return ret;
}
template<>
inline InputTypeEnum Scan<InputTypeEnum>(const std::string &input)
{
return (InputTypeEnum) Scan<size_t>(input);
}
template<>
inline WordAlignmentSort Scan<WordAlignmentSort>(const std::string &input)
{
return (WordAlignmentSort) Scan<size_t>(input);
}
//! convert vectors of string to vectors of type T variables
template<typename T>
inline std::vector<T> Scan(const std::vector<std::string> &input)
{
std::vector<T> output(input.size());
for (size_t i = 0; i < input.size(); i++) {
output[i] = Scan<T>(input[i]);
}
return output;
}
//! speeded up version of above
template<typename T>
inline void Scan(std::vector<T> &output, const std::vector<std::string> &input)
{
output.resize(input.size());
for (size_t i = 0; i < input.size(); i++) {
output[i] = Scan<T>(input[i]);
}
}
/** tokenise input string to vector of string. each element has been separated by a character in the delimiters argument.
The separator can only be 1 character long. The default delimiters are space or tab
*/
inline std::vector<std::string> Tokenize(const std::string& str,
const std::string& delimiters = " \t")
{
std::vector<std::string> tokens;
// Skip delimiters at beginning.
std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
// Find first "non-delimiter".
std::string::size_type pos = str.find_first_of(delimiters, lastPos);
while (std::string::npos != pos || std::string::npos != lastPos) {
// Found a token, add it to the vector.
tokens.push_back(str.substr(lastPos, pos - lastPos));
// Skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of(delimiters, pos);
// Find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos);
}
return tokens;
}
//! tokenise input string to vector of type T
template<typename T>
inline std::vector<T> Tokenize(const std::string &input,
const std::string& delimiters = " \t")
{
std::vector<std::string> stringVector = Tokenize(input, delimiters);
return Scan<T>(stringVector);
}
/** only split of the first delimiter. Used by class FeatureFunction for parse key=value pair.
* Value may have = character
*/
inline std::vector<std::string> TokenizeFirstOnly(const std::string& str,
const std::string& delimiters = " \t")
{
std::vector<std::string> tokens;
std::string::size_type pos = str.find_first_of(delimiters);
if (std::string::npos != pos) {
// Found a token, add it to the vector.
tokens.push_back(str.substr(0, pos));
tokens.push_back(str.substr(pos + 1, str.size() - pos - 1));
} else {
tokens.push_back(str);
}
return tokens;
}
inline std::vector<std::string> TokenizeMultiCharSeparator(
const std::string& str, const std::string& separator)
{
std::vector<std::string> tokens;
size_t pos = 0;
// Find first "non-delimiter".
std::string::size_type nextPos = str.find(separator, pos);
while (nextPos != std::string::npos) {
// Found a token, add it to the vector.
tokens.push_back(str.substr(pos, nextPos - pos));
// Skip delimiters. Note the "not_of"
pos = nextPos + separator.size();
// Find next "non-delimiter"
nextPos = str.find(separator, pos);
}
tokens.push_back(str.substr(pos, nextPos - pos));
return tokens;
}
// speeded up version of above
inline void TokenizeMultiCharSeparator(std::vector<std::string> &output,
const std::string& str, const std::string& separator)
{
size_t pos = 0;
// Find first "non-delimiter".
std::string::size_type nextPos = str.find(separator, pos);
while (nextPos != std::string::npos) {
// Found a token, add it to the vector.
output.push_back(Trim(str.substr(pos, nextPos - pos)));
// Skip delimiters. Note the "not_of"
pos = nextPos + separator.size();
// Find next "non-delimiter"
nextPos = str.find(separator, pos);
}
output.push_back(Trim(str.substr(pos, nextPos - pos)));
}
//! get string representation of any object/variable, as long as it can pipe to a stream
template<typename T>
inline std::string SPrint(const T &input)
{
std::stringstream stream("");
stream << input;
return stream.str();
}
//! irst number are in log 10, transform to natural log
inline float TransformLMScore(float irstScore)
{
return irstScore * 2.30258509299405f;
}
//! transform prob to natural log score
inline float TransformScore(float prob)
{
return log(prob);
}
//! make sure score doesn't fall below LOWEST_SCORE
inline float FloorScore(float logScore)
{
return (std::max)(logScore, LOWEST_SCORE);
}
inline float UntransformLMScore(float logNScore)
{
// opposite of above
return logNScore / 2.30258509299405f;
}
inline bool FileExists(const std::string& filePath)
{
std::ifstream ifs(filePath.c_str());
return !ifs.fail();
}
const std::string ToLower(const std::string& str);
//! delete and remove every element of a collection object such as set, list etc
template<class COLL>
void RemoveAllInColl(COLL &coll)
{
for (typename COLL::const_iterator iter = coll.begin(); iter != coll.end();
++iter) {
delete (*iter);
}
coll.clear();
}
template<typename T>
void Swap(T &a, T &b)
{
T &c = a;
a = b;
b = c;
}
// grab the underlying contain of priority queue
template<class T, class S, class C>
S& Container(std::priority_queue<T, S, C>& q)
{
struct HackedQueue: private std::priority_queue<T, S, C> {
static S& Container(std::priority_queue<T, S, C>& q) {
return q.*&HackedQueue::c;
}
};
return HackedQueue::Container(q);
}
#define HERE __FILE__ << ":" << __LINE__
/** Enforce rounding */
inline void FixPrecision(std::ostream& stream, size_t size = 3)
{
stream.setf(std::ios::fixed);
stream.precision(size);
}
}
|