Skip to content

Commit

Permalink
Code cleanup and refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
ugermann committed Dec 11, 2015
1 parent 240b88c commit 29694af
Show file tree
Hide file tree
Showing 144 changed files with 502 additions and 530 deletions.
2 changes: 1 addition & 1 deletion misc/queryPhraseTableMin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ int main(int argc, char **argv)
std::stringstream ss;
ss << nscores;
PhraseDictionaryCompact pdc("PhraseDictionaryCompact input-factor=0 output-factor=0 num-features=" + ss.str() + " path=" + ttable);
AllOptions opts;
AllOptions::ptr opts(new AllOptions);
pdc.Load(opts);

std::string line;
Expand Down
7 changes: 4 additions & 3 deletions moses-cmd/LatticeMBRGrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,12 @@ int main(int argc, char const* argv[])
}

StaticData& SD = const_cast<StaticData&>(StaticData::Instance());
LMBR_Options& lmbr = SD.options().lmbr;
MBR_Options& mbr = SD.options().mbr;
boost::shared_ptr<AllOptions> opts(new AllOptions(*SD.options()));
LMBR_Options& lmbr = opts->lmbr;
MBR_Options& mbr = opts->mbr;
lmbr.enabled = true;

boost::shared_ptr<IOWrapper> ioWrapper(new IOWrapper(SD.options()));
boost::shared_ptr<IOWrapper> ioWrapper(new IOWrapper(*opts));
if (!ioWrapper) {
throw runtime_error("Failed to initialise IOWrapper");
}
Expand Down
2 changes: 1 addition & 1 deletion moses-cmd/MainVW.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ int main(int argc, char const** argv)
IFVERBOSE(1) {
PrintUserTime("Created input-output object");
}
AllOptions::ptr opts(new AllOptions(StaticData::Instance().options()));
AllOptions::ptr opts(new AllOptions(*StaticData::Instance().options()));
boost::shared_ptr<IOWrapper> ioWrapper(new IOWrapper(*opts));
if (ioWrapper == NULL) {
cerr << "Error; Failed to create IO object" << endl;
Expand Down
5 changes: 3 additions & 2 deletions moses/BitmapContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class HypothesisScoreOrdererWithDistortion
, m_transOptRange(transOptRange) {
m_totalWeightDistortion = 0;
const StaticData &staticData = StaticData::Instance();

const std::vector<const DistortionScoreProducer*> &ffs = DistortionScoreProducer::GetDistortionFeatureFunctions();
std::vector<const DistortionScoreProducer*>::const_iterator iter;
for (iter = ffs.begin(); iter != ffs.end(); ++iter) {
Expand Down Expand Up @@ -139,7 +139,8 @@ BackwardsEdge::BackwardsEdge(const BitmapContainer &prevBitmapContainer
}

// Fetch the things we need for distortion cost computation.
int maxDistortion = StaticData::Instance().GetMaxDistortion();
// int maxDistortion = StaticData::Instance().GetMaxDistortion();
int maxDistortion = itype.options()->reordering.max_distortion;

if (maxDistortion == -1) {
for (HypothesisSet::const_iterator iter = m_prevBitmapContainer.GetHypotheses().begin(); iter != m_prevBitmapContainer.GetHypotheses().end(); ++iter) {
Expand Down
6 changes: 3 additions & 3 deletions moses/ChartHypothesis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ ChartHypothesis::~ChartHypothesis()
*/
void ChartHypothesis::GetOutputPhrase(Phrase &outPhrase) const
{
FactorType placeholderFactor = StaticData::Instance().options().input.placeholder_factor;
FactorType placeholderFactor = StaticData::Instance().options()->input.placeholder_factor;

for (size_t pos = 0; pos < GetCurrTargetPhrase().GetSize(); ++pos) {
const Word &word = GetCurrTargetPhrase().GetWord(pos);
Expand Down Expand Up @@ -256,7 +256,7 @@ void ChartHypothesis::CleanupArcList()
* However, may not be enough if only unique candidates are needed,
* so we'll keep all of arc list if nedd distinct n-best list
*/
AllOptions const& opts = StaticData::Instance().options();
AllOptions const& opts = *StaticData::Instance().options();
size_t nBestSize = opts.nbest.nbest_size;
bool distinctNBest = (opts.nbest.only_distinct
|| opts.mbr.enabled
Expand Down Expand Up @@ -336,7 +336,7 @@ std::ostream& operator<<(std::ostream& out, const ChartHypothesis& hypo)
out << "->" << hypo.GetWinningHypothesis()->GetId();
}

if (StaticData::Instance().GetIncludeLHSInSearchGraph()) {
if (hypo.GetManager().options()->output.include_lhs_in_search_graph) {
out << " " << hypo.GetTargetLHS() << "=>";
}
out << " " << hypo.GetCurrTargetPhrase()
Expand Down
6 changes: 3 additions & 3 deletions moses/ChartHypothesis.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class ChartHypothesis

boost::shared_ptr<ChartTranslationOption> m_transOpt;

Range m_currSourceWordsRange;
Range m_currSourceWordsRange;
std::vector<const FFState*> m_ffStates; /*! stateful feature function states */
/*! sum of scores of this hypothesis, and previous hypotheses. Lazily initialised. */
mutable boost::scoped_ptr<ScoreComponentCollection> m_scoreBreakdown;
Expand All @@ -62,8 +62,8 @@ class ChartHypothesis
,m_lmPrefix;
float m_totalScore;

ChartArcList *m_arcList; /*! all arcs that end at the same trellis point as this hypothesis */
const ChartHypothesis *m_winningHypo;
ChartArcList *m_arcList; /*! all arcs that end at the same trellis point as this hypothesis */
const ChartHypothesis *m_winningHypo;

std::vector<const ChartHypothesis*> m_prevHypos; // always sorted by source position?

Expand Down
2 changes: 1 addition & 1 deletion moses/ChartKBestExtractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void ChartKBestExtractor::Extract(
// Generate the target-side yield of the derivation d.
Phrase ChartKBestExtractor::GetOutputPhrase(const Derivation &d)
{
FactorType placeholderFactor = StaticData::Instance().options().input.placeholder_factor;
FactorType placeholderFactor = StaticData::Instance().options()->input.placeholder_factor;

Phrase ret(ARRAY_SIZE_INCR);

Expand Down
50 changes: 33 additions & 17 deletions moses/ChartParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,26 @@ ::ChartParserUnknown(ttasksptr const& ttask)
ChartParserUnknown::~ChartParserUnknown()
{
RemoveAllInColl(m_unksrcs);
// RemoveAllInColl(m_cacheTargetPhraseCollection);
}

void ChartParserUnknown::Process(const Word &sourceWord, const Range &range, ChartParserCallback &to)
AllOptions::ptr const&
ChartParserUnknown::
options() const
{
return m_ttask.lock()->options();
}

void
ChartParserUnknown::
Process(const Word &sourceWord, const Range &range, ChartParserCallback &to)
{
// unknown word, add as trans opt
const StaticData &staticData = StaticData::Instance();
const UnknownWordPenaltyProducer &unknownWordPenaltyProducer = UnknownWordPenaltyProducer::Instance();
const UnknownWordPenaltyProducer &unknownWordPenaltyProducer
= UnknownWordPenaltyProducer::Instance();

size_t isDigit = 0;
if (staticData.options().unk.drop) {
if (options()->unk.drop) {
const Factor *f = sourceWord[0]; // TODO hack. shouldn't know which factor is surface
const StringPiece s = f->GetString();
isDigit = s.find_first_of("0123456789");
Expand All @@ -79,9 +88,9 @@ void ChartParserUnknown::Process(const Word &sourceWord, const Range &range, Cha
}

//TranslationOption *transOpt;
if (! staticData.options().unk.drop || isDigit) {
if (! options()->unk.drop || isDigit) {
// loop
const UnknownLHSList &lhsList = staticData.GetUnknownLHS();
const UnknownLHSList &lhsList = options()->syntax.unknown_lhs; // staticData.GetUnknownLHS();
UnknownLHSList::const_iterator iterLHS;
for (iterLHS = lhsList.begin(); iterLHS != lhsList.end(); ++iterLHS) {
const string &targetLHSStr = iterLHS->first;
Expand All @@ -91,8 +100,8 @@ void ChartParserUnknown::Process(const Word &sourceWord, const Range &range, Cha
//const Word &sourceLHS = staticData.GetInputDefaultNonTerminal();
Word *targetLHS = new Word(true);

targetLHS->CreateFromString(Output, staticData.options().output.factor_order,
targetLHSStr, true);
targetLHS->CreateFromString(Output, options()->output.factor_order,
targetLHSStr, true);
UTIL_THROW_IF2(targetLHS->GetFactor(0) == NULL, "Null factor for target LHS");

// add to dictionary
Expand All @@ -108,9 +117,8 @@ void ChartParserUnknown::Process(const Word &sourceWord, const Range &range, Cha
targetPhrase->SetAlignmentInfo("0-0");
targetPhrase->EvaluateInIsolation(*unksrc);

AllOptions const& opts = staticData.options();
if (!opts.output.detailed_tree_transrep_filepath.empty() ||
opts.nbest.print_trees || staticData.GetTreeStructure() != NULL) {
if (!options()->output.detailed_tree_transrep_filepath.empty() ||
options()->nbest.print_trees || staticData.GetTreeStructure() != NULL) {
std::string prop = "[ ";
prop += (*targetLHS)[0]->GetString().as_string() + " ";
prop += sourceWord[0]->GetString().as_string() + " ]";
Expand All @@ -126,15 +134,15 @@ void ChartParserUnknown::Process(const Word &sourceWord, const Range &range, Cha

TargetPhrase *targetPhrase = new TargetPhrase(firstPt);
// loop
const UnknownLHSList &lhsList = staticData.GetUnknownLHS();
const UnknownLHSList &lhsList = options()->syntax.unknown_lhs;//staticData.GetUnknownLHS();
UnknownLHSList::const_iterator iterLHS;
for (iterLHS = lhsList.begin(); iterLHS != lhsList.end(); ++iterLHS) {
const string &targetLHSStr = iterLHS->first;
//float prob = iterLHS->second;

Word *targetLHS = new Word(true);
targetLHS->CreateFromString(Output, staticData.options().output.factor_order,
targetLHSStr, true);
targetLHS->CreateFromString(Output, staticData.options()->output.factor_order,
targetLHSStr, true);
UTIL_THROW_IF2(targetLHS->GetFactor(0) == NULL, "Null factor for target LHS");

targetPhrase->GetScoreBreakdown().Assign(&unknownWordPenaltyProducer, unknownScore);
Expand Down Expand Up @@ -214,9 +222,7 @@ void ChartParser::Create(const Range &range, ChartParserCallback &to)
if (range.GetNumWordsCovered() == 1
&& range.GetStartPos() != 0
&& range.GetStartPos() != m_source.GetSize()-1) {
bool always = m_ttask.lock()->options()->unk.always_create_direct_transopt;
// bool alwaysCreateDirectTranslationOption
// = StaticData::Instance().IsAlwaysCreateDirectTranslationOption();
bool always = options()->unk.always_create_direct_transopt;
if (to.Empty() || always) {
// create unknown words for 1 word coverage where we don't have any trans options
const Word &sourceWord = m_source.GetWord(range.GetStartPos());
Expand Down Expand Up @@ -291,4 +297,14 @@ long ChartParser::GetTranslationId() const
{
return m_source.GetTranslationId();
}


AllOptions::ptr const&
ChartParser::
options() const
{
return m_ttask.lock()->options();
}


} // namespace Moses
3 changes: 3 additions & 0 deletions moses/ChartParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class ChartParserUnknown
private:
std::vector<Phrase*> m_unksrcs;
std::list<TargetPhraseCollection::shared_ptr> m_cacheTargetPhraseCollection;
AllOptions::ptr const& options() const;
};

class ChartParser
Expand All @@ -78,6 +79,8 @@ class ChartParser
return m_unknown.GetUnknownSources();
}

AllOptions::ptr const& options() const;

private:
ChartParserUnknown m_unknown;
std::vector <DecodeGraph*> m_decodeGraphList;
Expand Down
25 changes: 11 additions & 14 deletions moses/ConfusionNet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,8 @@ ConfusionNet(AllOptions::ptr const& opts) : InputType(opts)
{
stats.createOne();

const StaticData& SD = StaticData::Instance();
if (SD.IsSyntax()) {
m_defaultLabelSet.insert(SD.GetInputDefaultNonTerminal());
if (is_syntax(opts->search.algo)) {
m_defaultLabelSet.insert(opts->syntax.input_default_non_terminal);
}
UTIL_THROW_IF2(InputFeature::InstancePtr() == NULL, "Input feature must be specified");
}
Expand All @@ -92,14 +91,14 @@ ConfusionNet(Sentence const& s) : InputType(s.options())

bool
ConfusionNet::
ReadF(std::istream& in, const std::vector<FactorType>& factorOrder, int format)
ReadF(std::istream& in, int format)
{
VERBOSE(2, "read confusion net with format "<<format<<"\n");
switch(format) {
case 0:
return ReadFormat0(in,factorOrder);
return ReadFormat0(in);
case 1:
return ReadFormat1(in,factorOrder);
return ReadFormat1(in);
default:
std::cerr << "ERROR: unknown format '"<<format
<<"' in ConfusionNet::Read";
Expand All @@ -109,22 +108,20 @@ ReadF(std::istream& in, const std::vector<FactorType>& factorOrder, int format)

int
ConfusionNet::
Read(std::istream& in,
const std::vector<FactorType>& factorOrder,
AllOptions const& opts)
Read(std::istream& in)
{
int rv=ReadF(in,factorOrder,0);
int rv=ReadF(in,0);
if(rv) stats.collect(*this);
return rv;
}

bool
ConfusionNet::
ReadFormat0(std::istream& in, const std::vector<FactorType>& factorOrder)
ReadFormat0(std::istream& in)
{
Clear();
const std::vector<FactorType>& factorOrder = m_options->input.factor_order;

// const StaticData &staticData = StaticData::Instance();
const InputFeature *inputFeature = InputFeature::InstancePtr();
size_t numInputScores = inputFeature->GetNumInputScores();
size_t numRealWordCount = inputFeature->GetNumRealWordsInInput();
Expand All @@ -140,7 +137,6 @@ ReadFormat0(std::istream& in, const std::vector<FactorType>& factorOrder)
Column col;
while(is>>word) {
Word w;
// String2Word(word,w,factorOrder);
w.CreateFromString(Input,factorOrder,StringPiece(word),false,false);
std::vector<float> probs(totalCount, 0.0);
for(size_t i=0; i < numInputScores; i++) {
Expand Down Expand Up @@ -179,9 +175,10 @@ ReadFormat0(std::istream& in, const std::vector<FactorType>& factorOrder)

bool
ConfusionNet::
ReadFormat1(std::istream& in, const std::vector<FactorType>& factorOrder)
ReadFormat1(std::istream& in)
{
Clear();
const std::vector<FactorType>& factorOrder = m_options->input.factor_order;
std::string line;
if(!getline(in,line)) return 0;
size_t s;
Expand Down
12 changes: 6 additions & 6 deletions moses/ConfusionNet.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class ConfusionNet : public InputType
std::vector<Column> data;
NonTerminalSet m_defaultLabelSet;

bool ReadFormat0(std::istream&,const std::vector<FactorType>& factorOrder);
bool ReadFormat1(std::istream&,const std::vector<FactorType>& factorOrder);
bool ReadFormat0(std::istream&);
bool ReadFormat1(std::istream&);
void String2Word(const std::string& s,Word& w,const std::vector<FactorType>& factorOrder);

public:
Expand All @@ -46,7 +46,8 @@ class ConfusionNet : public InputType

const Column& GetColumn(size_t i) const {
UTIL_THROW_IF2(i >= data.size(),
"Out of bounds. Trying to access " << i << " when vector only contains " << data.size());
"Out of bounds. Trying to access " << i
<< " when vector only contains " << data.size());
return data[i];
}
const Column& operator[](size_t i) const {
Expand All @@ -64,11 +65,10 @@ class ConfusionNet : public InputType
data.clear();
}

bool ReadF(std::istream&,const std::vector<FactorType>& factorOrder,int format=0);
bool ReadF(std::istream&, int format=0);
virtual void Print(std::ostream&) const;

int Read(std::istream& in,const std::vector<FactorType>& factorOrder,
AllOptions const& opts);
int Read(std::istream& in);

Phrase GetSubString(const Range&) const; //TODO not defined
std::string GetStringRep(const std::vector<FactorType> factorsToPrint) const; //TODO not defined
Expand Down
4 changes: 2 additions & 2 deletions moses/ExportInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ SimpleTranslationInterface::~SimpleTranslationInterface()
//the simplified version of string input/output translation
string SimpleTranslationInterface::translate(const string &inputString)
{
boost::shared_ptr<Moses::IOWrapper> ioWrapper(new IOWrapper(StaticData::Instance().options()));
boost::shared_ptr<Moses::IOWrapper> ioWrapper(new IOWrapper(*StaticData::Instance().options()));
// main loop over set of input sentences
size_t sentEnd = inputString.rfind('\n'); //find the last \n, the input stream has to be appended with \n to be translated
const string &newString = sentEnd != string::npos ? inputString : inputString + '\n';
Expand Down Expand Up @@ -180,7 +180,7 @@ batch_run()
IFVERBOSE(1) PrintUserTime("Created input-output object");

// set up read/writing class:
boost::shared_ptr<IOWrapper> ioWrapper(new IOWrapper(staticData.options()));
boost::shared_ptr<IOWrapper> ioWrapper(new IOWrapper(*staticData.options()));
UTIL_THROW_IF2(ioWrapper == NULL, "Error; Failed to create IO object"
<< " [" << HERE << "]");

Expand Down
9 changes: 5 additions & 4 deletions moses/FF/ConstrainedDecoding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ ConstrainedDecoding::ConstrainedDecoding(const std::string &line)
ReadParameters();
}

void ConstrainedDecoding::Load(AllOptions const& opts)
void ConstrainedDecoding::Load(AllOptions::ptr const& opts)
{
m_options = opts;
const StaticData &staticData = StaticData::Instance();
bool addBeginEndWord
= ((opts.search.algo == CYKPlus) || (opts.search.algo == ChartIncremental));
= ((opts->search.algo == CYKPlus) || (opts->search.algo == ChartIncremental));

for(size_t i = 0; i < m_paths.size(); ++i) {
InputFileStream constraintFile(m_paths[i]);
Expand All @@ -62,10 +63,10 @@ void ConstrainedDecoding::Load(AllOptions const& opts)
Phrase phrase(0);
if (vecStr.size() == 1) {
sentenceID++;
phrase.CreateFromString(Output, opts.output.factor_order, vecStr[0], NULL);
phrase.CreateFromString(Output, opts->output.factor_order, vecStr[0], NULL);
} else if (vecStr.size() == 2) {
sentenceID = Scan<long>(vecStr[0]);
phrase.CreateFromString(Output, opts.output.factor_order, vecStr[1], NULL);
phrase.CreateFromString(Output, opts->output.factor_order, vecStr[1], NULL);
} else {
UTIL_THROW(util::Exception, "Reference file not loaded");
}
Expand Down
Loading

0 comments on commit 29694af

Please sign in to comment.