00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef MERT_UTIL_H_
00010 #define MERT_UTIL_H_
00011
00012 #include <cmath>
00013 #include <cstdlib>
00014 #include <stdexcept>
00015 #include <limits>
00016 #include <vector>
00017 #include <map>
00018 #include <iostream>
00019 #include <sstream>
00020 #include <string>
00021 #include <cstring>
00022
00023 #include "Types.h"
00024
00025 namespace MosesTuning
00026 {
00027
00028 #ifdef TRACE_ENABLE
00029 #define TRACE_ERR(str) { std::cerr << str; }
00030 #else
00031 #define TRACE_ERR(str) { }
00032 #endif
00033
00034 #if __GNUC__ == 4 && __GNUC_MINOR__ == 8 && (__GNUC_PATCHLEVEL__ == 1 || __GNUC_PATCHLEVEL__ == 2)
00035
00036 #define NTH_ELEMENT3(begin, middle, end) std::sort(begin, end)
00037 #define NTH_ELEMENT4(begin, middle, end, orderer) std::sort(begin, end, orderer)
00038 #else
00039 #define NTH_ELEMENT3(begin, middle, end) std::nth_element(begin, middle, end)
00040 #define NTH_ELEMENT4(begin, middle, end, orderer) std::nth_element(begin, middle, end, orderer)
00041 #endif
00042
00043 const char kDefaultDelimiterSymbol[] = " ";
00044
00045 int verboselevel();
00046 int setverboselevel(int v);
00047
00048
00049 const float kEPS = 0.0001f;
00050
00051 template <typename T>
00052 bool IsAlmostEqual(T expected, T actual, float round=kEPS)
00053 {
00054 if (std::abs(expected - actual) < round) {
00055 return true;
00056 } else {
00057 std::cerr << "Fail: expected = " << expected
00058 << " (actual = " << actual << ")" << std::endl;
00059 return false;
00060 }
00061 }
00062
00073 size_t getNextPound(std::string &str, std::string &substr,
00074 const std::string &delimiter = kDefaultDelimiterSymbol);
00075
00076 void split(const std::string &s, char delim, std::vector<std::string> &elems);
00077
00084 void Tokenize(const char *str, const char delim, std::vector<std::string> *res);
00085
00086 template<typename T>
00087 inline T Scan(const std::string &input)
00088 {
00089 std::stringstream stream(input);
00090 T ret;
00091 stream >> ret;
00092 return ret;
00093 }
00094
00099 inline bool EndsWith(const std::string& str, const char* suffix)
00100 {
00101 return str.find_last_of(suffix) == str.size() - 1;
00102 }
00103
00104 template<typename T>
00105 inline std::string stringify(T x)
00106 {
00107 std::ostringstream o;
00108 if (!(o << x))
00109 throw std::runtime_error("stringify(template<typename T>)");
00110 return o.str();
00111 }
00112
00113 inline ScoreStatsType ConvertCharToScoreStatsType(const char *str)
00114 {
00115 return std::atoi(str);
00116 }
00117
00118 inline ScoreStatsType ConvertStringToScoreStatsType(const std::string& str)
00119 {
00120 return ConvertCharToScoreStatsType(str.c_str());
00121 }
00122
00123 inline FeatureStatsType ConvertCharToFeatureStatsType(const char *str)
00124 {
00125 return static_cast<FeatureStatsType>(std::atof(str));
00126 }
00127
00128 inline FeatureStatsType ConvertStringToFeatureStatsType(const std::string &str)
00129 {
00130 return ConvertCharToFeatureStatsType(str.c_str());
00131 }
00132
00133 inline std::string trimStr(const std::string& Src, const std::string& c = " \r\n")
00134 {
00135 size_t p2 = Src.find_last_not_of(c);
00136 if (p2 == std::string::npos) return std::string();
00137 size_t p1 = Src.find_first_not_of(c);
00138 if (p1 == std::string::npos) p1 = 0;
00139 return Src.substr(p1, (p2-p1)+1);
00140 }
00141
00142
00143 void ResetUserTime();
00144 void PrintUserTime(const std::string &message);
00145 double GetUserTime();
00146
00147 }
00148
00149 #endif // MERT_UTIL_H_