diff options
author | Phil Williams <philip.williams@mac.com> | 2015-03-10 14:07:04 +0300 |
---|---|---|
committer | Phil Williams <philip.williams@mac.com> | 2015-03-10 14:07:04 +0300 |
commit | e8a7163f0d45c88f11f70b14d0c8a8706794b8e1 (patch) | |
tree | fc9cfe60307baf9254b0c391a42631e861282d13 /moses/Syntax | |
parent | 137b07a4868ca407bceee104fdd9e7fdb39e4e67 (diff) |
Add SyntaxInputWeight feature function
Currently only used for forest input.
Diffstat (limited to 'moses/Syntax')
-rw-r--r-- | moses/Syntax/Cube.cpp | 2 | ||||
-rw-r--r-- | moses/Syntax/F2S/PHyperedgeToSHyperedgeBundle.h | 1 | ||||
-rw-r--r-- | moses/Syntax/F2S/RuleMatcherHyperTree-inl.h | 10 | ||||
-rw-r--r-- | moses/Syntax/InputWeightFF.cpp | 47 | ||||
-rw-r--r-- | moses/Syntax/InputWeightFF.h | 49 | ||||
-rw-r--r-- | moses/Syntax/PLabel.h | 1 | ||||
-rw-r--r-- | moses/Syntax/SHyperedgeBundle.h | 2 | ||||
-rw-r--r-- | moses/Syntax/SLabel.h | 1 |
8 files changed, 113 insertions, 0 deletions
diff --git a/moses/Syntax/Cube.cpp b/moses/Syntax/Cube.cpp index f5ff73cfa..0249291d3 100644 --- a/moses/Syntax/Cube.cpp +++ b/moses/Syntax/Cube.cpp @@ -107,6 +107,8 @@ SHyperedge *Cube::CreateHyperedge(const std::vector<int> &coordinates) } } + hyperedge->label.inputWeight = m_bundle.inputWeight; + hyperedge->label.translation = *(m_bundle.translations->begin()+coordinates.back()); diff --git a/moses/Syntax/F2S/PHyperedgeToSHyperedgeBundle.h b/moses/Syntax/F2S/PHyperedgeToSHyperedgeBundle.h index 250b00fc0..e551dc955 100644 --- a/moses/Syntax/F2S/PHyperedgeToSHyperedgeBundle.h +++ b/moses/Syntax/F2S/PHyperedgeToSHyperedgeBundle.h @@ -18,6 +18,7 @@ inline void PHyperedgeToSHyperedgeBundle(const PHyperedge &hyperedge, const PVertexToStackMap &stackMap, SHyperedgeBundle &bundle) { + bundle.inputWeight = hyperedge.label.inputWeight; bundle.translations = hyperedge.label.translations; bundle.stacks.clear(); for (std::vector<PVertex*>::const_iterator p = hyperedge.tail.begin(); diff --git a/moses/Syntax/F2S/RuleMatcherHyperTree-inl.h b/moses/Syntax/F2S/RuleMatcherHyperTree-inl.h index e07239825..ba444350a 100644 --- a/moses/Syntax/F2S/RuleMatcherHyperTree-inl.h +++ b/moses/Syntax/F2S/RuleMatcherHyperTree-inl.h @@ -37,13 +37,23 @@ void RuleMatcherHyperTree<Callback>::EnumerateHyperedges( m_queue.pop(); if (item.trieNode->HasRules()) { const FNS &fns = item.annotatedFNS.fns; + // Set the output hyperedge's tail. m_hyperedge.tail.clear(); for (FNS::const_iterator p = fns.begin(); p != fns.end(); ++p) { const Forest::Vertex *v = *p; m_hyperedge.tail.push_back(const_cast<PVertex *>(&(v->pvertex))); } + // Set the output hyperedge label's input weight. + m_hyperedge.label.inputWeight = 0.0f; + for (std::vector<const Forest::Hyperedge *>::const_iterator + p = item.annotatedFNS.fragment.begin(); + p != item.annotatedFNS.fragment.end(); ++p) { + m_hyperedge.label.inputWeight += (*p)->weight; + } + // Set the output hyperedge label's translation set pointer. m_hyperedge.label.translations = &(item.trieNode->GetTargetPhraseCollection()); + // Pass the output hyperedge to the callback. callback(m_hyperedge); } PropagateNextLexel(item); diff --git a/moses/Syntax/InputWeightFF.cpp b/moses/Syntax/InputWeightFF.cpp new file mode 100644 index 000000000..af44e31ec --- /dev/null +++ b/moses/Syntax/InputWeightFF.cpp @@ -0,0 +1,47 @@ +#include "InputWeightFF.h" + +#include <vector> + +#include "moses/ScoreComponentCollection.h" +#include "moses/TargetPhrase.h" + +namespace Moses +{ +namespace Syntax +{ + +InputWeightFF::InputWeightFF(const std::string &line) + : StatelessFeatureFunction(1, line) +{ + ReadParameters(); +} + +void InputWeightFF::EvaluateWhenApplied(const Hypothesis& hypo, + ScoreComponentCollection* accumulator) const +{ + // TODO Throw exception. + assert(false); +} + +void InputWeightFF::EvaluateWhenApplied(const ChartHypothesis &hypo, + ScoreComponentCollection* accumulator) const +{ + // TODO Throw exception. + assert(false); +} + +void InputWeightFF::EvaluateWhenApplied( + const Syntax::SHyperedge &hyperedge, + ScoreComponentCollection* accumulator) const +{ + accumulator->PlusEquals(this, hyperedge.label.inputWeight); +} + +void InputWeightFF::SetParameter(const std::string& key, + const std::string& value) +{ + StatelessFeatureFunction::SetParameter(key, value); +} + +} // Syntax +} // Moses diff --git a/moses/Syntax/InputWeightFF.h b/moses/Syntax/InputWeightFF.h new file mode 100644 index 000000000..bdda1d922 --- /dev/null +++ b/moses/Syntax/InputWeightFF.h @@ -0,0 +1,49 @@ +#pragma once + +#include <string> + +#include "moses/FF/StatelessFeatureFunction.h" + +namespace Moses +{ +namespace Syntax +{ + +class InputWeightFF : public StatelessFeatureFunction +{ +public: + InputWeightFF(const std::string &line); + + bool IsUseable(const FactorMask &mask) const { + return true; + } + + void EvaluateWhenApplied(const Hypothesis& hypo, + ScoreComponentCollection* accumulator) const; + + void EvaluateWhenApplied(const ChartHypothesis &hypo, + ScoreComponentCollection* accumulator) const; + + void EvaluateWhenApplied(const Syntax::SHyperedge &hyperedge, + ScoreComponentCollection* accumulator) const; + + + void SetParameter(const std::string& key, const std::string& value); + + // Virtual functions from StatelessFeatureFunction that are no-ops for + // InputWeightFF. + void EvaluateInIsolation(const Phrase &, const TargetPhrase &, + ScoreComponentCollection &, + ScoreComponentCollection &) const {} + + void EvaluateWithSourceContext(const InputType &, const InputPath &, + const TargetPhrase &, const StackVec *, + ScoreComponentCollection &, + ScoreComponentCollection *) const {} + + void EvaluateTranslationOptionListWithSourceContext( + const InputType &, const TranslationOptionList &) const {} +}; + +} // Syntax +} // Moses diff --git a/moses/Syntax/PLabel.h b/moses/Syntax/PLabel.h index a1c3dcce0..4537b86bb 100644 --- a/moses/Syntax/PLabel.h +++ b/moses/Syntax/PLabel.h @@ -8,6 +8,7 @@ namespace Syntax { struct PLabel { + float inputWeight; const TargetPhraseCollection *translations; }; diff --git a/moses/Syntax/SHyperedgeBundle.h b/moses/Syntax/SHyperedgeBundle.h index f4a07a181..54eda73bc 100644 --- a/moses/Syntax/SHyperedgeBundle.h +++ b/moses/Syntax/SHyperedgeBundle.h @@ -15,11 +15,13 @@ namespace Syntax struct PVertex; struct SHyperedgeBundle { + float inputWeight; std::vector<const SVertexStack*> stacks; const TargetPhraseCollection *translations; friend void swap(SHyperedgeBundle &x, SHyperedgeBundle &y) { using std::swap; + swap(x.inputWeight, y.inputWeight); swap(x.stacks, y.stacks); swap(x.translations, y.translations); } diff --git a/moses/Syntax/SLabel.h b/moses/Syntax/SLabel.h index 0b87cb76e..6a25082dc 100644 --- a/moses/Syntax/SLabel.h +++ b/moses/Syntax/SLabel.h @@ -9,6 +9,7 @@ namespace Syntax { struct SLabel { + float inputWeight; float score; ScoreComponentCollection scoreBreakdown; const TargetPhrase *translation; |