diff options
author | Hieu Hoang <hieuhoang@gmail.com> | 2017-02-16 00:26:28 +0300 |
---|---|---|
committer | Hieu Hoang <hieuhoang@gmail.com> | 2017-02-16 00:26:28 +0300 |
commit | 74b123649ea36af6a66790b7dd922ede91d0b4a2 (patch) | |
tree | f17a8a338802e966a315cf3374e1197b4764ce13 /moses2/TranslationModel | |
parent | 8f456299c448eb83a4f71f225cc2bd9d0e4d3212 (diff) |
probing pt lib compiles
Diffstat (limited to 'moses2/TranslationModel')
-rw-r--r-- | moses2/TranslationModel/ProbingPT/StoreTarget.cpp | 264 | ||||
-rw-r--r-- | moses2/TranslationModel/ProbingPT/StoreTarget.h | 51 | ||||
-rw-r--r-- | moses2/TranslationModel/ProbingPT/StoreVocab.cpp | 13 | ||||
-rw-r--r-- | moses2/TranslationModel/ProbingPT/StoreVocab.h | 60 | ||||
-rw-r--r-- | moses2/TranslationModel/ProbingPT/hash.cpp | 44 | ||||
-rw-r--r-- | moses2/TranslationModel/ProbingPT/hash.hh | 17 | ||||
-rw-r--r-- | moses2/TranslationModel/ProbingPT/line_splitter.cpp | 103 | ||||
-rw-r--r-- | moses2/TranslationModel/ProbingPT/line_splitter.hh | 57 | ||||
-rw-r--r-- | moses2/TranslationModel/ProbingPT/probing_hash_utils.cpp | 40 | ||||
-rw-r--r-- | moses2/TranslationModel/ProbingPT/probing_hash_utils.hh | 55 | ||||
-rw-r--r-- | moses2/TranslationModel/ProbingPT/querying.cpp | 179 | ||||
-rw-r--r-- | moses2/TranslationModel/ProbingPT/querying.hh | 78 | ||||
-rw-r--r-- | moses2/TranslationModel/ProbingPT/storing.cpp | 302 | ||||
-rw-r--r-- | moses2/TranslationModel/ProbingPT/storing.hh | 92 | ||||
-rw-r--r-- | moses2/TranslationModel/ProbingPT/vocabid.cpp | 59 | ||||
-rw-r--r-- | moses2/TranslationModel/ProbingPT/vocabid.hh | 29 |
16 files changed, 0 insertions, 1443 deletions
diff --git a/moses2/TranslationModel/ProbingPT/StoreTarget.cpp b/moses2/TranslationModel/ProbingPT/StoreTarget.cpp deleted file mode 100644 index af0de5c31..000000000 --- a/moses2/TranslationModel/ProbingPT/StoreTarget.cpp +++ /dev/null @@ -1,264 +0,0 @@ -/* - * StoreTarget.cpp - * - * Created on: 19 Jan 2016 - * Author: hieu - */ -#include <boost/foreach.hpp> -#include "StoreTarget.h" -#include "line_splitter.hh" -#include "probing_hash_utils.hh" -#include "../../legacy/OutputFileStream.h" -#include "../../legacy/Util2.h" - -using namespace std; - -namespace Moses2 -{ - -StoreTarget::StoreTarget(const std::string &basepath) - :m_basePath(basepath) - ,m_vocab(basepath + "/TargetVocab.dat") -{ - std::string path = basepath + "/TargetColl.dat"; - m_fileTargetColl.open(path.c_str(), - std::ios::out | std::ios::binary | std::ios::ate | std::ios::trunc); - if (!m_fileTargetColl.is_open()) { - throw "can't create file "; - } - -} - -StoreTarget::~StoreTarget() -{ - assert(m_coll.empty()); - m_fileTargetColl.close(); - - // vocab - m_vocab.Save(); -} - -uint64_t StoreTarget::Save() -{ - uint64_t ret = m_fileTargetColl.tellp(); - - // save to disk - uint64_t numTP = m_coll.size(); - m_fileTargetColl.write((char*) &numTP, sizeof(uint64_t)); - - for (size_t i = 0; i < m_coll.size(); ++i) { - Save(*m_coll[i]); - } - - // clear coll - RemoveAllInColl(m_coll); - m_coll.clear(); - - // starting position of coll - return ret; -} - -void StoreTarget::Save(const target_text &rule) -{ - // metadata for each tp - TargetPhraseInfo tpInfo; - tpInfo.alignTerm = GetAlignId(rule.word_align_term); - tpInfo.alignNonTerm = GetAlignId(rule.word_align_non_term); - tpInfo.numWords = rule.target_phrase.size(); - tpInfo.propLength = rule.property.size(); - - //cerr << "TPInfo=" << sizeof(TPInfo); - m_fileTargetColl.write((char*) &tpInfo, sizeof(TargetPhraseInfo)); - - // scores - for (size_t i = 0; i < rule.prob.size(); ++i) { - float prob = rule.prob[i]; - m_fileTargetColl.write((char*) &prob, sizeof(prob)); - } - - // tp - for (size_t i = 0; i < rule.target_phrase.size(); ++i) { - uint32_t vocabId = rule.target_phrase[i]; - m_fileTargetColl.write((char*) &vocabId, sizeof(vocabId)); - } - - // prop TODO - -} - -void StoreTarget::SaveAlignment() -{ - std::string path = m_basePath + "/Alignments.dat"; - Moses2::OutputFileStream file(path); - - BOOST_FOREACH(Alignments::value_type &valPair, m_aligns) { - file << valPair.second << "\t"; - - const std::vector<size_t> &aligns = valPair.first; - BOOST_FOREACH(size_t align, aligns) { - file << align << " "; - } - file << endl; - } - -} - -void StoreTarget::Append(const line_text &line, bool log_prob, bool scfg) -{ - target_text *rule = new target_text; - //cerr << "line.target_phrase=" << line.target_phrase << endl; - - // target_phrase - vector<bool> nonTerms; - util::TokenIter<util::SingleCharacter> it; - it = util::TokenIter<util::SingleCharacter>(line.target_phrase, - util::SingleCharacter(' ')); - while (it) { - StringPiece word = *it; - //cerr << "word=" << word << endl; - - bool nonTerm = false; - if (scfg) { - // not really sure how to handle factored SCFG and NT - if (scfg && word[0] == '[' && word[word.size() - 1] == ']') { - //cerr << "NON-TERM=" << tok << " " << nonTerms.size() << endl; - nonTerm = true; - } - nonTerms.push_back(nonTerm); - } - - util::TokenIter<util::SingleCharacter> itFactor; - itFactor = util::TokenIter<util::SingleCharacter>(word, - util::SingleCharacter('|')); - while (itFactor) { - StringPiece factor = *itFactor; - - string factorStr = factor.as_string(); - uint32_t vocabId = m_vocab.GetVocabId(factorStr); - - rule->target_phrase.push_back(vocabId); - - itFactor++; - } - - it++; - } - - // probs - it = util::TokenIter<util::SingleCharacter>(line.prob, - util::SingleCharacter(' ')); - while (it) { - string tok = it->as_string(); - float prob = Scan<float>(tok); - - if (log_prob) { - prob = FloorScore(log(prob)); - if (prob == 0.0f) prob = 0.0000000001; - } - - rule->prob.push_back(prob); - it++; - } - - /* - cerr << "nonTerms="; - for (size_t i = 0; i < nonTerms.size(); ++i) { - cerr << nonTerms[i] << " "; - } - cerr << endl; - */ - - // alignment - it = util::TokenIter<util::SingleCharacter>(line.word_align, - util::SingleCharacter(' ')); - while (it) { - string tokPair = Trim(it->as_string()); - if (tokPair.empty()) { - break; - } - - vector<size_t> alignPair = Tokenize<size_t>(tokPair, "-"); - assert(alignPair.size() == 2); - - bool nonTerm = false; - size_t sourcePos = alignPair[0]; - size_t targetPos = alignPair[1]; - if (scfg) { - nonTerm = nonTerms[targetPos]; - } - - //cerr << targetPos << "=" << nonTerm << endl; - - if (nonTerm) { - rule->word_align_non_term.push_back(sourcePos); - rule->word_align_non_term.push_back(targetPos); - //cerr << (int) rule->word_all1.back() << " "; - } else { - rule->word_align_term.push_back(sourcePos); - rule->word_align_term.push_back(targetPos); - } - - it++; - } - - // extra scores - string prop = line.property.as_string(); - AppendLexRO(prop, rule->prob, log_prob); - - //cerr << "line.property=" << line.property << endl; - //cerr << "prop=" << prop << endl; - - // properties - /* - for (size_t i = 0; i < prop.size(); ++i) { - rule->property.push_back(prop[i]); - } - */ - m_coll.push_back(rule); -} - -uint32_t StoreTarget::GetAlignId(const std::vector<size_t> &align) -{ - boost::unordered_map<std::vector<size_t>, uint32_t>::iterator iter = - m_aligns.find(align); - if (iter == m_aligns.end()) { - uint32_t ind = m_aligns.size(); - m_aligns[align] = ind; - return ind; - } else { - return iter->second; - } -} - -void StoreTarget::AppendLexRO(std::string &prop, std::vector<float> &retvector, - bool log_prob) const -{ - size_t startPos = prop.find("{{LexRO "); - - if (startPos != string::npos) { - size_t endPos = prop.find("}}", startPos + 8); - string lexProb = prop.substr(startPos + 8, endPos - startPos - 8); - //cerr << "lexProb=" << lexProb << endl; - - // append lex probs to pt probs - vector<float> scores = Tokenize<float>(lexProb); - - if (log_prob) { - for (size_t i = 0; i < scores.size(); ++i) { - scores[i] = FloorScore(log(scores[i])); - if (scores[i] == 0.0f) scores[i] = 0.0000000001; - } - } - - for (size_t i = 0; i < scores.size(); ++i) { - retvector.push_back(scores[i]); - } - - // exclude LexRO property from property column - prop = prop.substr(0, startPos) - + prop.substr(endPos + 2, prop.size() - endPos - 2); - //cerr << "line.property_to_be_binarized=" << line.property_to_be_binarized << "AAAA" << endl; - } -} - -} /* namespace Moses2 */ diff --git a/moses2/TranslationModel/ProbingPT/StoreTarget.h b/moses2/TranslationModel/ProbingPT/StoreTarget.h deleted file mode 100644 index 77ccf08cd..000000000 --- a/moses2/TranslationModel/ProbingPT/StoreTarget.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * StoreTarget.h - * - * Created on: 19 Jan 2016 - * Author: hieu - */ -#pragma once -#include <string> -#include <fstream> -#include <vector> -#include <inttypes.h> -#include <boost/unordered_map.hpp> -#include <boost/unordered_set.hpp> -#include "StoreVocab.h" - -namespace Moses2 -{ - -class line_text; -class target_text; - -class StoreTarget -{ -public: - StoreTarget(const std::string &basepath); - virtual ~StoreTarget(); - - uint64_t Save(); - void SaveAlignment(); - - void Append(const line_text &line, bool log_prob, bool scfg); -protected: - std::string m_basePath; - std::fstream m_fileTargetColl; - StoreVocab<uint32_t> m_vocab; - - typedef boost::unordered_map<std::vector<size_t>, uint32_t> Alignments; - Alignments m_aligns; - - std::vector<target_text*> m_coll; - - uint32_t GetAlignId(const std::vector<size_t> &align); - void Save(const target_text &rule); - - void AppendLexRO(std::string &prop, std::vector<float> &retvector, - bool log_prob) const; - -}; - -} /* namespace Moses2 */ - diff --git a/moses2/TranslationModel/ProbingPT/StoreVocab.cpp b/moses2/TranslationModel/ProbingPT/StoreVocab.cpp deleted file mode 100644 index e0b5b0b08..000000000 --- a/moses2/TranslationModel/ProbingPT/StoreVocab.cpp +++ /dev/null @@ -1,13 +0,0 @@ -/* - * StoreVocab.cpp - * - * Created on: 15 Jun 2016 - * Author: hieu - */ -#include <fstream> -#include "StoreVocab.h" - -namespace Moses2 -{ - -} /* namespace Moses2 */ diff --git a/moses2/TranslationModel/ProbingPT/StoreVocab.h b/moses2/TranslationModel/ProbingPT/StoreVocab.h deleted file mode 100644 index 3c405af66..000000000 --- a/moses2/TranslationModel/ProbingPT/StoreVocab.h +++ /dev/null @@ -1,60 +0,0 @@ -/* - * StoreVocab.h - * - * Created on: 15 Jun 2016 - * Author: hieu - */ -#pragma once -#include <string> -#include <boost/unordered_map.hpp> -#include "../../legacy/OutputFileStream.h" -#include "../../legacy/Util2.h" - -namespace Moses2 -{ - -template<typename VOCABID> -class StoreVocab -{ -protected: - std::string m_path; - - typedef boost::unordered_map<std::string, VOCABID> Coll; - Coll m_vocab; - -public: - StoreVocab(const std::string &path) - :m_path(path) - {} - - virtual ~StoreVocab() {} - - VOCABID GetVocabId(const std::string &word) { - typename Coll::iterator iter = m_vocab.find(word); - if (iter == m_vocab.end()) { - VOCABID ind = m_vocab.size() + 1; - m_vocab[word] = ind; - return ind; - } else { - return iter->second; - } - } - - void Insert(VOCABID id, const std::string &word) { - m_vocab[word] = id; - } - - void Save() { - OutputFileStream strme(m_path); - - typename Coll::const_iterator iter; - for (iter = m_vocab.begin(); iter != m_vocab.end(); ++iter) { - strme << iter->first << "\t" << iter->second << std::endl; - } - - strme.Close(); - } -}; - -} /* namespace Moses2 */ - diff --git a/moses2/TranslationModel/ProbingPT/hash.cpp b/moses2/TranslationModel/ProbingPT/hash.cpp deleted file mode 100644 index a0bda389a..000000000 --- a/moses2/TranslationModel/ProbingPT/hash.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include <iostream> -#include "hash.hh" - -using namespace std; - -namespace Moses2 -{ - -uint64_t getHash(StringPiece text) -{ - std::size_t len = text.size(); - uint64_t key = util::MurmurHashNative(text.data(), len); - return key; -} - -std::vector<uint64_t> getVocabIDs(const StringPiece &textin) -{ - //Tokenize - std::vector<uint64_t> output; - - util::TokenIter<util::SingleCharacter> itWord(textin, util::SingleCharacter(' ')); - - while (itWord) { - StringPiece word = *itWord; - uint64_t id = 0; - - util::TokenIter<util::SingleCharacter> itFactor(word, util::SingleCharacter('|')); - while (itFactor) { - StringPiece factor = *itFactor; - //cerr << "factor=" << factor << endl; - - id += getHash(factor); - itFactor++; - } - - output.push_back(id); - itWord++; - } - - return output; -} - -} - diff --git a/moses2/TranslationModel/ProbingPT/hash.hh b/moses2/TranslationModel/ProbingPT/hash.hh deleted file mode 100644 index 78cc27999..000000000 --- a/moses2/TranslationModel/ProbingPT/hash.hh +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -#include "util/string_piece.hh" -#include "util/murmur_hash.hh" -#include "util/string_piece.hh" //Tokenization and work with StringPiece -#include "util/tokenize_piece.hh" -#include <vector> - -namespace Moses2 -{ - -//Gets the MurmurmurHash for give string -uint64_t getHash(StringPiece text); - -std::vector<uint64_t> getVocabIDs(const StringPiece &textin); - -} diff --git a/moses2/TranslationModel/ProbingPT/line_splitter.cpp b/moses2/TranslationModel/ProbingPT/line_splitter.cpp deleted file mode 100644 index e4b5e2694..000000000 --- a/moses2/TranslationModel/ProbingPT/line_splitter.cpp +++ /dev/null @@ -1,103 +0,0 @@ -#include "line_splitter.hh" - -namespace Moses2 -{ - -line_text splitLine(const StringPiece &textin, bool scfg) -{ - const char delim[] = "|||"; - line_text output; - - //Tokenize - util::TokenIter<util::MultiCharacter> it(textin, util::MultiCharacter(delim)); - //Get source phrase - output.source_phrase = Trim(*it); - //std::cerr << "output.source_phrase=" << output.source_phrase << "AAAA" << std::endl; - - //Get target_phrase - it++; - output.target_phrase = Trim(*it); - //std::cerr << "output.target_phrase=" << output.target_phrase << "AAAA" << std::endl; - - if (scfg) { - /* - std::cerr << "output.source_phrase=" << output.source_phrase << std::endl; - std::cerr << "output.target_phrase=" << output.target_phrase << std::endl; - reformatSCFG(output); - std::cerr << "output.source_phrase=" << output.source_phrase << std::endl; - std::cerr << "output.target_phrase=" << output.target_phrase << std::endl; - */ - } - - //Get probabilities - it++; - output.prob = Trim(*it); - //std::cerr << "output.prob=" << output.prob << "AAAA" << std::endl; - - //Get WordAllignment - it++; - if (it == util::TokenIter<util::MultiCharacter>::end()) return output; - output.word_align = Trim(*it); - //std::cerr << "output.word_align=" << output.word_align << "AAAA" << std::endl; - - //Get count - it++; - if (it == util::TokenIter<util::MultiCharacter>::end()) return output; - output.counts = Trim(*it); - //std::cerr << "output.counts=" << output.counts << "AAAA" << std::endl; - - //Get sparse_score - it++; - if (it == util::TokenIter<util::MultiCharacter>::end()) return output; - output.sparse_score = Trim(*it); - //std::cerr << "output.sparse_score=" << output.sparse_score << "AAAA" << std::endl; - - //Get property - it++; - if (it == util::TokenIter<util::MultiCharacter>::end()) return output; - output.property = Trim(*it); - //std::cerr << "output.property=" << output.property << "AAAA" << std::endl; - - return output; -} - -std::vector<unsigned char> splitWordAll1(const StringPiece &textin) -{ - const char delim[] = " "; - const char delim2[] = "-"; - std::vector<unsigned char> output; - - //Case with no word alignments. - if (textin.size() == 0) { - return output; - } - - //Split on space - util::TokenIter<util::MultiCharacter> it(textin, util::MultiCharacter(delim)); - - //For each int - while (it) { - //Split on dash (-) - util::TokenIter<util::MultiCharacter> itInner(*it, - util::MultiCharacter(delim2)); - - //Insert the two entries in the vector. User will read entry 0 and 1 to get the first, - //2 and 3 for second etc. Use unsigned char instead of int to save space, as - //word allignments are all very small numbers that fit in a single byte - output.push_back((unsigned char) (atoi(itInner->data()))); - itInner++; - output.push_back((unsigned char) (atoi(itInner->data()))); - it++; - } - - return output; - -} - -void reformatSCFG(line_text &output) -{ - -} - -} - diff --git a/moses2/TranslationModel/ProbingPT/line_splitter.hh b/moses2/TranslationModel/ProbingPT/line_splitter.hh deleted file mode 100644 index 0b91fed09..000000000 --- a/moses2/TranslationModel/ProbingPT/line_splitter.hh +++ /dev/null @@ -1,57 +0,0 @@ -#pragma once - -#include "util/string_piece.hh" -#include "util/tokenize_piece.hh" -#include "util/file_piece.hh" -#include <vector> -#include <cstdlib> //atof -#include "util/string_piece.hh" //Tokenization and work with StringPiece -#include "util/tokenize_piece.hh" -#include <vector> - -namespace Moses2 -{ - -//Struct for holding processed line -struct line_text { - StringPiece source_phrase; - StringPiece target_phrase; - StringPiece prob; - StringPiece word_align; - StringPiece counts; - StringPiece sparse_score; - StringPiece property; - std::string property_to_be_binarized; -}; - -//Struct for holding processed line -struct target_text { - std::vector<unsigned int> target_phrase; - std::vector<float> prob; - std::vector<size_t> word_align_term; - std::vector<size_t> word_align_non_term; - std::vector<char> counts; - std::vector<char> sparse_score; - std::vector<char> property; - - /* - void Reset() - { - target_phrase.clear(); - prob.clear(); - word_all1.clear(); - counts.clear(); - sparse_score.clear(); - property.clear(); - } - */ -}; - -//Ask if it's better to have it receive a pointer to a line_text struct -line_text splitLine(const StringPiece &textin, bool scfg); -void reformatSCFG(line_text &output); - -std::vector<unsigned char> splitWordAll1(const StringPiece &textin); - -} - diff --git a/moses2/TranslationModel/ProbingPT/probing_hash_utils.cpp b/moses2/TranslationModel/ProbingPT/probing_hash_utils.cpp deleted file mode 100644 index 96c317b65..000000000 --- a/moses2/TranslationModel/ProbingPT/probing_hash_utils.cpp +++ /dev/null @@ -1,40 +0,0 @@ -#include <iostream> -#include "probing_hash_utils.hh" -#include "util/file.hh" - -namespace Moses2 -{ - -//Read table from disk, return memory map location -char * readTable(const char * filename, util::LoadMethod load_method, util::scoped_fd &file, util::scoped_memory &memory) -{ - //std::cerr << "filename=" << filename << std::endl; - file.reset(util::OpenReadOrThrow(filename)); - uint64_t total_size_ = util::SizeFile(file.get()); - - MapRead(load_method, file.get(), 0, total_size_, memory); - - return (char*) memory.get(); -} - -void serialize_table(char *mem, size_t size, const std::string &filename) -{ - std::ofstream os(filename.c_str(), std::ios::binary); - os.write((const char*) &mem[0], size); - os.close(); - -} - -uint64_t getKey(const uint64_t source_phrase[], size_t size) -{ - //TOO SLOW - //uint64_t key = util::MurmurHashNative(&source_phrase[0], source_phrase.size()); - uint64_t key = 0; - for (size_t i = 0; i < size; i++) { - key += (source_phrase[i] << i); - } - return key; -} - -} - diff --git a/moses2/TranslationModel/ProbingPT/probing_hash_utils.hh b/moses2/TranslationModel/ProbingPT/probing_hash_utils.hh deleted file mode 100644 index fa5a9a5a0..000000000 --- a/moses2/TranslationModel/ProbingPT/probing_hash_utils.hh +++ /dev/null @@ -1,55 +0,0 @@ -#pragma once - -#include "util/probing_hash_table.hh" - -#if defined(_WIN32) || defined(_WIN64) -#include <mman.h> -#else -#include <sys/mman.h> -#endif -#include <boost/functional/hash.hpp> -#include <fcntl.h> -#include <fstream> - -namespace Moses2 -{ - -#define API_VERSION 15 - -//Hash table entry -struct Entry { - typedef uint64_t Key; - Key key; - - Key GetKey() const { - return key; - } - - void SetKey(Key to) { - key = to; - } - - uint64_t value; -}; - -#define NONE std::numeric_limits<uint64_t>::max() - -//Define table -typedef util::ProbingHashTable<Entry, boost::hash<uint64_t> > Table; - -void serialize_table(char *mem, size_t size, const std::string &filename); - -char * readTable(const char * filename, util::LoadMethod load_method, util::scoped_fd &file, util::scoped_memory &memory); - -uint64_t getKey(const uint64_t source_phrase[], size_t size); - -struct TargetPhraseInfo { - uint32_t alignTerm; - uint32_t alignNonTerm; - uint16_t numWords; - uint16_t propLength; - uint16_t filler; -}; - -} - diff --git a/moses2/TranslationModel/ProbingPT/querying.cpp b/moses2/TranslationModel/ProbingPT/querying.cpp deleted file mode 100644 index d09bd45a8..000000000 --- a/moses2/TranslationModel/ProbingPT/querying.cpp +++ /dev/null @@ -1,179 +0,0 @@ -#include "querying.hh" -#include "util/exception.hh" -#include "../../legacy/Util2.h" - -using namespace std; - -namespace Moses2 -{ - -QueryEngine::QueryEngine(const char * filepath, util::LoadMethod load_method) -{ - - //Create filepaths - std::string basepath(filepath); - std::string path_to_config = basepath + "/config"; - std::string path_to_hashtable = basepath + "/probing_hash.dat"; - std::string path_to_source_vocabid = basepath + "/source_vocabids"; - std::string alignPath = basepath + "/Alignments.dat"; - - file_exits(basepath); - - ///Source phrase vocabids - read_map(source_vocabids, path_to_source_vocabid.c_str()); - - // alignments - read_alignments(alignPath); - - // target phrase - string targetCollPath = basepath + "/TargetColl.dat"; - memTPS = readTable(targetCollPath.c_str(), load_method, fileTPS_, memoryTPS_); - - //Read config file - boost::unordered_map<std::string, std::string> keyValue; - - std::ifstream config(path_to_config.c_str()); - std::string line; - while (getline(config, line)) { - std::vector<std::string> 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: - int version; - found = Get(keyValue, "API_VERSION", version); - if (!found) { - std::cerr << "Old or corrupted version of ProbingPT. Please rebinarize your phrase tables." << std::endl; - } else if (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. - int tablesize; - found = Get(keyValue, "uniq_entries", tablesize); - if (!found) { - std::cerr << "uniq_entries not found" << std::endl; - exit(EXIT_FAILURE); - } - - //Number of scores - 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 - 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()? - found = Get(keyValue, "log_prob", logProb); - if (!found) { - std::cerr << "logProb not found" << std::endl; - exit(EXIT_FAILURE); - } - - config.close(); - - //Read hashtable - table_filesize = Table::Size(tablesize, 1.2); - mem = readTable(path_to_hashtable.c_str(), load_method, file_, memory_); - Table table_init(mem, table_filesize); - table = table_init; - - std::cerr << "Initialized successfully! " << std::endl; -} - -QueryEngine::~QueryEngine() -{ - //Clear mmap content from memory. - //munmap(mem, table_filesize); - -} - -uint64_t QueryEngine::getKey(uint64_t source_phrase[], size_t size) const -{ - //TOO SLOW - //uint64_t key = util::MurmurHashNative(&source_phrase[0], source_phrase.size()); - return Moses2::getKey(source_phrase, size); -} - -std::pair<bool, uint64_t> QueryEngine::query(uint64_t key) -{ - std::pair<bool, uint64_t> ret; - - const Entry * entry; - ret.first = table.Find(key, entry); - if (ret.first) { - ret.second = entry->value; - } - return ret; -} - -void QueryEngine::read_alignments(const std::string &alignPath) -{ - std::ifstream strm(alignPath.c_str()); - - string line; - while (getline(strm, line)) { - vector<string> toks = Moses2::Tokenize(line, "\t "); - UTIL_THROW_IF2(toks.size() == 0, "Corrupt alignment file"); - - uint32_t alignInd = Scan<uint32_t>(toks[0]); - if (alignInd >= alignColl.size()) { - alignColl.resize(alignInd + 1); - } - - Alignments &aligns = alignColl[alignInd]; - for (size_t i = 1; i < toks.size(); ++i) { - size_t pos = Scan<size_t>(toks[i]); - aligns.push_back(pos); - } - } -} - -void QueryEngine::file_exits(const std::string &basePath) -{ - if (!FileExists(basePath + "/Alignments.dat")) { - UTIL_THROW2("Require file does not exist in: " << basePath << "/Alignments.dat"); - } - if (!FileExists(basePath + "/TargetColl.dat")) { - UTIL_THROW2("Require file does not exist in: " << basePath << "/TargetColl.dat"); - } - if (!FileExists(basePath + "/TargetVocab.dat")) { - UTIL_THROW2("Require file does not exist in: " << basePath << "/TargetVocab.dat"); - } - if (!FileExists(basePath + "/cache")) { - UTIL_THROW2("Require file does not exist in: " << basePath << "/cache"); - } - if (!FileExists(basePath + "/config")) { - UTIL_THROW2("Require file does not exist in: " << basePath << "/config"); - } - if (!FileExists(basePath + "/probing_hash.dat")) { - UTIL_THROW2("Require file does not exist in: " << basePath << "/probing_hash.dat"); - } - if (!FileExists(basePath + "/source_vocabids")) { - UTIL_THROW2("Require file does not exist in: " << basePath << "/source_vocabids"); - } - - /* - - if (!FileExists(path_to_config) || !FileExists(path_to_hashtable) || - !FileExists(path_to_source_vocabid) || !FileExists(basepath + alignPath) || - !FileExists(basepath + "/TargetColl.dat") || !FileExists(basepath + "/TargetVocab.dat") || - !FileExists(basepath + "/cache")) { - UTIL_THROW2("A required table doesn't exist in: " << basepath); - } - */ -} - -} - diff --git a/moses2/TranslationModel/ProbingPT/querying.hh b/moses2/TranslationModel/ProbingPT/querying.hh deleted file mode 100644 index 4cb3228f2..000000000 --- a/moses2/TranslationModel/ProbingPT/querying.hh +++ /dev/null @@ -1,78 +0,0 @@ -#pragma once - -#include <boost/iostreams/device/mapped_file.hpp> -#include <boost/unordered_map.hpp> -#include <sys/stat.h> //For finding size of file -#include "vocabid.hh" -#include <algorithm> //toLower -#include <deque> -#include "probing_hash_utils.hh" -#include "hash.hh" //Includes line splitter -#include "line_splitter.hh" -#include "../../legacy/Util2.h" - -namespace Moses2 -{ - -class QueryEngine -{ - std::map<uint64_t, std::string> source_vocabids; - - typedef std::vector<unsigned char> Alignments; - std::vector<Alignments> alignColl; - - Table table; - char *mem; //Memory for the table, necessary so that we can correctly destroy the object - - size_t table_filesize; - bool is_reordering; - - util::scoped_fd file_; - util::scoped_memory memory_; - - // target phrases - boost::iostreams::mapped_file_source file; - - util::scoped_fd fileTPS_; - util::scoped_memory memoryTPS_; - - void read_alignments(const std::string &alignPath); - void file_exits(const std::string &basePath); - -public: - int num_scores; - int num_lex_scores; - bool logProb; - const char *memTPS; - - QueryEngine(const char *, util::LoadMethod load_method); - ~QueryEngine(); - - std::pair<bool, uint64_t> query(uint64_t key); - - const std::map<uint64_t, std::string> &getSourceVocab() const { - return source_vocabids; - } - - const std::vector<Alignments> &getAlignments() const { - return alignColl; - } - - uint64_t getKey(uint64_t source_phrase[], size_t size) const; - - template<typename T> - inline bool Get(const boost::unordered_map<std::string, std::string> &keyValue, const std::string &sought, T &found) const { - boost::unordered_map<std::string, std::string>::const_iterator iter = keyValue.find(sought); - if (iter == keyValue.end()) { - return false; - } - - const std::string &foundStr = iter->second; - found = Scan<T>(foundStr); - return true; - } - -}; - -} - diff --git a/moses2/TranslationModel/ProbingPT/storing.cpp b/moses2/TranslationModel/ProbingPT/storing.cpp deleted file mode 100644 index ff26bc72b..000000000 --- a/moses2/TranslationModel/ProbingPT/storing.cpp +++ /dev/null @@ -1,302 +0,0 @@ -#include <sys/stat.h> -#include <boost/foreach.hpp> -#include "line_splitter.hh" -#include "storing.hh" -#include "StoreTarget.h" -#include "StoreVocab.h" -#include "../../legacy/Util2.h" -#include "../../legacy/InputFileStream.h" - -using namespace std; - -namespace Moses2 -{ - -/////////////////////////////////////////////////////////////////////// -void Node::Add(Table &table, const SourcePhrase &sourcePhrase, size_t pos) -{ - if (pos < sourcePhrase.size()) { - uint64_t vocabId = sourcePhrase[pos]; - - Node *child; - Children::iterator iter = m_children.find(vocabId); - if (iter == m_children.end()) { - // New node. Write other children then discard them - BOOST_FOREACH(Children::value_type &valPair, m_children) { - Node &otherChild = valPair.second; - otherChild.Write(table); - } - m_children.clear(); - - // create new node - child = &m_children[vocabId]; - assert(!child->done); - child->key = key + (vocabId << pos); - } else { - child = &iter->second; - } - - child->Add(table, sourcePhrase, pos + 1); - } else { - // this node was written previously 'cos it has rules - done = true; - } -} - -void Node::Write(Table &table) -{ - //cerr << "START write " << done << " " << key << endl; - BOOST_FOREACH(Children::value_type &valPair, m_children) { - Node &child = valPair.second; - child.Write(table); - } - - if (!done) { - // save - Entry sourceEntry; - sourceEntry.value = NONE; - sourceEntry.key = key; - - //Put into table - table.Insert(sourceEntry); - } -} - -/////////////////////////////////////////////////////////////////////// -void createProbingPT(const std::string &phrasetable_path, - const std::string &basepath, int num_scores, int num_lex_scores, - bool log_prob, int max_cache_size, bool scfg) -{ -#if defined(_WIN32) || defined(_WIN64) - std::cerr << "Create not implemented for Windows" << std::endl; -#else - std::cerr << "Starting..." << std::endl; - - //Get basepath and create directory if missing - mkdir(basepath.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); - - StoreTarget storeTarget(basepath); - - //Get uniq lines: - unsigned long uniq_entries = countUniqueSource(phrasetable_path); - - //Source phrase vocabids - StoreVocab<uint64_t> sourceVocab(basepath + "/source_vocabids"); - - //Read the file - util::FilePiece filein(phrasetable_path.c_str()); - - //Init the probing hash table - size_t size = Table::Size(uniq_entries, 1.2); - char * mem = new char[size]; - memset(mem, 0, size); - Table sourceEntries(mem, size); - - std::priority_queue<CacheItem*, std::vector<CacheItem*>, CacheItemOrderer> cache; - float totalSourceCount = 0; - - //Keep track of the size of each group of target phrases - size_t line_num = 0; - - //Read everything and processs - std::string prevSource; - - Node sourcePhrases; - sourcePhrases.done = true; - sourcePhrases.key = 0; - - while (true) { - try { - //Process line read - line_text line; - line = splitLine(filein.ReadLine(), scfg); - //cerr << "line=" << line.source_phrase << endl; - - ++line_num; - if (line_num % 1000000 == 0) { - std::cerr << line_num << " " << std::flush; - } - - //Add source phrases to vocabularyIDs - add_to_map(sourceVocab, line.source_phrase); - - if (prevSource.empty()) { - // 1st line - prevSource = line.source_phrase.as_string(); - storeTarget.Append(line, log_prob, scfg); - } else if (prevSource == line.source_phrase) { - //If we still have the same line, just append to it: - storeTarget.Append(line, log_prob, scfg); - } else { - assert(prevSource != line.source_phrase); - - //Create a new entry even - - // save - uint64_t targetInd = storeTarget.Save(); - - // next line - storeTarget.Append(line, log_prob, scfg); - - //Create an entry for the previous source phrase: - Entry sourceEntry; - sourceEntry.value = targetInd; - //The key is the sum of hashes of individual words bitshifted by their position in the phrase. - //Probably not entirerly correct, but fast and seems to work fine in practise. - std::vector<uint64_t> vocabid_source = getVocabIDs(prevSource); - if (scfg) { - // storing prefixes? - sourcePhrases.Add(sourceEntries, vocabid_source); - } - sourceEntry.key = getKey(vocabid_source); - - /* - cerr << "prevSource=" << prevSource << flush - << " vocabids=" << Debug(vocabid_source) << flush - << " key=" << sourceEntry.key << endl; - */ - //Put into table - sourceEntries.Insert(sourceEntry); - - // update cache - CURRENT source phrase, not prev - if (max_cache_size) { - std::string countStr = line.counts.as_string(); - countStr = Trim(countStr); - if (!countStr.empty()) { - std::vector<float> toks = Tokenize<float>(countStr); - //cerr << "CACHE:" << line.source_phrase << " " << countStr << " " << toks[1] << endl; - - if (toks.size() >= 2) { - totalSourceCount += toks[1]; - - // compute key for CURRENT source - std::vector<uint64_t> currVocabidSource = getVocabIDs(line.source_phrase.as_string()); - uint64_t currKey = getKey(currVocabidSource); - - CacheItem *item = new CacheItem( - Trim(line.source_phrase.as_string()), - currKey, - toks[1]); - cache.push(item); - - if (max_cache_size > 0 && cache.size() > max_cache_size) { - cache.pop(); - } - } - } - } - - //Set prevLine - prevSource = line.source_phrase.as_string(); - } - - } catch (util::EndOfFileException e) { - std::cerr - << "Reading phrase table finished, writing remaining files to disk." - << std::endl; - - //After the final entry is constructed we need to add it to the phrase_table - //Create an entry for the previous source phrase: - uint64_t targetInd = storeTarget.Save(); - - Entry sourceEntry; - sourceEntry.value = targetInd; - - //The key is the sum of hashes of individual words. Probably not entirerly correct, but fast - std::vector<uint64_t> vocabid_source = getVocabIDs(prevSource); - sourceEntry.key = getKey(vocabid_source); - - //Put into table - sourceEntries.Insert(sourceEntry); - - break; - } - } - - sourcePhrases.Write(sourceEntries); - - storeTarget.SaveAlignment(); - - serialize_table(mem, size, (basepath + "/probing_hash.dat")); - - sourceVocab.Save(); - - serialize_cache(cache, (basepath + "/cache"), totalSourceCount); - - delete[] mem; - - //Write configfile - std::ofstream configfile; - configfile.open((basepath + "/config").c_str()); - 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(); -#endif -} - -size_t countUniqueSource(const std::string &path) -{ - size_t ret = 0; - InputFileStream strme(path); - - std::string line, prevSource; - while (std::getline(strme, line)) { - std::vector<std::string> toks = TokenizeMultiCharSeparator(line, "|||"); - assert(toks.size() != 0); - - if (prevSource != toks[0]) { - prevSource = toks[0]; - ++ret; - } - } - - return ret; -} - -void serialize_cache( - std::priority_queue<CacheItem*, std::vector<CacheItem*>, CacheItemOrderer> &cache, - const std::string &path, float totalSourceCount) -{ - std::vector<const CacheItem*> vec(cache.size()); - - size_t ind = cache.size() - 1; - while (!cache.empty()) { - const CacheItem *item = cache.top(); - vec[ind] = item; - cache.pop(); - --ind; - } - - std::ofstream os(path.c_str()); - - os << totalSourceCount << std::endl; - for (size_t i = 0; i < vec.size(); ++i) { - const CacheItem *item = vec[i]; - os << item->count << "\t" << item->sourceKey << "\t" << item->source << std::endl; - delete item; - } - - os.close(); -} - -uint64_t getKey(const std::vector<uint64_t> &vocabid_source) -{ - return Moses2::getKey(vocabid_source.data(), vocabid_source.size()); -} - -std::vector<uint64_t> CreatePrefix(const std::vector<uint64_t> &vocabid_source, size_t endPos) -{ - assert(endPos < vocabid_source.size()); - - std::vector<uint64_t> ret(endPos + 1); - for (size_t i = 0; i <= endPos; ++i) { - ret[i] = vocabid_source[i]; - } - return ret; -} - -} - diff --git a/moses2/TranslationModel/ProbingPT/storing.hh b/moses2/TranslationModel/ProbingPT/storing.hh deleted file mode 100644 index a6f8dd143..000000000 --- a/moses2/TranslationModel/ProbingPT/storing.hh +++ /dev/null @@ -1,92 +0,0 @@ -#pragma once - -#include <boost/unordered_set.hpp> -#include <boost/unordered_map.hpp> -#include <cstdio> -#include <sstream> -#include <fstream> -#include <iostream> -#include <string> -#include <queue> -#include <sys/stat.h> //mkdir - -#include "hash.hh" //Includes line_splitter -#include "probing_hash_utils.hh" - -#include "util/file_piece.hh" -#include "util/file.hh" -#include "vocabid.hh" - -namespace Moses2 -{ -typedef std::vector<uint64_t> SourcePhrase; - - -class Node -{ - typedef boost::unordered_map<uint64_t, Node> Children; - Children m_children; - -public: - uint64_t key; - bool done; - - Node() - :done(false) - {} - - void Add(Table &table, const SourcePhrase &sourcePhrase, size_t pos = 0); - void Write(Table &table); -}; - - -void createProbingPT(const std::string &phrasetable_path, - const std::string &basepath, int num_scores, int num_lex_scores, - bool log_prob, int max_cache_size, bool scfg); -uint64_t getKey(const std::vector<uint64_t> &source_phrase); - -std::vector<uint64_t> CreatePrefix(const std::vector<uint64_t> &vocabid_source, size_t endPos); - -template<typename T> -std::string Debug(const std::vector<T> &vec) -{ - std::stringstream strm; - for (size_t i = 0; i < vec.size(); ++i) { - strm << vec[i] << " "; - } - return strm.str(); -} - -size_t countUniqueSource(const std::string &path); - -class CacheItem -{ -public: - std::string source; - uint64_t sourceKey; - float count; - CacheItem(const std::string &vSource, uint64_t vSourceKey, float vCount) - :source(vSource) - ,sourceKey(vSourceKey) - ,count(vCount) { - } - - bool operator<(const CacheItem &other) const { - return count > other.count; - } -}; - -class CacheItemOrderer -{ -public: - bool operator()(const CacheItem* a, const CacheItem* b) const { - return (*a) < (*b); - } -}; - -void serialize_cache( - std::priority_queue<CacheItem*, std::vector<CacheItem*>, CacheItemOrderer> &cache, - const std::string &path, float totalSourceCount); - -} - diff --git a/moses2/TranslationModel/ProbingPT/vocabid.cpp b/moses2/TranslationModel/ProbingPT/vocabid.cpp deleted file mode 100644 index e752f76bc..000000000 --- a/moses2/TranslationModel/ProbingPT/vocabid.cpp +++ /dev/null @@ -1,59 +0,0 @@ -#include <boost/foreach.hpp> -#include "vocabid.hh" -#include "StoreVocab.h" -#include "../../legacy/Util2.h" - -namespace Moses2 -{ - -void add_to_map(StoreVocab<uint64_t> &sourceVocab, - const StringPiece &textin) -{ - //Tokenize - util::TokenIter<util::SingleCharacter> itWord(textin, util::SingleCharacter(' ')); - - while (itWord) { - StringPiece word = *itWord; - - util::TokenIter<util::SingleCharacter> itFactor(word, util::SingleCharacter('|')); - while (itFactor) { - StringPiece factor = *itFactor; - - sourceVocab.Insert(getHash(factor), factor.as_string()); - itFactor++; - } - itWord++; - } -} - -void serialize_map(const std::map<uint64_t, std::string> &karta, - const std::string &filename) -{ - std::ofstream os(filename.c_str()); - - std::map<uint64_t, std::string>::const_iterator iter; - for (iter = karta.begin(); iter != karta.end(); ++iter) { - os << iter->first << '\t' << iter->second << std::endl; - } - - os.close(); -} - -void read_map(std::map<uint64_t, std::string> &karta, const char* filename) -{ - std::ifstream is(filename); - - std::string line; - while (getline(is, line)) { - std::vector<std::string> toks = Tokenize(line, "\t"); - assert(toks.size() == 2); - uint64_t ind = Scan<uint64_t>(toks[1]); - karta[ind] = toks[0]; - } - - //Close the stream after we are done. - is.close(); -} - -} - diff --git a/moses2/TranslationModel/ProbingPT/vocabid.hh b/moses2/TranslationModel/ProbingPT/vocabid.hh deleted file mode 100644 index 9d37bd1b5..000000000 --- a/moses2/TranslationModel/ProbingPT/vocabid.hh +++ /dev/null @@ -1,29 +0,0 @@ -//Serialization -#include <boost/serialization/serialization.hpp> -#include <boost/serialization/map.hpp> -#include <boost/archive/text_iarchive.hpp> -#include <boost/archive/text_oarchive.hpp> -#include <fstream> -#include <iostream> -#include <vector> - -#include <map> //Container -#include "hash.hh" //Hash of elements - -#include "util/string_piece.hh" //Tokenization and work with StringPiece -#include "util/tokenize_piece.hh" - -namespace Moses2 -{ -template<typename VOCABID> -class StoreVocab; - -void add_to_map(StoreVocab<uint64_t> &sourceVocab, - const StringPiece &textin); - -void serialize_map(const std::map<uint64_t, std::string> &karta, - const std::string &filename); - -void read_map(std::map<uint64_t, std::string> &karta, const char* filename); - -} |