Implemented Load()

This commit is contained in:
Hieu Hoang 2015-12-15 13:36:44 +00:00
parent aa6182522e
commit b2f0139c23
7 changed files with 87 additions and 7 deletions

View File

@ -6,7 +6,11 @@
*/
#include "LexicalReordering.h"
#include "../System.h"
#include "../Search/Manager.h"
#include "../legacy/InputFileStream.h"
using namespace std;
namespace Moses2 {
@ -48,7 +52,19 @@ LexicalReordering::~LexicalReordering()
void LexicalReordering::Load(System &system)
{
InputFileStream file(m_path);
string line;
while(getline(file, line)) {
std::vector<std::string> toks = TokenizeMultiCharSeparator(line, "|||");
assert(toks.size() == 3);
PhraseImpl *source = PhraseImpl::CreateFromString(system.systemPool, system.GetVocab(), system, toks[0]);
PhraseImpl *target = PhraseImpl::CreateFromString(system.systemPool, system.GetVocab(), system, toks[1]);
std::vector<SCORE> scores = Tokenize<SCORE>(toks[2]);
Key key(source, target);
m_coll[key] = scores;
}
}
void LexicalReordering::SetParameter(const std::string& key, const std::string& value)

View File

@ -6,7 +6,11 @@
*/
#pragma once
#include <vector>
#include <boost/unordered_map.hpp>
#include "StatefulFeatureFunction.h"
#include "../TypeDef.h"
#include "../Phrase.h"
namespace Moses2 {
@ -39,6 +43,37 @@ public:
protected:
std::string m_path;
FactorList m_FactorsF;
FactorList m_FactorsE;
FactorList m_FactorsC;
typedef std::pair<PhraseImpl*, PhraseImpl*> Key;
typedef std::vector<SCORE> Values;
struct KeyComparer
{
size_t operator()(const Key &obj) const {
size_t seed = obj.first->hash();
boost::hash_combine(seed, obj.second->hash());
return seed;
}
bool operator()(const Key& a, const Key& b) const {
if ((*a.first) != (*b.first)) {
return false;
}
if ((*a.second) != (*b.second)) {
return false;
}
return true;
}
};
typedef boost::unordered_map<Key, Values, KeyComparer, KeyComparer> Coll;
Coll m_coll;
};
} /* namespace Moses2 */

View File

@ -29,6 +29,23 @@ size_t Phrase::hash() const
return seed;
}
bool Phrase::operator==(const Phrase &compare) const
{
if (GetSize() != compare.GetSize()) {
return false;
}
for (size_t i = 0; i < GetSize(); ++i) {
const Word &word = (*this)[i];
const Word &otherWord = compare[i];
if (word != otherWord) {
return false;
}
}
return true;
}
////////////////////////////////////////////////////////////////////////////////////////
PhraseImpl *PhraseImpl::CreateFromString(MemPool &pool, FactorCollection &vocab, const System &system, const std::string &str)
{

View File

@ -25,7 +25,13 @@ class Phrase
public:
virtual const Word& operator[](size_t pos) const = 0;
virtual size_t GetSize() const = 0;
size_t hash() const;
virtual size_t hash() const;
virtual bool operator==(const Phrase &compare) const;
virtual bool operator!=(const Phrase &compare) const
{
return !( (*this) == compare );
}
};
class PhraseImpl : public Phrase

View File

@ -7,12 +7,17 @@
#pragma once
#include <cstddef>
#include <vector>
namespace Moses2
{
class Hypothesis;
#define NOT_FOUND std::numeric_limits<size_t>::max()
const size_t DEFAULT_MAX_PHRASE_LENGTH = 20;
const size_t DEFAULT_MAX_HYPOSTACK_SIZE = 200;
const size_t DEFAULT_CUBE_PRUNING_POP_LIMIT = 1000;
const size_t DEFAULT_MAX_TRANS_OPT_CACHE_SIZE = 10000;
#ifndef BOS_
@ -22,7 +27,9 @@ const size_t DEFAULT_MAX_TRANS_OPT_CACHE_SIZE = 10000;
#define EOS_ "</s>" //End of sentence symbol
#endif
typedef size_t FactorType;
typedef float SCORE;
typedef std::vector<FactorType> FactorList;
// Note: StaticData uses SearchAlgorithm to determine whether the translation
// model is phrase-based or syntax-based. If you add a syntax-based search

View File

@ -34,6 +34,11 @@ public:
return cmp == 0;
}
bool operator!=(const Word &compare) const
{
return !( (*this) == compare );
}
bool operator<(const Word &compare) const;
const Factor* operator[](size_t ind) const {

View File

@ -14,12 +14,6 @@
namespace Moses2
{
#define NOT_FOUND std::numeric_limits<size_t>::max()
typedef size_t FactorType;
const size_t DEFAULT_MAX_PHRASE_LENGTH = 20;
const size_t DEFAULT_MAX_HYPOSTACK_SIZE = 200;
const size_t DEFAULT_CUBE_PRUNING_POP_LIMIT = 1000;
template<typename T>
class UnorderedComparer
{