diff --git a/contrib/other-builds/moses2/legacy/ProbingPT/probing_hash_utils.hh b/contrib/other-builds/moses2/legacy/ProbingPT/probing_hash_utils.hh index 0b33bae78..cc68ec920 100644 --- a/contrib/other-builds/moses2/legacy/ProbingPT/probing_hash_utils.hh +++ b/contrib/other-builds/moses2/legacy/ProbingPT/probing_hash_utils.hh @@ -10,7 +10,7 @@ namespace Moses2 { -#define API_VERSION 10 +#define API_VERSION 11 //Hash table entry struct Entry diff --git a/contrib/other-builds/moses2/legacy/ProbingPT/quering.cpp b/contrib/other-builds/moses2/legacy/ProbingPT/quering.cpp index d0a449792..18bc12fe9 100644 --- a/contrib/other-builds/moses2/legacy/ProbingPT/quering.cpp +++ b/contrib/other-builds/moses2/legacy/ProbingPT/quering.cpp @@ -1,4 +1,7 @@ +#include #include "quering.hh" +#include "util/exception.hh" +#include "../Util2.h" namespace Moses2 { @@ -15,28 +18,54 @@ QueryEngine::QueryEngine(const char * filepath) read_map(source_vocabids, path_to_source_vocabid.c_str()); //Read config file - std::string line; + std::unordered_map keyValue; + std::ifstream config((basepath + "/config").c_str()); + std::string line; + while (getline(config, line)) { + std::vector toks = Moses2::Tokenize(line, "\t"); + UTIL_THROW_IF2(toks.size() != 2, "Wrong config format:" << line); + keyValue[ toks[0] ] = toks[1]; + } + + bool found; //Check API version: - getline(config, line); - int version = atoi(line.c_str()); - if (version != API_VERSION) { + int version; + found = Get(keyValue, "API_VERSION", version); + if (!found || version != API_VERSION) { std::cerr << "The ProbingPT API has changed. " << version << "!=" << API_VERSION << " Please rebinarize your phrase tables." << std::endl; exit(EXIT_FAILURE); } + //Get tablesize. - getline(config, line); - int tablesize = atoi(line.c_str()); + int tablesize; + found = Get(keyValue, "uniq_entries", tablesize); + if (!found) { + std::cerr << "uniq_entries not found" << std::endl; + exit(EXIT_FAILURE); + } + //Number of scores - getline(config, line); - num_scores = atoi(line.c_str()); + found = Get(keyValue, "num_scores", num_scores); + if (!found) { + std::cerr << "num_scores not found" << std::endl; + exit(EXIT_FAILURE); + } + //How may scores from lex reordering models - getline(config, line); - num_lex_scores = atoi(line.c_str()); + found = Get(keyValue, "num_lex_scores", num_lex_scores); + if (!found) { + std::cerr << "num_lex_scores not found" << std::endl; + exit(EXIT_FAILURE); + } + // have the scores been log() and FloorScore()? - getline(config, line); - logProb = atoi(line.c_str()); + found = Get(keyValue, "log_prob", logProb); + if (!found) { + std::cerr << "logProb not found" << std::endl; + exit(EXIT_FAILURE); + } config.close(); diff --git a/contrib/other-builds/moses2/legacy/ProbingPT/quering.hh b/contrib/other-builds/moses2/legacy/ProbingPT/quering.hh index c6d6f000e..e8e595b2c 100644 --- a/contrib/other-builds/moses2/legacy/ProbingPT/quering.hh +++ b/contrib/other-builds/moses2/legacy/ProbingPT/quering.hh @@ -1,5 +1,6 @@ #pragma once +#include #include //For finding size of file #include "vocabid.hh" #include //toLower @@ -7,6 +8,7 @@ #include "probing_hash_utils.hh" #include "hash.hh" //Includes line splitter #include "line_splitter.hh" +#include "../Util2.h" namespace Moses2 { @@ -39,6 +41,19 @@ public: uint64_t getKey(uint64_t source_phrase[], size_t size) const; + template + inline bool Get(const std::unordered_map &keyValue, const std::string &sought, T &found) const + { + std::unordered_map::const_iterator iter = keyValue.find(sought); + if (iter == keyValue.end()) { + return false; + } + + const std::string &foundStr = iter->second; + found = Scan(foundStr); + return true; + } + }; } diff --git a/contrib/other-builds/moses2/legacy/ProbingPT/storing.cpp b/contrib/other-builds/moses2/legacy/ProbingPT/storing.cpp index b4ed36ab1..32c0ab322 100644 --- a/contrib/other-builds/moses2/legacy/ProbingPT/storing.cpp +++ b/contrib/other-builds/moses2/legacy/ProbingPT/storing.cpp @@ -152,11 +152,11 @@ void createProbingPT(const std::string &phrasetable_path, //Write configfile std::ofstream configfile; configfile.open((basepath + "/config").c_str()); - configfile << API_VERSION << '\n'; - configfile << uniq_entries << '\n'; - configfile << num_scores << '\n'; - configfile << num_lex_scores << '\n'; - configfile << log_prob << '\n'; + configfile << "API_VERSION\t" << API_VERSION << '\n'; + configfile << "uniq_entries\t" << uniq_entries << '\n'; + configfile << "num_scores\t" << num_scores << '\n'; + configfile << "num_lex_scores\t" << num_lex_scores << '\n'; + configfile << "log_prob\t" << log_prob << '\n'; configfile.close(); }