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 const char kDefaultDelimiterSymbol[] = " ";
00035
00036 int verboselevel();
00037 int setverboselevel(int v);
00038
00039
00040 const float kEPS = 0.0001f;
00041
00042 template <typename T>
00043 bool IsAlmostEqual(T expected, T actual, float round=kEPS) {
00044 if (std::abs(expected - actual) < round) {
00045 return true;
00046 } else {
00047 std::cerr << "Fail: expected = " << expected
00048 << " (actual = " << actual << ")" << std::endl;
00049 return false;
00050 }
00051 }
00052
00063 size_t getNextPound(std::string &str, std::string &substr,
00064 const std::string &delimiter = kDefaultDelimiterSymbol);
00065
00066 void split(const std::string &s, char delim, std::vector<std::string> &elems);
00067
00074 void Tokenize(const char *str, const char delim, std::vector<std::string> *res);
00075
00076 template<typename T>
00077 inline T Scan(const std::string &input)
00078 {
00079 std::stringstream stream(input);
00080 T ret;
00081 stream >> ret;
00082 return ret;
00083 }
00084
00089 inline bool EndsWith(const std::string& str, const char* suffix) {
00090 return str.find_last_of(suffix) == str.size() - 1;
00091 }
00092
00093 template<typename T>
00094 inline std::string stringify(T x)
00095 {
00096 std::ostringstream o;
00097 if (!(o << x))
00098 throw std::runtime_error("stringify(template<typename T>)");
00099 return o.str();
00100 }
00101
00102 inline ScoreStatsType ConvertCharToScoreStatsType(const char *str)
00103 {
00104 return std::atoi(str);
00105 }
00106
00107 inline ScoreStatsType ConvertStringToScoreStatsType(const std::string& str)
00108 {
00109 return ConvertCharToScoreStatsType(str.c_str());
00110 }
00111
00112 inline FeatureStatsType ConvertCharToFeatureStatsType(const char *str)
00113 {
00114 return static_cast<FeatureStatsType>(std::atof(str));
00115 }
00116
00117 inline FeatureStatsType ConvertStringToFeatureStatsType(const std::string &str)
00118 {
00119 return ConvertCharToFeatureStatsType(str.c_str());
00120 }
00121
00122 inline std::string trimStr(const std::string& Src, const std::string& c = " \r\n")
00123 {
00124 size_t p2 = Src.find_last_not_of(c);
00125 if (p2 == std::string::npos) return std::string();
00126 size_t p1 = Src.find_first_not_of(c);
00127 if (p1 == std::string::npos) p1 = 0;
00128 return Src.substr(p1, (p2-p1)+1);
00129 }
00130
00131
00132 void ResetUserTime();
00133 void PrintUserTime(const std::string &message);
00134 double GetUserTime();
00135
00136 }
00137
00138 #endif // MERT_UTIL_H_