2008-06-11 14:52:57 +04:00
|
|
|
// $Id$
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
Moses - factored phrase-based language decoder
|
|
|
|
Copyright (C) 2006 University of Edinburgh
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
***********************************************************************/
|
|
|
|
|
2015-04-30 21:44:29 +03:00
|
|
|
#pragma once
|
2008-06-11 14:52:57 +04:00
|
|
|
|
2008-09-12 22:09:06 +04:00
|
|
|
#include <iostream>
|
2008-06-11 14:52:57 +04:00
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <cmath>
|
2013-11-19 22:52:15 +04:00
|
|
|
#include <cassert>
|
2008-06-11 14:52:57 +04:00
|
|
|
#include <limits>
|
2008-10-09 03:51:26 +04:00
|
|
|
#include <map>
|
2008-10-17 01:14:38 +04:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2013-11-19 22:52:15 +04:00
|
|
|
#include "util/exception.hh"
|
2012-01-08 11:36:54 +04:00
|
|
|
#include "TypeDef.h"
|
2008-06-11 14:52:57 +04:00
|
|
|
|
2008-10-09 03:51:26 +04:00
|
|
|
namespace Moses
|
|
|
|
{
|
|
|
|
|
2008-06-11 14:52:57 +04:00
|
|
|
/** Outputting debugging/verbose information to stderr.
|
|
|
|
* Use TRACE_ENABLE flag to redirect tracing output into oblivion
|
|
|
|
* so that you can output your own ad-hoc debugging info.
|
2012-06-29 02:29:46 +04:00
|
|
|
* However, if you use stderr directly, please delete calls to it once
|
2008-06-11 14:52:57 +04:00
|
|
|
* you finished debugging so that it won't clutter up.
|
|
|
|
* Also use TRACE_ENABLE to turn off output of any debugging info
|
|
|
|
* when compiling for a gui front-end so that running gui won't generate
|
|
|
|
* output on command line
|
|
|
|
* */
|
2015-02-10 02:04:17 +03:00
|
|
|
|
|
|
|
// TRACE_ERR might have been defined by IRSTLM
|
|
|
|
#ifdef TRACE_ERR
|
|
|
|
#undef TRACE_ERR
|
|
|
|
#endif
|
2008-06-11 14:52:57 +04:00
|
|
|
#ifdef TRACE_ENABLE
|
2010-02-24 13:37:49 +03:00
|
|
|
#define TRACE_ERR(str) do { std::cerr << str; } while (false)
|
2008-06-11 14:52:57 +04:00
|
|
|
#else
|
2010-02-24 13:37:49 +03:00
|
|
|
#define TRACE_ERR(str) do {} while (false)
|
2008-06-11 14:52:57 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/** verbose macros
|
|
|
|
* */
|
2014-07-09 05:41:28 +04:00
|
|
|
|
2015-02-10 02:04:17 +03:00
|
|
|
// VERBOSE might have been defined by IRSTLM
|
|
|
|
#ifdef VERBOSE
|
|
|
|
#undef VERBOSE
|
|
|
|
#endif
|
2014-12-04 22:13:42 +03:00
|
|
|
#define VERBOSE(level,str) { IFVERBOSE(level) { TRACE_ERR(str); } }
|
2015-02-10 02:04:17 +03:00
|
|
|
|
|
|
|
// VERBOSE might have been defined by IRSTLM
|
|
|
|
#ifdef IFVERBOSE
|
|
|
|
#undef IFVERBOSE
|
|
|
|
#endif
|
2008-06-11 14:52:57 +04:00
|
|
|
#define IFVERBOSE(level) if (StaticData::Instance().GetVerboseLevel() >= level)
|
2014-12-04 22:13:42 +03:00
|
|
|
#define XVERBOSE(level,str) VERBOSE(level, "[" << HERE << "] " << str)
|
2014-07-09 05:41:28 +04:00
|
|
|
#define HERE __FILE__ << ":" << __LINE__
|
2014-12-04 22:13:42 +03:00
|
|
|
#define FEATUREVERBOSE(level,str) FEATUREVERBOSE2(level, "[" << GetScoreProducerDescription() << "] " << str)
|
|
|
|
#define FEATUREVERBOSE2(level,str) { IFFEATUREVERBOSE(level) { TRACE_ERR(str); } }
|
|
|
|
#define IFFEATUREVERBOSE(level) if ((m_verbosity == std::numeric_limits<std::size_t>::max() && StaticData::Instance().GetVerboseLevel() >= level) || (m_verbosity != std::numeric_limits<std::size_t>::max() && m_verbosity >= level))
|
2014-07-09 05:41:28 +04:00
|
|
|
|
2008-06-11 14:52:57 +04:00
|
|
|
|
2013-11-15 14:55:38 +04:00
|
|
|
#if __GNUC__ == 4 && __GNUC_MINOR__ == 8 && (__GNUC_PATCHLEVEL__ == 1 || __GNUC_PATCHLEVEL__ == 2)
|
|
|
|
// gcc nth_element() bug
|
|
|
|
#define NTH_ELEMENT3(begin, middle, end) std::sort(begin, end)
|
|
|
|
#define NTH_ELEMENT4(begin, middle, end, orderer) std::sort(begin, end, orderer)
|
|
|
|
#else
|
|
|
|
#define NTH_ELEMENT3(begin, middle, end) std::nth_element(begin, middle, end)
|
|
|
|
#define NTH_ELEMENT4(begin, middle, end, orderer) std::nth_element(begin, middle, end, orderer)
|
|
|
|
#endif
|
|
|
|
|
2015-04-30 21:44:29 +03:00
|
|
|
|
2010-04-08 21:16:10 +04:00
|
|
|
const std::string ToLower(const std::string& str);
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2015-04-30 21:44:29 +03:00
|
|
|
//! 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));
|
|
|
|
}
|
|
|
|
|
2008-06-11 14:52:57 +04:00
|
|
|
//! 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)
|
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
std::stringstream stream("");
|
|
|
|
stream << input;
|
|
|
|
return stream.str();
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//! 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)
|
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
std::stringstream stream(input);
|
|
|
|
T ret;
|
|
|
|
stream >> ret;
|
|
|
|
return ret;
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
2008-06-21 00:24:16 +04:00
|
|
|
//! just return input
|
|
|
|
template<>
|
|
|
|
inline std::string Scan<std::string>(const std::string &input)
|
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
return input;
|
2008-06-21 00:24:16 +04:00
|
|
|
}
|
|
|
|
|
2014-11-27 20:49:08 +03:00
|
|
|
template<>
|
|
|
|
inline WordAlignmentSort Scan<WordAlignmentSort>(const std::string &input)
|
|
|
|
{
|
|
|
|
return (WordAlignmentSort) Scan<size_t>(input);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
inline InputTypeEnum Scan<InputTypeEnum>(const std::string &input)
|
|
|
|
{
|
|
|
|
return (InputTypeEnum) Scan<size_t>(input);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
inline SearchAlgorithm Scan<SearchAlgorithm>(const std::string &input)
|
|
|
|
{
|
|
|
|
return (SearchAlgorithm) Scan<size_t>(input);
|
|
|
|
}
|
|
|
|
|
2014-11-28 20:15:01 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-11-28 02:02:41 +03:00
|
|
|
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 {
|
2015-01-14 14:07:42 +03:00
|
|
|
UTIL_THROW2("Unknown XML input type");
|
2014-11-28 02:02:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-06-11 14:52:57 +04:00
|
|
|
//! Specialisation to understand yes/no y/n true/false 0/1
|
|
|
|
template<>
|
|
|
|
bool Scan<bool>(const std::string &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)
|
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
std::vector<T> output(input.size());
|
|
|
|
for (size_t i = 0 ; i < input.size() ; i++) {
|
|
|
|
output[i] = Scan<T>( input[i] );
|
|
|
|
}
|
|
|
|
return output;
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
2010-04-08 21:16:10 +04:00
|
|
|
//! speeded up version of above
|
|
|
|
template<typename T>
|
|
|
|
inline void Scan(std::vector<T> &output, const std::vector< std::string > &input)
|
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
output.resize(input.size());
|
|
|
|
for (size_t i = 0 ; i < input.size() ; i++) {
|
|
|
|
output[i] = Scan<T>( input[i] );
|
|
|
|
}
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2008-09-12 22:09:06 +04:00
|
|
|
/** replace all occurrences of todelStr in str with the string toaddStr */
|
|
|
|
inline std::string Replace(const std::string& str,
|
2011-02-24 16:14:42 +03:00
|
|
|
const std::string& todelStr,
|
|
|
|
const std::string& toaddStr)
|
2008-09-12 22:09:06 +04:00
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
size_t pos=0;
|
|
|
|
std::string newStr=str;
|
|
|
|
while ((pos=newStr.find(todelStr,pos))!=std::string::npos) {
|
|
|
|
newStr.replace(pos++,todelStr.size(),toaddStr);
|
|
|
|
}
|
|
|
|
return newStr;
|
2008-09-12 22:09:06 +04:00
|
|
|
}
|
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
/** tokenise input string to vector of string. each element has been separated by a character in the delimiters argument.
|
2008-06-11 14:52:57 +04:00
|
|
|
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,
|
2011-02-24 16:14:42 +03:00
|
|
|
const std::string& delimiters = " \t")
|
2008-06-11 14:52:57 +04:00
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
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;
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
2010-04-08 21:16:10 +04:00
|
|
|
// speeded up version of above
|
|
|
|
inline void Tokenize(std::vector<std::string> &output
|
2011-02-24 16:14:42 +03:00
|
|
|
, const std::string& str
|
|
|
|
, const std::string& delimiters = " \t")
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
// 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.
|
|
|
|
output.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);
|
|
|
|
}
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2008-06-11 14:52:57 +04:00
|
|
|
//! tokenise input string to vector of type T
|
|
|
|
template<typename T>
|
|
|
|
inline std::vector<T> Tokenize( const std::string &input
|
2011-02-24 16:14:42 +03:00
|
|
|
, const std::string& delimiters = " \t")
|
2008-06-11 14:52:57 +04:00
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
std::vector<std::string> stringVector = Tokenize(input, delimiters);
|
|
|
|
return Scan<T>( stringVector );
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
2010-04-08 21:16:10 +04:00
|
|
|
// speeded up version of above
|
|
|
|
template<typename T>
|
|
|
|
inline void Tokenize( std::vector<T> &output
|
2011-02-24 16:14:42 +03:00
|
|
|
, const std::string &input
|
|
|
|
, const std::string& delimiters = " \t")
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
std::vector<std::string> stringVector;
|
|
|
|
Tokenize(stringVector, input, delimiters);
|
|
|
|
return Scan<T>(output, stringVector );
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2008-06-11 14:52:57 +04:00
|
|
|
inline std::vector<std::string> TokenizeMultiCharSeparator(
|
2011-02-24 16:14:42 +03:00
|
|
|
const std::string& str,
|
|
|
|
const std::string& separator)
|
2008-06-11 14:52:57 +04:00
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
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;
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
2010-04-08 21:16:10 +04:00
|
|
|
// speeded up version of above
|
|
|
|
inline void TokenizeMultiCharSeparator(std::vector<std::string> &output
|
2011-02-24 16:14:42 +03:00
|
|
|
,const std::string& str
|
|
|
|
,const std::string& separator)
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
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)));
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
2013-07-18 17:01:08 +04:00
|
|
|
/** 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));
|
2013-07-18 20:00:44 +04:00
|
|
|
} else {
|
|
|
|
tokens.push_back(str);
|
2013-07-18 17:01:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return tokens;
|
|
|
|
}
|
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2008-06-11 14:52:57 +04:00
|
|
|
/**
|
|
|
|
* Convert vector of type T to string
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
std::string Join(const std::string& delimiter, const std::vector<T>& items)
|
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
std::ostringstream outstr;
|
|
|
|
if(items.size() == 0) return "";
|
|
|
|
outstr << items[0];
|
|
|
|
for(unsigned int i = 1; i < items.size(); i++)
|
|
|
|
outstr << delimiter << items[i];
|
|
|
|
return outstr.str();
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
2015-01-06 17:19:34 +03:00
|
|
|
/*
|
|
|
|
* Convert any container to string
|
|
|
|
*/
|
|
|
|
template<typename It>
|
|
|
|
std::string Join(const std::string &delim, It begin, It end)
|
|
|
|
{
|
|
|
|
std::ostringstream outstr;
|
|
|
|
if (begin != end)
|
|
|
|
outstr << *begin++;
|
|
|
|
for ( ; begin != end; ++begin)
|
|
|
|
outstr << delim << *begin;
|
|
|
|
return outstr.str();
|
|
|
|
}
|
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
//! transform prob to natural log score
|
2008-06-11 14:52:57 +04:00
|
|
|
inline float TransformScore(float prob)
|
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
return log(prob);
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
//! transform natural log score to prob. Not currently used
|
2008-06-11 14:52:57 +04:00
|
|
|
inline float UntransformScore(float score)
|
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
return exp(score);
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//! irst number are in log 10, transform to natural log
|
2010-04-08 21:16:10 +04:00
|
|
|
inline float TransformLMScore(float irstScore)
|
2011-02-24 16:14:42 +03:00
|
|
|
{
|
|
|
|
return irstScore * 2.30258509299405f;
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
2010-04-08 21:16:10 +04:00
|
|
|
inline float UntransformLMScore(float logNScore)
|
2011-02-24 16:14:42 +03:00
|
|
|
{
|
|
|
|
// opposite of above
|
|
|
|
return logNScore / 2.30258509299405f;
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//! make sure score doesn't fall below LOWEST_SCORE
|
|
|
|
inline float FloorScore(float logScore)
|
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
return (std::max)(logScore , LOWEST_SCORE);
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** convert prob vector to log prob and calc inner product with weight vector.
|
|
|
|
* At least, that's what I think it does, fn is only 9 lines but can't figure out what it does.
|
|
|
|
* Not sure whether give zens a medal for being a genius, or shoot him for writing unreadable code. Mabe both...
|
|
|
|
*/
|
2011-02-24 16:14:42 +03:00
|
|
|
inline float CalcTranslationScore(const std::vector<float> &probVector,
|
|
|
|
const std::vector<float> &weightT)
|
2008-06-11 14:52:57 +04:00
|
|
|
{
|
2013-11-23 00:27:46 +04:00
|
|
|
UTIL_THROW_IF2(weightT.size() != probVector.size(),
|
2014-01-15 19:42:02 +04:00
|
|
|
"Weight and score vector sizes not the same");
|
2011-02-24 16:14:42 +03:00
|
|
|
float rv=0.0;
|
|
|
|
for(float const *sb=&probVector[0],*se=sb+probVector.size(),*wb=&weightT[0];
|
|
|
|
sb!=se; ++sb, ++wb)
|
|
|
|
rv += TransformScore(*sb) * (*wb);
|
|
|
|
return rv;
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
/** declaration of ToString() function to go in header for each class.
|
|
|
|
* This function, as well as the operator<< fn for each class, is
|
|
|
|
* for debugging purposes only. The output format is likely to change from
|
|
|
|
* time-to-time as classes are updated so shouldn't be relied upon
|
2008-06-11 14:52:57 +04:00
|
|
|
* for any decoding algorithm
|
|
|
|
*/
|
|
|
|
#define TO_STRING() std::string ToString() const;
|
|
|
|
|
|
|
|
//! definition of ToString() function to go in .cpp file. Can be used for any class that can be piped to a stream
|
|
|
|
#define TO_STRING_BODY(CLASS) \
|
|
|
|
std::string CLASS::ToString() const \
|
|
|
|
{ \
|
|
|
|
std::stringstream out; \
|
|
|
|
out << *this; \
|
|
|
|
return out.str(); \
|
|
|
|
} \
|
2015-05-02 13:45:24 +03:00
|
|
|
|
2013-04-22 15:21:59 +04:00
|
|
|
//! delete and remove every element of a collection object such as set, list etc
|
2008-06-11 14:52:57 +04:00
|
|
|
template<class COLL>
|
|
|
|
void RemoveAllInColl(COLL &coll)
|
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
for (typename COLL::const_iterator iter = coll.begin() ; iter != coll.end() ; ++iter) {
|
|
|
|
delete (*iter);
|
|
|
|
}
|
|
|
|
coll.clear();
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
2013-04-22 15:21:59 +04:00
|
|
|
//! delete and remove every element of map
|
|
|
|
template<class COLL>
|
|
|
|
void RemoveAllInMap(COLL &coll)
|
|
|
|
{
|
|
|
|
for (typename COLL::const_iterator iter = coll.begin() ; iter != coll.end() ; ++iter) {
|
|
|
|
delete (iter->second);
|
|
|
|
}
|
|
|
|
coll.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-11 14:52:57 +04:00
|
|
|
//! x-platform reference to temp folder
|
|
|
|
std::string GetTempFolder();
|
|
|
|
//! MD5 hash of a file
|
|
|
|
std::string GetMD5Hash(const std::string &filePath);
|
|
|
|
|
|
|
|
//! save memory by getting rid of spare, unused elements in a collection
|
2011-02-24 16:14:42 +03:00
|
|
|
template<typename T>
|
|
|
|
inline void ShrinkToFit(T& v)
|
2008-06-11 14:52:57 +04:00
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
if(v.capacity()>v.size())
|
|
|
|
T(v).swap(v);
|
2013-11-19 22:52:15 +04:00
|
|
|
assert(v.capacity()==v.size());
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FileExists(const std::string& filePath);
|
|
|
|
|
|
|
|
// A couple of utilities to measure decoding time
|
|
|
|
void ResetUserTime();
|
|
|
|
void PrintUserTime(const std::string &message);
|
|
|
|
double GetUserTime();
|
|
|
|
|
2013-08-14 14:20:51 +04:00
|
|
|
// dump SGML parser for <dlt> tags
|
|
|
|
std::vector< std::map<std::string, std::string> > ProcessAndStripDLT(std::string &line);
|
|
|
|
|
2008-06-11 14:52:57 +04:00
|
|
|
// dump SGML parser for <seg> tags
|
|
|
|
std::map<std::string, std::string> ProcessAndStripSGML(std::string &line);
|
|
|
|
|
2013-04-12 22:43:53 +04:00
|
|
|
std::string PassthroughSGML(std::string &line, const std::string tagName,const std::string& lbrackStr="<", const std::string& rbrackStr=">");
|
|
|
|
|
2010-09-14 18:05:00 +04:00
|
|
|
/**
|
|
|
|
* Returns the first string bounded by the delimiters (default delimiters are " " and "\t")i starting from position first_pos
|
|
|
|
* and and stores the starting position of the next string (in first_str)
|
|
|
|
*/
|
|
|
|
inline std::string GetFirstString(const std::string& str, int& first_pos, const std::string& delimiters = " \t")
|
|
|
|
{
|
2013-05-29 21:16:15 +04:00
|
|
|
|
|
|
|
std::string first_str;
|
|
|
|
// Skip delimiters at beginning.
|
|
|
|
std::string::size_type lastPos = str.find_first_not_of(delimiters, first_pos);
|
|
|
|
|
|
|
|
// Find first "non-delimiter".
|
|
|
|
std::string::size_type pos = str.find_first_of(delimiters, lastPos);
|
|
|
|
|
|
|
|
if (std::string::npos != pos || std::string::npos != lastPos) {
|
|
|
|
|
|
|
|
first_str = str.substr(lastPos, pos - lastPos);
|
|
|
|
|
|
|
|
// Skip delimiters. Note the "not_of"
|
|
|
|
lastPos = str.find_first_not_of(delimiters, pos);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
first_pos = lastPos;
|
|
|
|
return first_str;
|
2010-09-14 18:05:00 +04:00
|
|
|
}
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2011-10-04 19:46:24 +04:00
|
|
|
template<class T>
|
|
|
|
T log_sum (T log_a, T log_b)
|
|
|
|
{
|
|
|
|
if (log_a < log_b) {
|
2015-04-08 17:08:05 +03:00
|
|
|
return log_b + log1p(exp(log_a - log_b));
|
2011-10-04 19:46:24 +04:00
|
|
|
} else {
|
2015-04-08 17:08:05 +03:00
|
|
|
return log_a + log1p(exp(log_b - log_a));
|
2011-10-04 19:46:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-06 12:57:38 +03:00
|
|
|
/**
|
|
|
|
* Compare floats for equality with some tolerance.
|
|
|
|
*/
|
2015-01-06 12:55:11 +03:00
|
|
|
inline bool Equals(float a, float b)
|
|
|
|
{
|
|
|
|
return fabs(a - b) < FLOAT_EPSILON;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-30 15:25:36 +04:00
|
|
|
/** Enforce rounding */
|
2014-12-02 22:09:10 +03:00
|
|
|
inline void FixPrecision(std::ostream& stream, size_t size = 3)
|
2014-09-30 15:25:36 +04:00
|
|
|
{
|
|
|
|
stream.setf(std::ios::fixed);
|
|
|
|
stream.precision(size);
|
2008-10-09 03:51:26 +04:00
|
|
|
}
|
|
|
|
|
2014-09-30 17:22:38 +04:00
|
|
|
class FeatureFunction;
|
|
|
|
|
|
|
|
void PrintFeatureWeight(const FeatureFunction* ff);
|
|
|
|
void ShowWeights();
|
2014-09-30 15:25:36 +04:00
|
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|