2008-06-11 14:52:57 +04:00
|
|
|
// $Id$
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
Moses - factored phrase-based language decoder
|
|
|
|
Copyright (C) 2006 University of Edinburgh
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
***********************************************************************/
|
|
|
|
|
|
|
|
#include <algorithm>
|
2012-10-28 21:47:22 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include "util/check.hh"
|
|
|
|
#include "util/exception.hh"
|
2011-10-14 20:40:30 +04:00
|
|
|
#include "util/tokenize_piece.hh"
|
|
|
|
|
2008-06-11 14:52:57 +04:00
|
|
|
#include "TargetPhrase.h"
|
|
|
|
#include "GenerationDictionary.h"
|
2011-10-13 18:27:01 +04:00
|
|
|
#include "LM/Base.h"
|
2008-06-11 14:52:57 +04:00
|
|
|
#include "StaticData.h"
|
|
|
|
#include "ScoreComponentCollection.h"
|
|
|
|
#include "Util.h"
|
2011-06-17 01:20:20 +04:00
|
|
|
#include "AlignmentInfoCollection.h"
|
2012-04-10 18:45:18 +04:00
|
|
|
|
2008-06-11 14:52:57 +04:00
|
|
|
using namespace std;
|
|
|
|
|
2008-10-09 03:51:26 +04:00
|
|
|
namespace Moses
|
|
|
|
{
|
2011-11-21 14:49:26 +04:00
|
|
|
TargetPhrase::TargetPhrase( std::string out_string)
|
2013-05-30 20:16:10 +04:00
|
|
|
:Phrase(0)
|
|
|
|
, m_fullScore(0.0)
|
|
|
|
, m_futureScore(0.0)
|
2013-05-29 21:16:15 +04:00
|
|
|
, m_alignTerm(&AlignmentInfoCollection::Instance().GetEmptyAlignmentInfo())
|
|
|
|
, m_alignNonTerm(&AlignmentInfoCollection::Instance().GetEmptyAlignmentInfo())
|
|
|
|
, m_lhsTarget(NULL)
|
2010-05-04 01:39:23 +04:00
|
|
|
{
|
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
//ACAT
|
|
|
|
const StaticData &staticData = StaticData::Instance();
|
2013-05-22 15:03:00 +04:00
|
|
|
CreateFromString(Output, staticData.GetInputFactorOrder(), out_string, staticData.GetFactorDelimiter(), NULL);
|
2010-05-04 01:39:23 +04:00
|
|
|
}
|
|
|
|
|
2011-11-21 14:49:26 +04:00
|
|
|
TargetPhrase::TargetPhrase()
|
2013-05-29 21:16:15 +04:00
|
|
|
:Phrase()
|
|
|
|
, m_fullScore(0.0)
|
2013-05-30 20:16:10 +04:00
|
|
|
, m_futureScore(0.0)
|
2013-05-29 21:16:15 +04:00
|
|
|
, m_alignTerm(&AlignmentInfoCollection::Instance().GetEmptyAlignmentInfo())
|
|
|
|
, m_alignNonTerm(&AlignmentInfoCollection::Instance().GetEmptyAlignmentInfo())
|
|
|
|
, m_lhsTarget(NULL)
|
2013-05-22 13:57:19 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TargetPhrase::TargetPhrase(const Phrase &phrase)
|
2013-05-29 21:16:15 +04:00
|
|
|
: Phrase(phrase)
|
|
|
|
, m_fullScore(0.0)
|
2013-05-30 20:16:10 +04:00
|
|
|
, m_futureScore(0.0)
|
2013-05-29 21:16:15 +04:00
|
|
|
, m_alignTerm(&AlignmentInfoCollection::Instance().GetEmptyAlignmentInfo())
|
|
|
|
, m_alignNonTerm(&AlignmentInfoCollection::Instance().GetEmptyAlignmentInfo())
|
|
|
|
, m_lhsTarget(NULL)
|
2008-06-11 14:52:57 +04:00
|
|
|
{
|
|
|
|
}
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2013-05-22 13:40:12 +04:00
|
|
|
TargetPhrase::TargetPhrase(const TargetPhrase ©)
|
2013-05-29 21:16:15 +04:00
|
|
|
: Phrase(copy)
|
|
|
|
, m_fullScore(copy.m_fullScore)
|
2013-05-30 20:16:10 +04:00
|
|
|
, m_futureScore(copy.m_futureScore)
|
2013-06-05 22:44:43 +04:00
|
|
|
, m_scoreBreakdown(copy.m_scoreBreakdown)
|
2013-05-29 21:16:15 +04:00
|
|
|
, m_alignTerm(copy.m_alignTerm)
|
|
|
|
, m_alignNonTerm(copy.m_alignNonTerm)
|
2013-05-22 13:40:12 +04:00
|
|
|
{
|
2013-05-22 13:57:19 +04:00
|
|
|
if (copy.m_lhsTarget) {
|
2013-07-24 18:55:17 +04:00
|
|
|
m_lhsTarget = new Word(*copy.m_lhsTarget);
|
2013-05-29 21:16:15 +04:00
|
|
|
} else {
|
2013-05-22 13:57:19 +04:00
|
|
|
m_lhsTarget = NULL;
|
|
|
|
}
|
2013-05-22 13:40:12 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-22 13:57:19 +04:00
|
|
|
TargetPhrase::~TargetPhrase()
|
2008-06-11 14:52:57 +04:00
|
|
|
{
|
2013-05-22 20:32:06 +04:00
|
|
|
//cerr << "m_lhsTarget=" << m_lhsTarget << endl;
|
|
|
|
|
2013-05-22 13:57:19 +04:00
|
|
|
delete m_lhsTarget;
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2008-09-24 20:48:23 +04:00
|
|
|
#ifdef HAVE_PROTOBUF
|
2011-02-24 16:14:42 +03:00
|
|
|
void TargetPhrase::WriteToRulePB(hgmert::Rule* pb) const
|
|
|
|
{
|
|
|
|
pb->add_trg_words("[X,1]");
|
|
|
|
for (size_t pos = 0 ; pos < GetSize() ; pos++)
|
|
|
|
pb->add_trg_words(GetWord(pos)[0]->GetString());
|
2008-09-24 20:48:23 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-05-28 03:41:25 +04:00
|
|
|
void TargetPhrase::Evaluate(const Phrase &source)
|
2013-05-02 15:15:26 +04:00
|
|
|
{
|
|
|
|
const std::vector<FeatureFunction*> &ffs = FeatureFunction::GetFeatureFunctions();
|
2013-05-30 15:51:40 +04:00
|
|
|
Evaluate(source, ffs);
|
|
|
|
}
|
2013-05-02 15:15:26 +04:00
|
|
|
|
2013-05-30 15:51:40 +04:00
|
|
|
void TargetPhrase::Evaluate(const Phrase &source, const std::vector<FeatureFunction*> &ffs)
|
|
|
|
{
|
2013-05-30 20:16:10 +04:00
|
|
|
if (ffs.size()) {
|
2013-06-05 22:44:43 +04:00
|
|
|
const StaticData &staticData = StaticData::Instance();
|
2013-05-30 20:16:10 +04:00
|
|
|
ScoreComponentCollection futureScoreBreakdown;
|
|
|
|
for (size_t i = 0; i < ffs.size(); ++i) {
|
|
|
|
const FeatureFunction &ff = *ffs[i];
|
2013-06-05 22:44:43 +04:00
|
|
|
if (! staticData.IsFeatureFunctionIgnored( ff )) {
|
2013-06-06 18:51:31 +04:00
|
|
|
ff.Evaluate(source, *this, m_scoreBreakdown, futureScoreBreakdown);
|
2013-06-05 22:44:43 +04:00
|
|
|
}
|
2013-05-30 20:16:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
float weightedScore = m_scoreBreakdown.GetWeightedScore();
|
2013-05-31 02:59:29 +04:00
|
|
|
m_futureScore += futureScoreBreakdown.GetWeightedScore();
|
|
|
|
m_fullScore = weightedScore + m_futureScore;
|
2013-05-08 14:28:31 +04:00
|
|
|
|
2013-05-30 20:16:10 +04:00
|
|
|
}
|
2013-05-02 15:15:26 +04:00
|
|
|
}
|
2008-09-12 22:09:06 +04:00
|
|
|
|
2013-08-08 20:35:42 +04:00
|
|
|
void TargetPhrase::Evaluate(const InputType &input, const Phrase &sourcePhrase)
|
2013-05-23 21:12:08 +04:00
|
|
|
{
|
|
|
|
const std::vector<FeatureFunction*> &ffs = FeatureFunction::GetFeatureFunctions();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < ffs.size(); ++i) {
|
2013-05-29 21:16:15 +04:00
|
|
|
const FeatureFunction &ff = *ffs[i];
|
2013-08-08 20:35:42 +04:00
|
|
|
ff.Evaluate(input, sourcePhrase, m_scoreBreakdown);
|
2013-05-23 21:12:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-02 20:44:19 +04:00
|
|
|
void TargetPhrase::SetXMLScore(float score)
|
2008-06-11 14:52:57 +04:00
|
|
|
{
|
2012-12-21 18:54:43 +04:00
|
|
|
const StaticData &staticData = StaticData::Instance();
|
2013-05-02 20:44:19 +04:00
|
|
|
const FeatureFunction* prod = staticData.GetPhraseDictionaries()[0];
|
|
|
|
size_t numScores = prod->GetNumScoreComponents();
|
|
|
|
vector <float> scoreVector(numScores,score/numScores);
|
2013-05-13 18:36:09 +04:00
|
|
|
|
|
|
|
m_scoreBreakdown.Assign(prod, scoreVector);
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
2013-05-02 21:21:52 +04:00
|
|
|
void TargetPhrase::SetInputScore(const Scores &scoreVector)
|
2009-02-05 20:37:09 +03:00
|
|
|
{
|
2013-05-02 21:21:52 +04:00
|
|
|
//we use an existing score producer to figure out information for score setting (number of scores and weights)
|
2012-12-21 18:54:43 +04:00
|
|
|
const StaticData &staticData = StaticData::Instance();
|
2013-02-22 00:03:35 +04:00
|
|
|
const FeatureFunction* prod = staticData.GetPhraseDictionaries()[0];
|
2009-02-05 20:37:09 +03:00
|
|
|
|
2013-05-02 21:21:52 +04:00
|
|
|
//expand the input weight vector
|
|
|
|
CHECK(scoreVector.size() <= prod->GetNumScoreComponents());
|
|
|
|
Scores sizedScoreVector = scoreVector;
|
|
|
|
sizedScoreVector.resize(prod->GetNumScoreComponents(),0.0f);
|
2009-02-05 20:37:09 +03:00
|
|
|
|
2013-05-13 18:36:09 +04:00
|
|
|
m_scoreBreakdown.Assign(prod, sizedScoreVector);
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2011-10-14 20:40:30 +04:00
|
|
|
void TargetPhrase::SetAlignmentInfo(const StringPiece &alignString)
|
2010-07-18 02:29:06 +04:00
|
|
|
{
|
2013-05-29 21:16:15 +04:00
|
|
|
AlignmentInfo::CollType alignTerm, alignNonTerm;
|
2011-10-14 20:40:30 +04:00
|
|
|
for (util::TokenIter<util::AnyCharacter, true> token(alignString, util::AnyCharacter(" \t")); token; ++token) {
|
2012-10-28 21:47:22 +04:00
|
|
|
util::TokenIter<util::SingleCharacter, false> dash(*token, util::SingleCharacter('-'));
|
|
|
|
|
|
|
|
char *endptr;
|
|
|
|
size_t sourcePos = strtoul(dash->data(), &endptr, 10);
|
|
|
|
UTIL_THROW_IF(endptr != dash->data() + dash->size(), util::ErrnoException, "Error parsing alignment" << *dash);
|
|
|
|
++dash;
|
|
|
|
size_t targetPos = strtoul(dash->data(), &endptr, 10);
|
|
|
|
UTIL_THROW_IF(endptr != dash->data() + dash->size(), util::ErrnoException, "Error parsing alignment" << *dash);
|
|
|
|
UTIL_THROW_IF(++dash, util::Exception, "Extra gunk in alignment " << *token);
|
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2012-10-19 18:10:10 +04:00
|
|
|
if (GetWord(targetPos).IsNonTerminal()) {
|
2013-05-29 21:16:15 +04:00
|
|
|
alignNonTerm.insert(std::pair<size_t,size_t>(sourcePos, targetPos));
|
|
|
|
} else {
|
|
|
|
alignTerm.insert(std::pair<size_t,size_t>(sourcePos, targetPos));
|
2012-10-19 18:10:10 +04:00
|
|
|
}
|
2012-04-19 22:08:06 +04:00
|
|
|
}
|
2012-10-19 18:10:10 +04:00
|
|
|
SetAlignTerm(alignTerm);
|
|
|
|
SetAlignNonTerm(alignNonTerm);
|
2012-04-19 22:08:06 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-10-19 18:10:10 +04:00
|
|
|
void TargetPhrase::SetAlignTerm(const AlignmentInfo::CollType &coll)
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
2013-05-29 21:16:15 +04:00
|
|
|
const AlignmentInfo *alignmentInfo = AlignmentInfoCollection::Instance().Add(coll);
|
|
|
|
m_alignTerm = alignmentInfo;
|
2012-10-19 19:00:42 +04:00
|
|
|
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2012-10-19 18:10:10 +04:00
|
|
|
void TargetPhrase::SetAlignNonTerm(const AlignmentInfo::CollType &coll)
|
2012-04-19 22:08:06 +04:00
|
|
|
{
|
2013-05-29 21:16:15 +04:00
|
|
|
const AlignmentInfo *alignmentInfo = AlignmentInfoCollection::Instance().Add(coll);
|
|
|
|
m_alignNonTerm = alignmentInfo;
|
2012-04-19 22:08:06 +04:00
|
|
|
}
|
2008-09-12 22:09:06 +04:00
|
|
|
|
2013-04-30 20:27:35 +04:00
|
|
|
void TargetPhrase::SetSparseScore(const FeatureFunction* translationScoreProducer, const StringPiece &sparseString)
|
|
|
|
{
|
|
|
|
m_scoreBreakdown.Assign(translationScoreProducer, sparseString.as_string());
|
|
|
|
}
|
|
|
|
|
2013-05-31 00:12:36 +04:00
|
|
|
void TargetPhrase::Merge(const TargetPhrase ©, const std::vector<FactorType>& factorVec)
|
|
|
|
{
|
|
|
|
Phrase::MergeFactors(copy, factorVec);
|
|
|
|
m_scoreBreakdown.Merge(copy.GetScoreBreakdown());
|
2013-05-31 02:59:29 +04:00
|
|
|
m_futureScore += copy.m_futureScore;
|
|
|
|
m_fullScore += copy.m_fullScore;
|
2013-05-31 00:12:36 +04:00
|
|
|
}
|
|
|
|
|
2008-06-11 14:52:57 +04:00
|
|
|
TO_STRING_BODY(TargetPhrase);
|
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream& os, const TargetPhrase& tp)
|
|
|
|
{
|
2013-07-24 18:55:17 +04:00
|
|
|
if (tp.m_lhsTarget) {
|
|
|
|
os << *tp.m_lhsTarget<< " -> ";
|
|
|
|
}
|
2013-04-10 21:40:25 +04:00
|
|
|
os << static_cast<const Phrase&>(tp) << ":" << flush;
|
|
|
|
os << tp.GetAlignNonTerm() << flush;
|
|
|
|
os << ": c=" << tp.m_fullScore << flush;
|
|
|
|
os << " " << tp.m_scoreBreakdown << flush;
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2008-06-11 14:52:57 +04:00
|
|
|
return os;
|
|
|
|
}
|
2008-10-09 03:51:26 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|