mirror of
https://github.com/moses-smt/mosesdecoder.git
synced 2024-09-11 19:27:11 +03:00
Code cleanup.
This commit is contained in:
parent
889a33dcd6
commit
69e5a1cc15
@ -205,10 +205,7 @@ int main(int argc, char const* argv[])
|
||||
<< r << " " << size_t(prune_i) << " " << scale_i
|
||||
<< " ||| ";
|
||||
vector<Word> mbrBestHypo = doLatticeMBR(manager,nBestList);
|
||||
manager.OutputBestHypo(mbrBestHypo, lineCount,
|
||||
manager.options().output.ReportSegmentation,
|
||||
manager.options().output.ReportAllFactors,
|
||||
cout);
|
||||
manager.OutputBestHypo(mbrBestHypo, cout);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -332,57 +332,22 @@ GetTargetPhraseStringRep() const
|
||||
return GetTargetPhraseStringRep(allFactors);
|
||||
}
|
||||
|
||||
void
|
||||
size_t
|
||||
Hypothesis::
|
||||
OutputAlignment(std::ostream &out, WordAlignmentSort sortOrder) const
|
||||
OutputAlignment(std::ostream &out, bool recursive=true) const
|
||||
{
|
||||
std::vector<const Hypothesis *> edges;
|
||||
const Hypothesis *currentHypo = this;
|
||||
while (currentHypo) {
|
||||
edges.push_back(currentHypo);
|
||||
currentHypo = currentHypo->GetPrevHypo();
|
||||
}
|
||||
WordAlignmentSort const& waso = m_manager.options().output.WA_SortOrder;
|
||||
TargetPhrase const& tp = GetCurrTargetPhrase();
|
||||
|
||||
OutputAlignment(out, edges, sortOrder);
|
||||
|
||||
}
|
||||
|
||||
void
|
||||
Hypothesis::
|
||||
OutputAlignment(ostream &out,
|
||||
vector<const Hypothesis *> const& edges,
|
||||
WordAlignmentSort waso)
|
||||
{
|
||||
size_t targetOffset = 0;
|
||||
|
||||
for (int currEdge = (int)edges.size() - 1 ; currEdge >= 0 ; currEdge--) {
|
||||
const Hypothesis &edge = *edges[currEdge];
|
||||
const TargetPhrase &tp = edge.GetCurrTargetPhrase();
|
||||
size_t sourceOffset = edge.GetCurrSourceWordsRange().GetStartPos();
|
||||
|
||||
OutputAlignment(out, tp.GetAlignTerm(), sourceOffset, targetOffset, waso);
|
||||
|
||||
targetOffset += tp.GetSize();
|
||||
}
|
||||
// Used by --print-alignment-info, so no endl
|
||||
}
|
||||
|
||||
void
|
||||
Hypothesis::
|
||||
OutputAlignment(ostream &out, const AlignmentInfo &ai,
|
||||
size_t sourceOffset, size_t targetOffset,
|
||||
WordAlignmentSort waso)
|
||||
{
|
||||
typedef std::vector< const std::pair<size_t,size_t>* > AlignVec;
|
||||
AlignVec alignments = ai.GetSortedAlignments(waso);
|
||||
|
||||
AlignVec::const_iterator it;
|
||||
for (it = alignments.begin(); it != alignments.end(); ++it) {
|
||||
const std::pair<size_t,size_t> &alignment = **it;
|
||||
out << alignment.first + sourceOffset << "-"
|
||||
<< alignment.second + targetOffset << " ";
|
||||
}
|
||||
// call with head recursion to output things in the right order
|
||||
size_t trg_off = recursive && m_prevHypo ? m_prevHypo->OutputAlignment(out) : 0;
|
||||
size_t src_off = GetCurrSourceWordsRange().GetStartPos();
|
||||
|
||||
typedef std::pair<size_t,size_t> const* entry;
|
||||
std::vector<entry> alnvec = tp.GetAlignTerm().GetSortedAlignments(waso);
|
||||
BOOST_FOREACH(entry e, alnvec)
|
||||
out << e->first + src_off << "-" << e->second + trg_off << " ";
|
||||
return trg_off + tp.GetSize();
|
||||
}
|
||||
|
||||
void
|
||||
@ -489,8 +454,9 @@ OutputSurface(std::ostream &out, const Hypothesis &edge,
|
||||
out << "|" << sourceStart << "-" << sourceEnd; // enriched "-tt"
|
||||
if (options.ReportSegmentation == 2) {
|
||||
out << ",wa=";
|
||||
const AlignmentInfo &ai = edge.GetCurrTargetPhrase().GetAlignTerm();
|
||||
Hypothesis::OutputAlignment(out, ai, 0, 0, options.WA_SortOrder);
|
||||
edge.OutputAlignment(out, false);
|
||||
// const AlignmentInfo &ai = edge.GetCurrTargetPhrase().GetAlignTerm();
|
||||
// Hypothesis::OutputAlignment(out, ai, 0, 0, options.WA_SortOrder);
|
||||
out << ",total=";
|
||||
out << edge.GetScore() - edge.GetPrevHypo()->GetScore();
|
||||
out << ",";
|
||||
|
@ -209,8 +209,8 @@ public:
|
||||
return m_arcList;
|
||||
}
|
||||
const ScoreComponentCollection& GetScoreBreakdown() const {
|
||||
if (!m_scoreBreakdown.get()) {
|
||||
m_scoreBreakdown.reset(new ScoreComponentCollection());
|
||||
if (!m_scoreBreakdown) {
|
||||
m_scoreBreakdown.reset(new ScoreComponentCollection);
|
||||
m_scoreBreakdown->PlusEquals(m_currScoreBreakdown);
|
||||
if (m_prevHypo) {
|
||||
m_scoreBreakdown->PlusEquals(m_prevHypo->GetScoreBreakdown());
|
||||
@ -239,18 +239,7 @@ public:
|
||||
return m_transOpt;
|
||||
}
|
||||
|
||||
void
|
||||
OutputAlignment(std::ostream &out, WordAlignmentSort sortOrder) const;
|
||||
|
||||
static void
|
||||
OutputAlignment(std::ostream &out,
|
||||
const std::vector<const Hypothesis *> &edges,
|
||||
WordAlignmentSort waso);
|
||||
|
||||
static void
|
||||
OutputAlignment(std::ostream &out, const Moses::AlignmentInfo &ai,
|
||||
size_t sourceOffset, size_t targetOffset,
|
||||
WordAlignmentSort waso);
|
||||
size_t OutputAlignment(std::ostream &out, bool recursive) const;
|
||||
|
||||
void OutputInput(std::ostream& os) const;
|
||||
static void OutputInput(std::vector<const Phrase*>& map, const Hypothesis* hypo);
|
||||
|
@ -246,7 +246,7 @@ printThisHypothesis(long translationId, const Hypothesis* hypo,
|
||||
* \param count the number of n-best translations to produce
|
||||
* \param ret holds the n-best list that was calculated
|
||||
*/
|
||||
void Manager::CalcNBest(size_t count, TrellisPathList &ret,bool onlyDistinct) const
|
||||
void Manager::CalcNBest(size_t count, TrellisPathList &ret, bool onlyDistinct) const
|
||||
{
|
||||
if (count <= 0)
|
||||
return;
|
||||
@ -1572,7 +1572,7 @@ void Manager::OutputBest(OutputCollector *collector) const
|
||||
exit(1);
|
||||
}
|
||||
TrellisPathList nBestList;
|
||||
CalcNBest(nBestSize, nBestList,true);
|
||||
CalcNBest(nBestSize, nBestList, true);
|
||||
VERBOSE(2,"size of n-best: " << nBestList.GetSize() << " (" << nBestSize << ")" << endl);
|
||||
IFVERBOSE(2) {
|
||||
PrintUserTime("calculated n-best list for (L)MBR decoding");
|
||||
@ -1589,9 +1589,7 @@ void Manager::OutputBest(OutputCollector *collector) const
|
||||
} else {
|
||||
//Lattice MBR decoding
|
||||
vector<Word> mbrBestHypo = doLatticeMBR(*this,nBestList);
|
||||
OutputBestHypo(mbrBestHypo, translationId,
|
||||
options().output.ReportSegmentation,
|
||||
options().output.ReportAllFactors, out);
|
||||
OutputBestHypo(mbrBestHypo, out);
|
||||
IFVERBOSE(2) {
|
||||
PrintUserTime("finished Lattice MBR decoding");
|
||||
}
|
||||
@ -1601,9 +1599,7 @@ void Manager::OutputBest(OutputCollector *collector) const
|
||||
// consensus decoding
|
||||
else if (options().search.consensus) {
|
||||
const TrellisPath &conBestHypo = doConsensusDecoding(*this,nBestList);
|
||||
OutputBestHypo(conBestHypo, translationId,
|
||||
options().output.ReportSegmentation,
|
||||
options().output.ReportAllFactors, out);
|
||||
OutputBestHypo(conBestHypo, out);
|
||||
OutputAlignment(m_alignmentOut, conBestHypo);
|
||||
IFVERBOSE(2) {
|
||||
PrintUserTime("finished Consensus decoding");
|
||||
@ -1613,9 +1609,7 @@ void Manager::OutputBest(OutputCollector *collector) const
|
||||
// n-best MBR decoding
|
||||
else {
|
||||
const TrellisPath &mbrBestHypo = doMBR(nBestList);
|
||||
OutputBestHypo(mbrBestHypo, translationId,
|
||||
options().output.ReportSegmentation,
|
||||
options().output.ReportAllFactors, out);
|
||||
OutputBestHypo(mbrBestHypo, out);
|
||||
OutputAlignment(m_alignmentOut, mbrBestHypo);
|
||||
IFVERBOSE(2) {
|
||||
PrintUserTime("finished MBR decoding");
|
||||
@ -1638,22 +1632,16 @@ void Manager::OutputNBest(OutputCollector *collector) const
|
||||
return;
|
||||
}
|
||||
|
||||
const StaticData &staticData = StaticData::Instance();
|
||||
long translationId = m_source.GetTranslationId();
|
||||
|
||||
if (options().lmbr.enabled) {
|
||||
if (options().nbest.enabled) {
|
||||
collector->Write(translationId, m_latticeNBestOut.str());
|
||||
collector->Write(m_source.GetTranslationId(), m_latticeNBestOut.str());
|
||||
}
|
||||
} else {
|
||||
TrellisPathList nBestList;
|
||||
ostringstream out;
|
||||
CalcNBest(options().nbest.nbest_size, nBestList,
|
||||
options().nbest.only_distinct);
|
||||
OutputNBest(out, nBestList,
|
||||
staticData.GetOutputFactorOrder(),
|
||||
m_source.GetTranslationId(),
|
||||
options().output.ReportSegmentation);
|
||||
NBestOptions const& nbo = options().nbest;
|
||||
CalcNBest(nbo.nbest_size, nBestList, nbo.only_distinct);
|
||||
OutputNBest(out, nBestList);
|
||||
collector->Write(m_source.GetTranslationId(), out.str());
|
||||
}
|
||||
|
||||
@ -1661,12 +1649,8 @@ void Manager::OutputNBest(OutputCollector *collector) const
|
||||
|
||||
void
|
||||
Manager::
|
||||
OutputNBest(std::ostream& out,
|
||||
const Moses::TrellisPathList &nBestList,
|
||||
const std::vector<Moses::FactorType>& outputFactorOrder,
|
||||
long translationId, char reportSegmentation) const
|
||||
OutputNBest(std::ostream& out, Moses::TrellisPathList const& nBestList) const
|
||||
{
|
||||
// const StaticData &staticData = StaticData::Instance();
|
||||
NBestOptions const& nbo = options().nbest;
|
||||
bool reportAllFactors = nbo.include_all_factors;
|
||||
bool includeSegmentation = nbo.include_segmentation;
|
||||
@ -1678,10 +1662,10 @@ OutputNBest(std::ostream& out,
|
||||
const std::vector<const Hypothesis *> &edges = path.GetEdges();
|
||||
|
||||
// print the surface factor of the translation
|
||||
out << translationId << " ||| ";
|
||||
out << m_source.GetTranslationId() << " ||| ";
|
||||
for (int currEdge = (int)edges.size() - 1 ; currEdge >= 0 ; currEdge--) {
|
||||
const Hypothesis &edge = *edges[currEdge];
|
||||
OutputSurface(out, edge); //, outputFactorOrder, reportSegmentation, reportAllFactors);
|
||||
OutputSurface(out, edge);
|
||||
}
|
||||
out << " |||";
|
||||
|
||||
@ -1862,7 +1846,8 @@ std::map<size_t, const Factor*> Manager::GetPlaceholders(const Hypothesis &hypo,
|
||||
for (size_t sourcePos = 0; sourcePos < inputPhrase.GetSize(); ++sourcePos) {
|
||||
const Factor *factor = inputPhrase.GetFactor(sourcePos, placeholderFactor);
|
||||
if (factor) {
|
||||
std::set<size_t> targetPos = hypo.GetTranslationOption().GetTargetPhrase().GetAlignTerm().GetAlignmentsForSource(sourcePos);
|
||||
TargetPhrase const& tp = hypo.GetTranslationOption().GetTargetPhrase();
|
||||
std::set<size_t> targetPos = tp.GetAlignTerm().GetAlignmentsForSource(sourcePos);
|
||||
UTIL_THROW_IF2(targetPos.size() != 1,
|
||||
"Placeholder should be aligned to 1, and only 1, word");
|
||||
ret[*targetPos.begin()] = factor;
|
||||
@ -1879,10 +1864,7 @@ void Manager::OutputLatticeSamples(OutputCollector *collector) const
|
||||
TrellisPathList latticeSamples;
|
||||
ostringstream out;
|
||||
CalcLatticeSamples(staticData.GetLatticeSamplesSize(), latticeSamples);
|
||||
OutputNBest(out,latticeSamples,
|
||||
staticData.GetOutputFactorOrder(),
|
||||
m_source.GetTranslationId(),
|
||||
options().output.ReportSegmentation);
|
||||
OutputNBest(out,latticeSamples);
|
||||
collector->Write(m_source.GetTranslationId(), out.str());
|
||||
}
|
||||
|
||||
@ -1903,37 +1885,23 @@ void Manager::OutputAlignment(OutputCollector *collector) const
|
||||
edges.push_back(currentHypo);
|
||||
currentHypo = currentHypo->GetPrevHypo();
|
||||
}
|
||||
|
||||
OutputAlignment(collector,m_source.GetTranslationId(), edges);
|
||||
}
|
||||
}
|
||||
|
||||
void Manager::OutputAlignment(OutputCollector* collector, size_t lineNo , const vector<const Hypothesis *> &edges) const
|
||||
{
|
||||
ostringstream out;
|
||||
OutputAlignment(out, edges);
|
||||
|
||||
collector->Write(lineNo,out.str());
|
||||
}
|
||||
|
||||
void Manager::OutputAlignment(ostream &out, const vector<const Hypothesis *> &edges) const
|
||||
{
|
||||
size_t targetOffset = 0;
|
||||
|
||||
for (int currEdge = (int)edges.size() - 1 ; currEdge >= 0 ; currEdge--) {
|
||||
const Hypothesis &edge = *edges[currEdge];
|
||||
const TargetPhrase &tp = edge.GetCurrTargetPhrase();
|
||||
size_t sourceOffset = edge.GetCurrSourceWordsRange().GetStartPos();
|
||||
|
||||
BOOST_REVERSE_FOREACH(Hypothesis const* e, edges) {
|
||||
const TargetPhrase &tp = e->GetCurrTargetPhrase();
|
||||
size_t sourceOffset = e->GetCurrSourceWordsRange().GetStartPos();
|
||||
OutputAlignment(out, tp.GetAlignTerm(), sourceOffset, targetOffset);
|
||||
|
||||
targetOffset += tp.GetSize();
|
||||
}
|
||||
// Used by --alignment-output-file so requires endl
|
||||
out << std::endl;
|
||||
out << std::endl; // Used by --alignment-output-file so requires endl
|
||||
collector->Write(m_source.GetTranslationId(), out.str());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void Manager::OutputDetailedTranslationReport(OutputCollector *collector) const
|
||||
void
|
||||
Manager::
|
||||
OutputDetailedTranslationReport(OutputCollector *collector) const
|
||||
{
|
||||
if (collector) {
|
||||
ostringstream out;
|
||||
@ -1944,7 +1912,9 @@ void Manager::OutputDetailedTranslationReport(OutputCollector *collector) const
|
||||
|
||||
}
|
||||
|
||||
void Manager::OutputUnknowns(OutputCollector *collector) const
|
||||
void
|
||||
Manager::
|
||||
OutputUnknowns(OutputCollector *collector) const
|
||||
{
|
||||
if (collector) {
|
||||
long translationId = m_source.GetTranslationId();
|
||||
@ -1959,7 +1929,9 @@ void Manager::OutputUnknowns(OutputCollector *collector) const
|
||||
|
||||
}
|
||||
|
||||
void Manager::OutputWordGraph(OutputCollector *collector) const
|
||||
void
|
||||
Manager::
|
||||
OutputWordGraph(OutputCollector *collector) const
|
||||
{
|
||||
if (collector) {
|
||||
long translationId = m_source.GetTranslationId();
|
||||
@ -1970,7 +1942,9 @@ void Manager::OutputWordGraph(OutputCollector *collector) const
|
||||
}
|
||||
}
|
||||
|
||||
void Manager::OutputSearchGraph(OutputCollector *collector) const
|
||||
void
|
||||
Manager::
|
||||
OutputSearchGraph(OutputCollector *collector) const
|
||||
{
|
||||
if (collector) {
|
||||
long translationId = m_source.GetTranslationId();
|
||||
@ -2058,31 +2032,28 @@ void Manager::OutputLatticeMBRNBest(std::ostream& out, const vector<LatticeMBRSo
|
||||
}
|
||||
}
|
||||
|
||||
void Manager::OutputBestHypo(const std::vector<Word>& mbrBestHypo, long /*translationId*/, char /*reportSegmentation*/, bool /*reportAllFactors*/, ostream& out) const
|
||||
void
|
||||
Manager::
|
||||
OutputBestHypo(const std::vector<Word>& mbrBestHypo, ostream& out) const
|
||||
{
|
||||
|
||||
FactorType f = options().output.factor_order[0];
|
||||
for (size_t i = 0 ; i < mbrBestHypo.size() ; i++) {
|
||||
const Factor *factor = mbrBestHypo[i].GetFactor(StaticData::Instance().GetOutputFactorOrder()[0]);
|
||||
UTIL_THROW_IF2(factor == NULL,
|
||||
"No factor 0 at position " << i);
|
||||
if (i>0) out << " " << *factor;
|
||||
else out << *factor;
|
||||
const Factor *factor = mbrBestHypo[i].GetFactor(f);
|
||||
UTIL_THROW_IF2(factor == NULL, "No factor " << f << " at position " << i);
|
||||
if (i) out << " ";
|
||||
out << *factor;
|
||||
}
|
||||
out << endl;
|
||||
}
|
||||
|
||||
void
|
||||
Manager::
|
||||
OutputBestHypo(const Moses::TrellisPath &path, long /*translationId*/,
|
||||
char reportSegmentation, bool reportAllFactors,
|
||||
std::ostream &out) const
|
||||
OutputBestHypo(const Moses::TrellisPath &path, std::ostream &out) const
|
||||
{
|
||||
const std::vector<const Hypothesis *> &edges = path.GetEdges();
|
||||
|
||||
std::vector<const Hypothesis *> const& edges = path.GetEdges();
|
||||
for (int currEdge = (int)edges.size() - 1 ; currEdge >= 0 ; currEdge--) {
|
||||
const Hypothesis &edge = *edges[currEdge];
|
||||
Hypothesis const& edge = *edges[currEdge];
|
||||
OutputSurface(out, edge);
|
||||
// , StaticData::Instance().GetOutputFactorOrder(), reportSegmentation, reportAllFactors);
|
||||
}
|
||||
out << endl;
|
||||
}
|
||||
@ -2092,7 +2063,9 @@ Manager::
|
||||
OutputAlignment(std::ostringstream &out, const TrellisPath &path) const
|
||||
{
|
||||
WordAlignmentSort waso = options().output.WA_SortOrder;
|
||||
Hypothesis::OutputAlignment(out, path.GetEdges(), waso);
|
||||
BOOST_REVERSE_FOREACH(Hypothesis const* e, path.GetEdges())
|
||||
e->OutputAlignment(out, false);
|
||||
// Hypothesis::OutputAlignment(out, path.GetEdges(), waso);
|
||||
// Used by --alignment-output-file so requires endl
|
||||
out << std::endl;
|
||||
}
|
||||
|
@ -132,21 +132,16 @@ protected:
|
||||
mutable std::ostringstream m_latticeNBestOut;
|
||||
mutable std::ostringstream m_alignmentOut;
|
||||
public:
|
||||
void OutputNBest(std::ostream& out
|
||||
, const Moses::TrellisPathList &nBestList
|
||||
, const std::vector<Moses::FactorType>& outputFactorOrder
|
||||
, long translationId
|
||||
, char reportSegmentation) const;
|
||||
|
||||
void OutputNBest(std::ostream& out, const Moses::TrellisPathList &nBestList) const;
|
||||
void OutputSurface(std::ostream &out, const Hypothesis &edge) const;
|
||||
|
||||
void OutputAlignment(std::ostream &out, const AlignmentInfo &ai, size_t sourceOffset, size_t targetOffset) const;
|
||||
void OutputInput(std::ostream& os, const Hypothesis* hypo) const;
|
||||
void OutputInput(std::vector<const Phrase*>& map, const Hypothesis* hypo) const;
|
||||
void OutputPassthroughInformation(std::ostream& os, const Hypothesis* hypo) const;
|
||||
std::map<size_t, const Factor*> GetPlaceholders(const Hypothesis &hypo, FactorType placeholderFactor) const;
|
||||
void OutputAlignment(OutputCollector* collector, size_t lineNo , const std::vector<const Hypothesis *> &edges) const;
|
||||
void OutputAlignment(std::ostream &out, const std::vector<const Hypothesis *> &edges) const;
|
||||
|
||||
std::map<size_t, const Factor*>
|
||||
GetPlaceholders(const Hypothesis &hypo, FactorType placeholderFactor) const;
|
||||
|
||||
void OutputWordGraph(std::ostream &outputWordGraphStream, const Hypothesis *hypo, size_t &linkId) const;
|
||||
|
||||
@ -171,9 +166,8 @@ public:
|
||||
int GetNextHypoId();
|
||||
|
||||
void OutputLatticeMBRNBest(std::ostream& out, const std::vector<LatticeMBRSolution>& solutions,long translationId) const;
|
||||
void OutputBestHypo(const std::vector<Moses::Word>& mbrBestHypo, long /*translationId*/,
|
||||
char reportSegmentation, bool reportAllFactors, std::ostream& out) const;
|
||||
void OutputBestHypo(const Moses::TrellisPath &path, long /*translationId*/,char reportSegmentation, bool reportAllFactors, std::ostream &out) const;
|
||||
void OutputBestHypo(const std::vector<Moses::Word>& mbrBestHypo, std::ostream& out) const;
|
||||
void OutputBestHypo(const Moses::TrellisPath &path, std::ostream &out) const;
|
||||
|
||||
#ifdef HAVE_PROTOBUF
|
||||
void SerializeSearchGraphPB(long translationId, std::ostream& outputStream) const;
|
||||
@ -196,8 +190,12 @@ public:
|
||||
/***
|
||||
*For Lattice MBR
|
||||
*/
|
||||
void GetForwardBackwardSearchGraph(std::map< int, bool >* pConnected,
|
||||
std::vector< const Hypothesis* >* pConnectedList, std::map < const Hypothesis*, std::set < const Hypothesis* > >* pOutgoingHyps, std::vector< float>* pFwdBwdScores) const;
|
||||
void
|
||||
GetForwardBackwardSearchGraph
|
||||
( std::map< int, bool >* pConnected,
|
||||
std::vector< const Hypothesis* >* pConnectedList,
|
||||
std::map < const Hypothesis*, std::set < const Hypothesis* > >* pOutgoingHyps,
|
||||
std::vector< float>* pFwdBwdScores) const;
|
||||
|
||||
// outputs
|
||||
void OutputBest(OutputCollector *collector) const;
|
||||
|
@ -31,19 +31,6 @@ GetContextWindow() const
|
||||
return m_context;
|
||||
}
|
||||
|
||||
// SPTR<std::map<std::string, float> const>
|
||||
// TranslationTask::GetContextWeights() const
|
||||
// {
|
||||
// return m_context_weights;
|
||||
// }
|
||||
|
||||
// void
|
||||
// TranslationTask
|
||||
// ::ReSetContextWeights(std::map<std::string, float> const& new_weights)
|
||||
// {
|
||||
// m_context_weights.reset(new std::map<string,float>(new_weights));
|
||||
// }
|
||||
|
||||
void
|
||||
TranslationTask::
|
||||
SetContextWindow(boost::shared_ptr<std::vector<std::string> > const& cw)
|
||||
|
@ -164,21 +164,27 @@ void TrellisPath::CreateDeviantPaths(TrellisPathList &pathColl) const
|
||||
}
|
||||
}
|
||||
|
||||
const boost::shared_ptr<ScoreComponentCollection> TrellisPath::GetScoreBreakdown() const
|
||||
boost::shared_ptr<ScoreComponentCollection> const
|
||||
TrellisPath::
|
||||
GetScoreBreakdown() const
|
||||
{
|
||||
if (!m_scoreBreakdown) {
|
||||
float totalScore = m_path[0]->GetWinningHypo()->GetFutureScore(); // calculated for sanity check only
|
||||
float totalScore = m_path[0]->GetWinningHypo()->GetFutureScore();
|
||||
// calculated for sanity check only
|
||||
|
||||
m_scoreBreakdown = boost::shared_ptr<ScoreComponentCollection>(new ScoreComponentCollection());
|
||||
m_scoreBreakdown->PlusEquals(ScoreComponentCollection(m_path[0]->GetWinningHypo()->GetScoreBreakdown()));
|
||||
m_scoreBreakdown.reset(new ScoreComponentCollection());
|
||||
m_scoreBreakdown->PlusEquals(m_path[0]->GetWinningHypo()->GetScoreBreakdown());
|
||||
|
||||
//calc score
|
||||
// adjust score
|
||||
// I assume things are done this way on the assumption that most hypothesis edges
|
||||
// are shared with the winning path, so that score adjustments are cheaper than
|
||||
// recomputing the score from scratch. UG
|
||||
size_t sizePath = m_path.size();
|
||||
for (size_t pos = 0 ; pos < sizePath ; pos++) {
|
||||
const Hypothesis *hypo = m_path[pos];
|
||||
const Hypothesis *winningHypo = hypo->GetWinningHypo();
|
||||
if (hypo != winningHypo) {
|
||||
totalScore = totalScore - winningHypo->GetFutureScore() + hypo->GetFutureScore();
|
||||
totalScore += hypo->GetFutureScore() - winningHypo->GetFutureScore();
|
||||
m_scoreBreakdown->MinusEquals(winningHypo->GetScoreBreakdown());
|
||||
m_scoreBreakdown->PlusEquals(hypo->GetScoreBreakdown());
|
||||
}
|
||||
@ -208,16 +214,14 @@ Phrase TrellisPath::GetTargetPhrase() const
|
||||
|
||||
Phrase TrellisPath::GetSurfacePhrase() const
|
||||
{
|
||||
const std::vector<FactorType> &outputFactor
|
||||
= manager().options().output.factor_order;
|
||||
// = StaticData::Instance().GetOutputFactorOrder();
|
||||
std::vector<FactorType> const& oFactor = manager().options().output.factor_order;
|
||||
Phrase targetPhrase = GetTargetPhrase();
|
||||
Phrase ret(targetPhrase.GetSize());
|
||||
|
||||
for (size_t pos = 0 ; pos < targetPhrase.GetSize() ; ++pos) {
|
||||
Word &newWord = ret.AddWord();
|
||||
for (size_t i = 0 ; i < outputFactor.size() ; i++) {
|
||||
FactorType factorType = outputFactor[i];
|
||||
for (size_t i = 0 ; i < oFactor.size() ; i++) {
|
||||
FactorType factorType = oFactor[i];
|
||||
const Factor *factor = targetPhrase.GetFactor(pos, factorType);
|
||||
UTIL_THROW_IF2(factor == NULL,
|
||||
"No factor " << factorType << " at position " << pos);
|
||||
|
@ -88,8 +88,9 @@ namespace Moses {
|
||||
|
||||
std::map<std::string, xmlrpc_c::value>::const_iterator m;
|
||||
m = param.find("output-factors");
|
||||
if (m != param.end())
|
||||
factor_order = Tokenize<FactorType>(xmlrpc_c::value_string(m->second), ",");
|
||||
if (m != param.end()) {
|
||||
factor_order=Tokenize<FactorType>(xmlrpc_c::value_string(m->second),",");
|
||||
}
|
||||
|
||||
if (ReportAllFactors) {
|
||||
factor_order.clear();
|
||||
@ -97,10 +98,21 @@ namespace Moses {
|
||||
factor_order.push_back(i);
|
||||
}
|
||||
|
||||
m = param.find("align");
|
||||
if (m != param.end() && Scan<bool>(xmlrpc_c::value_string(m->second)))
|
||||
ReportSegmentation = 1;
|
||||
|
||||
PrintAlignmentInfo = check(param,"word-align",PrintAlignmentInfo);
|
||||
|
||||
m = param.find("factor-delimiter");
|
||||
if (m != param.end()) FactorDelimiter = Trim(xmlrpc_c::value_string(m->second));
|
||||
if (m != param.end()) {
|
||||
FactorDelimiter = Trim(xmlrpc_c::value_string(m->second));
|
||||
}
|
||||
|
||||
m = param.find("output-factor-delimiter");
|
||||
if (m != param.end()) FactorDelimiter = Trim(xmlrpc_c::value_string(m->second));
|
||||
if (m != param.end()) {
|
||||
FactorDelimiter = Trim(xmlrpc_c::value_string(m->second));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -62,7 +62,6 @@ Run()
|
||||
Session const& S = m_translator->get_session(m_session_id);
|
||||
m_scope = S.scope;
|
||||
m_session_id = S.id;
|
||||
// cerr << "SESSION ID" << m_session_id << endl;
|
||||
}
|
||||
else m_scope.reset(new Moses::ContextScope);
|
||||
|
||||
@ -72,18 +71,11 @@ Run()
|
||||
|
||||
Moses::StaticData const& SD = Moses::StaticData::Instance();
|
||||
|
||||
//Make sure alternative paths are retained, if necessary
|
||||
// if (m_withGraphInfo || m_nbestSize>0)
|
||||
// why on earth is this a global variable? Is this even thread-safe???? UG
|
||||
// (const_cast<Moses::StaticData&>(SD)).SetOutputSearchGraph(true);
|
||||
// std::stringstream out, graphInfo, transCollOpts;
|
||||
|
||||
if (SD.IsSyntax())
|
||||
run_chart_decoder();
|
||||
else
|
||||
run_phrase_decoder();
|
||||
|
||||
// XVERBOSE(1,"Output: " << out.str() << endl);
|
||||
{
|
||||
boost::lock_guard<boost::mutex> lock(m_mutex);
|
||||
m_done = true;
|
||||
@ -166,30 +158,16 @@ insertGraphInfo(Manager& manager, map<string, xmlrpc_c::value>& retData)
|
||||
retData["sg"] = xmlrpc_c::value_array(searchGraphXml);
|
||||
}
|
||||
|
||||
// void
|
||||
// TranslationRequest::
|
||||
// output_phrase(ostream& out, Phrase const& phrase) const
|
||||
// {
|
||||
// if (!m_options.output.ReportAllFactors) {
|
||||
// for (size_t i = 0 ; i < phrase.GetSize(); ++i)
|
||||
// out << *phrase.GetFactor(i, 0) << " ";
|
||||
// } else out << phrase;
|
||||
// }
|
||||
|
||||
void
|
||||
TranslationRequest::
|
||||
outputNBest(const Manager& manager, map<string, xmlrpc_c::value>& retData)
|
||||
{
|
||||
TrellisPathList nBestList;
|
||||
vector<xmlrpc_c::value> nBestXml;
|
||||
manager.CalcNBest(m_options.nbest.nbest_size, nBestList,
|
||||
m_options.nbest.only_distinct);
|
||||
|
||||
StaticData const& SD = StaticData::Instance();
|
||||
manager.OutputNBest(cout, nBestList,
|
||||
SD.GetOutputFactorOrder(),
|
||||
m_source->GetTranslationId(),
|
||||
options().output.ReportSegmentation);
|
||||
Moses::NBestOptions const& nbo = m_options.nbest;
|
||||
manager.CalcNBest(nbo.nbest_size, nBestList, nbo.only_distinct);
|
||||
manager.OutputNBest(cout, nBestList);
|
||||
|
||||
BOOST_FOREACH(Moses::TrellisPath const* path, nBestList) {
|
||||
vector<const Hypothesis *> const& E = path->GetEdges();
|
||||
@ -199,10 +177,9 @@ outputNBest(const Manager& manager, map<string, xmlrpc_c::value>& retData)
|
||||
if (m_withScoreBreakdown) {
|
||||
// should the score breakdown be reported in a more structured manner?
|
||||
ostringstream buf;
|
||||
bool with_labels = m_options.nbest.include_feature_labels;
|
||||
bool with_labels = nbo.include_feature_labels;
|
||||
path->GetScoreBreakdown()->OutputAllFeatureScores(buf, with_labels);
|
||||
nBestXmlItem["fvals"] = xmlrpc_c::value_string(buf.str());
|
||||
|
||||
nBestXmlItem["scores"] = PackScores(*path->GetScoreBreakdown());
|
||||
}
|
||||
|
||||
@ -218,20 +195,18 @@ TranslationRequest::
|
||||
insertTranslationOptions(Moses::Manager& manager,
|
||||
std::map<std::string, xmlrpc_c::value>& retData)
|
||||
{
|
||||
const TranslationOptionCollection* toptsColl
|
||||
= manager.getSntTranslationOptions();
|
||||
std::vector<Moses::FactorType> const& ofactor_order = options().output.factor_order;
|
||||
|
||||
const TranslationOptionCollection* toptsColl = manager.getSntTranslationOptions();
|
||||
vector<xmlrpc_c::value> toptsXml;
|
||||
size_t const stop = toptsColl->GetSource().GetSize();
|
||||
TranslationOptionList const* tol;
|
||||
for (size_t s = 0 ; s < stop ; ++s) {
|
||||
for (size_t e = s;
|
||||
(tol = toptsColl->GetTranslationOptionList(s,e)) != NULL;
|
||||
++e) {
|
||||
for (size_t e=s;(tol=toptsColl->GetTranslationOptionList(s,e))!=NULL;++e) {
|
||||
BOOST_FOREACH(TranslationOption const* topt, *tol) {
|
||||
std::map<std::string, xmlrpc_c::value> toptXml;
|
||||
TargetPhrase const& tp = topt->GetTargetPhrase();
|
||||
StaticData const& GLOBAL = StaticData::Instance();
|
||||
std::string tphrase = tp.GetStringRep(GLOBAL.GetOutputFactorOrder());
|
||||
std::string tphrase = tp.GetStringRep(ofactor_order);
|
||||
toptXml["phrase"] = xmlrpc_c::value_string(tphrase);
|
||||
toptXml["fscore"] = xmlrpc_c::value_double(topt->GetFutureScore());
|
||||
toptXml["start"] = xmlrpc_c::value_int(s);
|
||||
@ -241,7 +216,6 @@ insertTranslationOptions(Moses::Manager& manager,
|
||||
= topt->GetScoreBreakdown().getCoreFeatures();
|
||||
for (size_t j = 0; j < scores.size(); ++j)
|
||||
scoresXml.push_back(xmlrpc_c::value_double(scores[j]));
|
||||
|
||||
toptXml["scores"] = xmlrpc_c::value_array(scoresXml);
|
||||
toptsXml.push_back(xmlrpc_c::value_struct(toptXml));
|
||||
}
|
||||
@ -254,7 +228,6 @@ TranslationRequest::
|
||||
TranslationRequest(xmlrpc_c::paramList const& paramList,
|
||||
boost::condition_variable& cond, boost::mutex& mut)
|
||||
: m_cond(cond), m_mutex(mut), m_done(false), m_paramList(paramList)
|
||||
// , m_nbestSize(0)
|
||||
, m_session_id(0)
|
||||
{
|
||||
m_options = StaticData::Instance().options();
|
||||
@ -276,9 +249,7 @@ TranslationRequest::
|
||||
parse_request(std::map<std::string, xmlrpc_c::value> const& params)
|
||||
{
|
||||
// parse XMLRPC request
|
||||
// params_t const params = m_paramList.getStruct(0);
|
||||
m_paramList.verifyEnd(1); // ??? UG
|
||||
|
||||
m_options.update(params);
|
||||
|
||||
// source text must be given, or we don't know what to translate
|
||||
@ -295,8 +266,6 @@ parse_request(std::map<std::string, xmlrpc_c::value> const& params)
|
||||
else
|
||||
m_session_id = 0;
|
||||
|
||||
m_withAlignInfo = check(params, "align");
|
||||
m_withWordAlignInfo = check(params, "word-align");
|
||||
m_withGraphInfo = check(params, "sg");
|
||||
m_withTopts = check(params, "topt");
|
||||
m_withScoreBreakdown = check(params, "add-score-breakdown");
|
||||
@ -320,10 +289,6 @@ parse_request(std::map<std::string, xmlrpc_c::value> const& params)
|
||||
}
|
||||
}
|
||||
|
||||
// si = params.find("nbest");
|
||||
// if (si != params.end())
|
||||
// m_nbestSize = xmlrpc_c::value_int(si->second);
|
||||
|
||||
si = params.find("context");
|
||||
if (si != params.end())
|
||||
{
|
||||
@ -379,14 +344,13 @@ pack_hypothesis(const Moses::Manager& manager, vector<Hypothesis const* > const&
|
||||
ostringstream target;
|
||||
BOOST_REVERSE_FOREACH(Hypothesis const* e, edges) {
|
||||
manager.OutputSurface(target, *e);
|
||||
// , m_options.output.factor_order,
|
||||
// m_options.output.ReportSegmentation, m_options.output.ReportAllFactors);
|
||||
}
|
||||
XVERBOSE(1, "BEST TRANSLATION: " << *(manager.GetBestHypothesis()) << std::endl);
|
||||
// XVERBOSE(1,"SERVER TRANSLATION: " << target.str() << std::endl);
|
||||
XVERBOSE(1, "BEST TRANSLATION: " << *(manager.GetBestHypothesis())
|
||||
<< std::endl);
|
||||
dest[key] = xmlrpc_c::value_string(target.str());
|
||||
|
||||
if (m_withAlignInfo) {
|
||||
// if (m_withAlignInfo) {
|
||||
if (options().output.ReportSegmentation) {
|
||||
// phrase alignment, if requested
|
||||
|
||||
vector<xmlrpc_c::value> p_aln;
|
||||
@ -395,7 +359,8 @@ pack_hypothesis(const Moses::Manager& manager, vector<Hypothesis const* > const&
|
||||
dest["align"] = xmlrpc_c::value_array(p_aln);
|
||||
}
|
||||
|
||||
if (m_withWordAlignInfo) {
|
||||
// if (m_withWordAlignInfo) {
|
||||
if (options().output.PrintAlignmentInfo) {
|
||||
// word alignment, if requested
|
||||
vector<xmlrpc_c::value> w_aln;
|
||||
BOOST_REVERSE_FOREACH(Hypothesis const* e, edges)
|
||||
@ -421,15 +386,13 @@ void
|
||||
TranslationRequest::
|
||||
run_phrase_decoder()
|
||||
{
|
||||
if (m_withGraphInfo || m_options.nbest.nbest_size>0)
|
||||
if (m_withGraphInfo || m_options.nbest.nbest_size > 0) {
|
||||
m_options.output.SearchGraph = "true";
|
||||
m_options.nbest.enabled = true;
|
||||
}
|
||||
|
||||
Manager manager(this->self());
|
||||
// if (m_bias.size()) manager.SetBias(&m_bias);
|
||||
|
||||
|
||||
manager.Decode();
|
||||
|
||||
pack_hypothesis(manager, manager.GetBestHypothesis(), "text", m_retData);
|
||||
if (m_session_id)
|
||||
m_retData["session-id"] = xmlrpc_c::value_int(m_session_id);
|
||||
@ -438,9 +401,5 @@ run_phrase_decoder()
|
||||
if (m_withTopts) insertTranslationOptions(manager,m_retData);
|
||||
if (m_options.nbest.nbest_size) outputNBest(manager, m_retData);
|
||||
|
||||
// (const_cast<StaticData&>(Moses::StaticData::Instance()))
|
||||
// .SetOutputSearchGraph(false);
|
||||
// WTF? one more reason not to have this as global variable! --- UG
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user