mosesdecoder/mert/Util.cpp

97 lines
1.6 KiB
C++
Raw Normal View History

/*
* Util.cpp
2012-02-20 03:29:53 +04:00
* mert - Minimum Error Rate Training
*
* Created by Nicola Bertoldi on 13/05/08.
*
*/
#include "Util.h"
#include "Timer.h"
using namespace std;
2013-05-29 21:16:15 +04:00
namespace
{
2011-11-11 17:00:30 +04:00
MosesTuning::Timer g_timer;
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;
}
2011-11-11 17:00:30 +04:00
} // namespace
namespace MosesTuning
{
int verboselevel()
{
return g_verbose;
}
int setverboselevel(int v)
{
g_verbose = v;
return g_verbose;
}
2011-11-13 16:13:44 +04:00
size_t getNextPound(std::string &str, std::string &substr,
const std::string &delimiter)
{
2011-11-11 17:00:30 +04:00
size_t pos = 0;
// 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());
} else {
2011-11-13 16:13:44 +04:00
substr.assign(str);
str.assign("");
}
}
2011-11-11 17:00:30 +04:00
return pos;
}
2013-05-29 21:16:15 +04:00
void split(const std::string &s, char delim, std::vector<std::string> &elems)
{
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++;
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;
}
}
void ResetUserTime()
{
g_timer.start();
}
void PrintUserTime(const std::string &message)
{
g_timer.check(message.c_str());
}
double GetUserTime()
{
return g_timer.get_elapsed_cpu_time();
}
}