2008-05-14 11:57:45 +04:00
|
|
|
/*
|
|
|
|
* Util.cpp
|
2012-02-20 03:29:53 +04:00
|
|
|
* mert - Minimum Error Rate Training
|
2011-02-24 15:42:19 +03:00
|
|
|
*
|
2008-05-14 11:57:45 +04:00
|
|
|
* Created by Nicola Bertoldi on 13/05/08.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Util.h"
|
2009-01-07 16:30:06 +03:00
|
|
|
#include "Timer.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
namespace
|
|
|
|
{
|
2011-11-11 17:00:30 +04:00
|
|
|
|
2012-06-30 23:23:45 +04:00
|
|
|
MosesTuning::Timer g_timer;
|
2012-02-20 03:02:23 +04:00
|
|
|
int g_verbose = 0;
|
|
|
|
|
2011-11-11 17:00:30 +04:00
|
|
|
bool FindDelimiter(const std::string &str, const std::string &delim, size_t *pos)
|
|
|
|
{
|
|
|
|
*pos = str.find(delim);
|
|
|
|
return *pos != std::string::npos ? true : false;
|
|
|
|
}
|
2012-02-20 03:02:23 +04:00
|
|
|
|
2011-11-11 17:00:30 +04:00
|
|
|
} // namespace
|
|
|
|
|
2012-06-30 23:23:45 +04:00
|
|
|
namespace MosesTuning
|
|
|
|
{
|
|
|
|
|
2011-02-24 15:42:19 +03:00
|
|
|
int verboselevel()
|
|
|
|
{
|
2012-02-20 03:02:23 +04:00
|
|
|
return g_verbose;
|
2008-05-16 11:09:15 +04:00
|
|
|
}
|
|
|
|
|
2011-02-24 15:42:19 +03:00
|
|
|
int setverboselevel(int v)
|
|
|
|
{
|
2012-02-20 03:02:23 +04:00
|
|
|
g_verbose = v;
|
|
|
|
return g_verbose;
|
2008-05-16 11:09:15 +04:00
|
|
|
}
|
|
|
|
|
2011-11-13 16:13:44 +04:00
|
|
|
size_t getNextPound(std::string &str, std::string &substr,
|
|
|
|
const std::string &delimiter)
|
2008-05-14 11:57:45 +04:00
|
|
|
{
|
2011-11-11 17:00:30 +04:00
|
|
|
size_t pos = 0;
|
2011-02-24 15:42:19 +03:00
|
|
|
|
2011-11-12 03:58:23 +04:00
|
|
|
// skip all occurrences of delimiter
|
2011-11-11 17:00:30 +04:00
|
|
|
while (pos == 0) {
|
2011-11-13 16:13:44 +04:00
|
|
|
if (FindDelimiter(str, delimiter, &pos)) {
|
|
|
|
substr.assign(str, 0, pos);
|
|
|
|
str.erase(0, pos + delimiter.size());
|
2011-02-24 15:42:19 +03:00
|
|
|
} else {
|
2011-11-13 16:13:44 +04:00
|
|
|
substr.assign(str);
|
|
|
|
str.assign("");
|
2011-02-24 15:42:19 +03:00
|
|
|
}
|
|
|
|
}
|
2011-11-11 17:00:30 +04:00
|
|
|
return pos;
|
2011-11-12 04:40:01 +04:00
|
|
|
}
|
2008-05-20 18:15:30 +04:00
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
void split(const std::string &s, char delim, std::vector<std::string> &elems)
|
|
|
|
{
|
2011-08-20 19:25:19 +04:00
|
|
|
std::stringstream ss(s);
|
|
|
|
std::string item;
|
|
|
|
while(std::getline(ss, item, delim)) {
|
|
|
|
elems.push_back(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-11 17:00:30 +04:00
|
|
|
void Tokenize(const char *str, const char delim,
|
2013-05-29 21:16:15 +04:00
|
|
|
std::vector<std::string> *res)
|
|
|
|
{
|
2011-11-11 17:00:30 +04:00
|
|
|
while (1) {
|
|
|
|
const char *begin = str;
|
|
|
|
while (*str != delim && *str) str++;
|
2012-02-20 02:39:24 +04:00
|
|
|
if (begin != str) // Don't create empty string objects.
|
|
|
|
res->push_back(std::string(begin, str));
|
2011-11-11 17:00:30 +04:00
|
|
|
if (*str++ == 0) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-07 16:30:06 +03:00
|
|
|
void ResetUserTime()
|
|
|
|
{
|
|
|
|
g_timer.start();
|
2011-11-12 04:40:01 +04:00
|
|
|
}
|
2009-01-07 16:30:06 +03:00
|
|
|
|
|
|
|
void PrintUserTime(const std::string &message)
|
2011-02-24 15:42:19 +03:00
|
|
|
{
|
|
|
|
g_timer.check(message.c_str());
|
2009-01-07 16:30:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
double GetUserTime()
|
|
|
|
{
|
2012-02-27 03:34:51 +04:00
|
|
|
return g_timer.get_elapsed_cpu_time();
|
2009-01-07 16:30:06 +03:00
|
|
|
}
|
2012-06-30 23:23:45 +04:00
|
|
|
|
|
|
|
}
|