mirror of
https://github.com/moses-smt/mosesdecoder.git
synced 2024-09-11 11:25:40 +03:00
alternate weight settings: added option to turn off features completely
This commit is contained in:
parent
b0bf6a23b9
commit
081710ed4c
@ -142,6 +142,7 @@ int ChartHypothesis::RecombineCompare(const ChartHypothesis &compare) const
|
||||
*/
|
||||
void ChartHypothesis::CalcScore()
|
||||
{
|
||||
const StaticData &staticData = StaticData::Instance();
|
||||
// total scores from prev hypos
|
||||
std::vector<const ChartHypothesis*>::iterator iter;
|
||||
for (iter = m_prevHypos.begin(); iter != m_prevHypos.end(); ++iter) {
|
||||
@ -163,14 +164,18 @@ void ChartHypothesis::CalcScore()
|
||||
const std::vector<const StatelessFeatureFunction*>& sfs =
|
||||
StatelessFeatureFunction::GetStatelessFeatureFunctions();
|
||||
for (unsigned i = 0; i < sfs.size(); ++i) {
|
||||
sfs[i]->EvaluateChart(ChartBasedFeatureContext(this),&m_scoreBreakdown);
|
||||
if (! staticData.IsFeatureFunctionIgnored( *sfs[i] )) {
|
||||
sfs[i]->EvaluateChart(ChartBasedFeatureContext(this),&m_scoreBreakdown);
|
||||
}
|
||||
}
|
||||
|
||||
const std::vector<const StatefulFeatureFunction*>& ffs =
|
||||
StatefulFeatureFunction::GetStatefulFeatureFunctions();
|
||||
for (unsigned i = 0; i < ffs.size(); ++i)
|
||||
m_ffStates[i] = ffs[i]->EvaluateChart(*this,i,&m_scoreBreakdown);
|
||||
|
||||
for (unsigned i = 0; i < ffs.size(); ++i) {
|
||||
if (! staticData.IsFeatureFunctionIgnored( *ffs[i] )) {
|
||||
m_ffStates[i] = ffs[i]->EvaluateChart(*this,i,&m_scoreBreakdown);
|
||||
}
|
||||
}
|
||||
m_totalScore = m_scoreBreakdown.GetWeightedScore();
|
||||
}
|
||||
|
||||
|
@ -344,6 +344,7 @@ void ChartManager::CreateDeviantPaths(
|
||||
|
||||
void ChartManager::PreCalculateScores()
|
||||
{
|
||||
const StaticData &staticData = StaticData::Instance();
|
||||
for (size_t i = 0; i < m_translationOptionList.GetSize(); ++i) {
|
||||
const ChartTranslationOptions& cto = m_translationOptionList.Get(i);
|
||||
for (TargetPhraseCollection::const_iterator j = cto.GetTargetPhraseCollection().begin();
|
||||
@ -355,7 +356,9 @@ void ChartManager::PreCalculateScores()
|
||||
StatelessFeatureFunction::GetStatelessFeatureFunctions();
|
||||
ScoreComponentCollection& breakdown = m_precalculatedScores[*targetPhrase];
|
||||
for (size_t k = 0; k < sfs.size(); ++k) {
|
||||
sfs[k]->EvaluateChart(context,&breakdown);
|
||||
if (! staticData.IsFeatureFunctionIgnored( *sfs[k] )) {
|
||||
sfs[k]->EvaluateChart(context,&breakdown);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -56,13 +56,12 @@ Hypothesis::Hypothesis(Manager& manager, InputType const& source, const TargetPh
|
||||
m_sourceCompleted.GetFirstGapPos()>0 ? m_sourceCompleted.GetFirstGapPos()-1 : NOT_FOUND)
|
||||
, m_currTargetWordsRange(0, emptyTarget.GetSize()-1)
|
||||
, m_wordDeleted(false)
|
||||
, m_totalScore(0.0f)
|
||||
, m_futureScore(0.0f)
|
||||
, m_ffStates(StatefulFeatureFunction::GetStatefulFeatureFunctions().size())
|
||||
, m_arcList(NULL)
|
||||
, m_transOpt(NULL)
|
||||
, m_manager(manager)
|
||||
, m_totalScore(0.0f)
|
||||
, m_futureScore(0.0f)
|
||||
|
||||
, m_id(m_manager.GetNextHypoId())
|
||||
{
|
||||
// used for initial seeding of trans process
|
||||
@ -88,14 +87,14 @@ Hypothesis::Hypothesis(const Hypothesis &prevHypo, const TranslationOption &tran
|
||||
, m_currTargetWordsRange ( prevHypo.m_currTargetWordsRange.GetEndPos() + 1
|
||||
,prevHypo.m_currTargetWordsRange.GetEndPos() + transOpt.GetTargetPhrase().GetSize())
|
||||
, m_wordDeleted(false)
|
||||
, m_totalScore(0.0f)
|
||||
, m_futureScore(0.0f)
|
||||
, m_totalScore(0.0f)
|
||||
, m_futureScore(0.0f)
|
||||
, m_scoreBreakdown(prevHypo.GetScoreBreakdown())
|
||||
, m_ffStates(prevHypo.m_ffStates.size())
|
||||
, m_arcList(NULL)
|
||||
, m_transOpt(&transOpt)
|
||||
, m_manager(prevHypo.GetManager())
|
||||
, m_id(m_manager.GetNextHypoId())
|
||||
, m_scoreBreakdown(prevHypo.GetScoreBreakdown())
|
||||
{
|
||||
m_scoreBreakdown.PlusEquals(transOpt.GetScoreBreakdown());
|
||||
|
||||
@ -255,16 +254,21 @@ int Hypothesis::RecombineCompare(const Hypothesis &compare) const
|
||||
void Hypothesis::EvaluateWith(const StatefulFeatureFunction &sfff,
|
||||
int state_idx)
|
||||
{
|
||||
m_ffStates[state_idx] = sfff.Evaluate(
|
||||
const StaticData &staticData = StaticData::Instance();
|
||||
if (! staticData.IsFeatureFunctionIgnored( sfff )) {
|
||||
m_ffStates[state_idx] = sfff.Evaluate(
|
||||
*this,
|
||||
m_prevHypo ? m_prevHypo->m_ffStates[state_idx] : NULL,
|
||||
&m_scoreBreakdown);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void Hypothesis::EvaluateWith(const StatelessFeatureFunction& slff)
|
||||
{
|
||||
slff.Evaluate(PhraseBasedFeatureContext(this), &m_scoreBreakdown);
|
||||
const StaticData &staticData = StaticData::Instance();
|
||||
if (! staticData.IsFeatureFunctionIgnored( slff )) {
|
||||
slff.Evaluate(PhraseBasedFeatureContext(this), &m_scoreBreakdown);
|
||||
}
|
||||
}
|
||||
|
||||
/***
|
||||
@ -292,10 +296,12 @@ void Hypothesis::CalcScore(const SquareMatrix &futureScore)
|
||||
StatefulFeatureFunction::GetStatefulFeatureFunctions();
|
||||
for (unsigned i = 0; i < ffs.size(); ++i) {
|
||||
const StatefulFeatureFunction &ff = *ffs[i];
|
||||
m_ffStates[i] = ff.Evaluate(
|
||||
*this,
|
||||
m_prevHypo ? m_prevHypo->m_ffStates[i] : NULL,
|
||||
&m_scoreBreakdown);
|
||||
const StaticData &staticData = StaticData::Instance();
|
||||
if (! staticData.IsFeatureFunctionIgnored(ff)) {
|
||||
m_ffStates[i] = ff.Evaluate(*this,
|
||||
m_prevHypo ? m_prevHypo->m_ffStates[i] : NULL,
|
||||
&m_scoreBreakdown);
|
||||
}
|
||||
}
|
||||
|
||||
IFVERBOSE(2) {
|
||||
|
@ -84,10 +84,7 @@ void Manager::ProcessSentence()
|
||||
// check if alternate weight setting is used
|
||||
// this is not thread safe! it changes StaticData
|
||||
if (StaticData::Instance().GetHasAlternateWeightSettings()) {
|
||||
std::cerr << "config defines weight setting\n";
|
||||
if (m_source.GetSpecifiesWeightSetting()) {
|
||||
std::cerr << "sentence specifies weight setting\n";
|
||||
std::cerr << "calling SetWeightSetting( " << m_source.GetWeightSetting() << ")\n";
|
||||
StaticData::Instance().SetWeightSetting(m_source.GetWeightSetting());
|
||||
} else {
|
||||
StaticData::Instance().SetWeightSetting("default");
|
||||
|
@ -1248,9 +1248,27 @@ bool StaticData::LoadAlternateWeightSettings()
|
||||
// other specifications
|
||||
for(size_t j=1; j<tokens.size(); j++) {
|
||||
vector<string> args = Tokenize(tokens[j], "=");
|
||||
if (args[0] == "weight-file") {
|
||||
// TODO: support for sparse weights
|
||||
// TODO: support for sparse weights
|
||||
if (args[0] == "weight-file") {
|
||||
cerr << "ERROR: sparse weight files currently not supported";
|
||||
}
|
||||
// ignore feature functions
|
||||
else if (args[0] == "ignore-ff") {
|
||||
set< string > *ffNameSet = new set< string >;
|
||||
m_weightSettingIgnoreFF[ currentId ] = *ffNameSet;
|
||||
vector<string> featureFunctionName = Tokenize(args[1], " ");
|
||||
for(size_t k=0; k<featureFunctionName.size(); k++) {
|
||||
// check if a valid nane
|
||||
map<string,FeatureFunction*>::iterator ffLookUp = nameToFF.find(featureFunctionName[k]);
|
||||
if (ffLookUp == nameToFF.end()) {
|
||||
cerr << "ERROR: alternate weight setting " << currentId << " specifies to ignore feature function " << featureFunctionName[k] << " but there is no such feature function" << endl;
|
||||
hasErrors = true;
|
||||
}
|
||||
else {
|
||||
m_weightSettingIgnoreFF[ currentId ].insert( featureFunctionName[k] );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -211,10 +211,10 @@ protected:
|
||||
// alternate weight settings
|
||||
mutable std::string m_currentWeightSetting;
|
||||
std::map< std::string, ScoreComponentCollection* > m_weightSetting;
|
||||
std::map< std::string, std::set< std::string > > m_weightSettingIgnoreFF;
|
||||
|
||||
StaticData();
|
||||
|
||||
|
||||
void LoadChartDecodingParameters();
|
||||
void LoadNonTerminals();
|
||||
|
||||
@ -664,13 +664,26 @@ public:
|
||||
return m_weightSetting.size() > 0;
|
||||
}
|
||||
|
||||
bool IsFeatureFunctionIgnored( const FeatureFunction &ff ) const {
|
||||
if (!GetHasAlternateWeightSettings()) {
|
||||
return false;
|
||||
}
|
||||
std::map< std::string, std::set< std::string > >::const_iterator lookupIgnoreFF
|
||||
= m_weightSettingIgnoreFF.find( m_currentWeightSetting );
|
||||
if (lookupIgnoreFF == m_weightSettingIgnoreFF.end()) {
|
||||
return false;
|
||||
}
|
||||
const std::string &ffName = ff.GetScoreProducerDescription();
|
||||
const std::set< std::string > &ignoreFF = lookupIgnoreFF->second;
|
||||
return ignoreFF.count( ffName );
|
||||
}
|
||||
|
||||
/** process alternate weight settings
|
||||
* (specified with [alternate-weight-setting] in config file) */
|
||||
void SetWeightSetting(const std::string &settingName) const {
|
||||
|
||||
// if no change in weight setting, do nothing
|
||||
if (m_currentWeightSetting == settingName) {
|
||||
UserMessage::Add("no change in weight setting");
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -78,10 +78,10 @@ TargetPhrase::TargetPhrase(const TargetPhrase ©)
|
||||
: Phrase(copy)
|
||||
, m_fullScore(copy.m_fullScore)
|
||||
, m_futureScore(copy.m_futureScore)
|
||||
, m_scoreBreakdown(copy.m_scoreBreakdown)
|
||||
, m_sourcePhrase(copy.m_sourcePhrase)
|
||||
, m_alignTerm(copy.m_alignTerm)
|
||||
, m_alignNonTerm(copy.m_alignNonTerm)
|
||||
, m_scoreBreakdown(copy.m_scoreBreakdown)
|
||||
{
|
||||
if (copy.m_lhsTarget) {
|
||||
m_lhsTarget = new Word(copy.m_lhsTarget);
|
||||
@ -116,10 +116,13 @@ void TargetPhrase::Evaluate(const Phrase &source)
|
||||
void TargetPhrase::Evaluate(const Phrase &source, const std::vector<FeatureFunction*> &ffs)
|
||||
{
|
||||
if (ffs.size()) {
|
||||
const StaticData &staticData = StaticData::Instance();
|
||||
ScoreComponentCollection futureScoreBreakdown;
|
||||
for (size_t i = 0; i < ffs.size(); ++i) {
|
||||
const FeatureFunction &ff = *ffs[i];
|
||||
ff.Evaluate(source, *this, m_scoreBreakdown, futureScoreBreakdown);
|
||||
if (! staticData.IsFeatureFunctionIgnored( ff )) {
|
||||
ff.Evaluate(source, *this, m_scoreBreakdown, futureScoreBreakdown);
|
||||
}
|
||||
}
|
||||
|
||||
float weightedScore = m_scoreBreakdown.GetWeightedScore();
|
||||
|
Loading…
Reference in New Issue
Block a user