| #pragma once |
|
|
| #include <boost/thread.hpp> |
| #include <boost/thread/mutex.hpp> |
| #include <fstream> |
| #include <iostream> |
| #include <string> |
| #include <limits> |
| #include <sstream> |
| #include <vector> |
| #include <queue> |
| #include <cmath> |
| #include <stdlib.h> |
| #include "../TypeDef.h" |
| #include "util/exception.hh" |
|
|
| namespace Moses2 |
| { |
|
|
| #ifdef TRACE_ERR |
| #undef TRACE_ERR |
| #endif |
| #ifdef TRACE_ENABLE |
| #define TRACE_ERR(str) do { std::cerr << str; } while (false) |
| #else |
| #define TRACE_ERR(str) do {} while (false) |
| #endif |
|
|
| |
|
|
| template<typename T> |
| class UnorderedComparer |
| { |
| public: |
| size_t operator()(const T* obj) const { |
| return obj->hash(); |
| } |
|
|
| bool operator()(const T* a, const T* b) const { |
| return a->hash() == b->hash(); |
| } |
|
|
| }; |
|
|
| |
|
|
|
|
| template<typename T> |
| void Init(T arr[], size_t size, const T &val) |
| { |
| for (size_t i = 0; i < size; ++i) { |
| arr[i] = val; |
| } |
| } |
|
|
| |
| 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)); |
| } |
|
|
| |
| template<typename T> |
| inline T Scan(const std::string &input) |
| { |
| std::stringstream stream(input); |
| T ret; |
| stream >> ret; |
| return ret; |
| } |
|
|
| |
| template<> |
| inline std::string Scan<std::string>(const std::string &input) |
| { |
| return input; |
| } |
|
|
| template<> |
| inline SCORE Scan<SCORE>(const std::string &input) |
| { |
| SCORE ret = atof(input.c_str()); |
| return ret; |
| } |
|
|
| |
| template<> |
| bool Scan<bool>(const std::string &input); |
|
|
| 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); |
| } |
|
|
| template<> |
| inline SearchAlgorithm Scan<SearchAlgorithm>(const std::string &input) |
| { |
| return (SearchAlgorithm) Scan<size_t>(input); |
| } |
|
|
| 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 { |
| UTIL_THROW2("Unknown XML input type"); |
| } |
|
|
| return ret; |
| } |
|
|
| template<> |
| inline InputTypeEnum Scan<InputTypeEnum>(const std::string &input) |
| { |
| return (InputTypeEnum) Scan<size_t>(input); |
| } |
|
|
| template<> |
| inline WordAlignmentSort Scan<WordAlignmentSort>(const std::string &input) |
| { |
| return (WordAlignmentSort) Scan<size_t>(input); |
| } |
|
|
| |
| template<typename T> |
| inline std::vector<T> Scan(const std::vector<std::string> &input) |
| { |
| std::vector<T> output(input.size()); |
| for (size_t i = 0; i < input.size(); i++) { |
| output[i] = Scan<T>(input[i]); |
| } |
| return output; |
| } |
|
|
| |
| template<typename T> |
| inline void Scan(std::vector<T> &output, const std::vector<std::string> &input) |
| { |
| output.resize(input.size()); |
| for (size_t i = 0; i < input.size(); i++) { |
| output[i] = Scan<T>(input[i]); |
| } |
| } |
|
|
| |
| |
| |
| inline std::vector<std::string> Tokenize(const std::string& str, |
| const std::string& delimiters = " \t") |
| { |
| std::vector<std::string> tokens; |
| |
| std::string::size_type lastPos = str.find_first_not_of(delimiters, 0); |
| |
| std::string::size_type pos = str.find_first_of(delimiters, lastPos); |
|
|
| while (std::string::npos != pos || std::string::npos != lastPos) { |
| |
| tokens.push_back(str.substr(lastPos, pos - lastPos)); |
| |
| lastPos = str.find_first_not_of(delimiters, pos); |
| |
| pos = str.find_first_of(delimiters, lastPos); |
| } |
|
|
| return tokens; |
| } |
|
|
| |
| template<typename T> |
| inline std::vector<T> Tokenize(const std::string &input, |
| const std::string& delimiters = " \t") |
| { |
| std::vector<std::string> stringVector = Tokenize(input, delimiters); |
| return Scan<T>(stringVector); |
| } |
|
|
| |
| |
| |
| 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) { |
| |
| tokens.push_back(str.substr(0, pos)); |
| tokens.push_back(str.substr(pos + 1, str.size() - pos - 1)); |
| } else { |
| tokens.push_back(str); |
| } |
|
|
| return tokens; |
| } |
|
|
| inline std::vector<std::string> TokenizeMultiCharSeparator( |
| const std::string& str, const std::string& separator) |
| { |
| std::vector<std::string> tokens; |
|
|
| size_t pos = 0; |
| |
| std::string::size_type nextPos = str.find(separator, pos); |
|
|
| while (nextPos != std::string::npos) { |
| |
| tokens.push_back(str.substr(pos, nextPos - pos)); |
| |
| pos = nextPos + separator.size(); |
| |
| nextPos = str.find(separator, pos); |
| } |
| tokens.push_back(str.substr(pos, nextPos - pos)); |
|
|
| return tokens; |
| } |
|
|
| |
| inline void TokenizeMultiCharSeparator(std::vector<std::string> &output, |
| const std::string& str, const std::string& separator) |
| { |
| size_t pos = 0; |
| |
| std::string::size_type nextPos = str.find(separator, pos); |
|
|
| while (nextPos != std::string::npos) { |
| |
| output.push_back(Trim(str.substr(pos, nextPos - pos))); |
| |
| pos = nextPos + separator.size(); |
| |
| nextPos = str.find(separator, pos); |
| } |
| output.push_back(Trim(str.substr(pos, nextPos - pos))); |
| } |
|
|
| |
| template<typename T> |
| inline std::string SPrint(const T &input) |
| { |
| std::stringstream stream(""); |
| stream << input; |
| return stream.str(); |
| } |
|
|
| |
| inline float TransformLMScore(float irstScore) |
| { |
| return irstScore * 2.30258509299405f; |
| } |
|
|
| |
| inline float TransformScore(float prob) |
| { |
| return log(prob); |
| } |
|
|
| |
| inline float FloorScore(float logScore) |
| { |
| return (std::max)(logScore, LOWEST_SCORE); |
| } |
|
|
| inline float UntransformLMScore(float logNScore) |
| { |
| |
| return logNScore / 2.30258509299405f; |
| } |
|
|
| inline bool FileExists(const std::string& filePath) |
| { |
| std::ifstream ifs(filePath.c_str()); |
| return !ifs.fail(); |
| } |
|
|
| const std::string ToLower(const std::string& str); |
|
|
| |
| template<class COLL> |
| void RemoveAllInColl(COLL &coll) |
| { |
| for (typename COLL::const_iterator iter = coll.begin(); iter != coll.end(); |
| ++iter) { |
| delete (*iter); |
| } |
| coll.clear(); |
| } |
|
|
| template<typename T> |
| void Swap(T &a, T &b) |
| { |
| T &c = a; |
| a = b; |
| b = c; |
| } |
|
|
| |
| template<class T, class S, class C> |
| S& Container(std::priority_queue<T, S, C>& q) |
| { |
| struct HackedQueue: private std::priority_queue<T, S, C> { |
| static S& Container(std::priority_queue<T, S, C>& q) { |
| return q.*&HackedQueue::c; |
| } |
| }; |
| return HackedQueue::Container(q); |
| } |
|
|
| #define HERE __FILE__ << ":" << __LINE__ |
|
|
| |
| inline void FixPrecision(std::ostream& stream, size_t size = 3) |
| { |
| stream.setf(std::ios::fixed); |
| stream.precision(size); |
| } |
|
|
| } |
|
|
|
|