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
|
|
|
|
***********************************************************************/
|
|
|
|
|
2010-02-24 14:15:44 +03:00
|
|
|
#ifndef moses_SentenceStats_h
|
|
|
|
#define moses_SentenceStats_h
|
2008-06-11 14:52:57 +04:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2015-03-28 16:09:03 +03:00
|
|
|
#include <ctime>
|
2014-01-03 22:45:31 +04:00
|
|
|
#include "Timer.h"
|
2008-06-11 14:52:57 +04:00
|
|
|
#include "Phrase.h"
|
|
|
|
#include "Hypothesis.h"
|
|
|
|
#include "TypeDef.h" //FactorArray
|
|
|
|
#include "InputType.h"
|
|
|
|
#include "Util.h" //Join()
|
|
|
|
|
2008-10-09 03:51:26 +04:00
|
|
|
namespace Moses
|
|
|
|
{
|
|
|
|
|
2012-06-29 02:29:46 +04:00
|
|
|
//! Hold info about recombination. Used by SentenceStats class
|
2011-02-24 16:14:42 +03:00
|
|
|
struct RecombinationInfo {
|
|
|
|
RecombinationInfo() {} //for std::vector
|
|
|
|
RecombinationInfo(size_t srcWords, float gProb, float bProb)
|
|
|
|
: numSourceWords(srcWords), betterProb(gProb), worseProb(bProb) {}
|
|
|
|
|
|
|
|
size_t numSourceWords;
|
|
|
|
float betterProb, worseProb;
|
2008-06-11 14:52:57 +04:00
|
|
|
};
|
|
|
|
|
2012-06-29 02:29:46 +04:00
|
|
|
/**
|
2008-06-11 14:52:57 +04:00
|
|
|
* stats relating to decoder operation on a given sentence
|
|
|
|
*/
|
|
|
|
class SentenceStats
|
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
public:
|
|
|
|
|
|
|
|
/***
|
|
|
|
* to be called before decoding a sentence
|
|
|
|
*/
|
|
|
|
SentenceStats(const InputType& source) {
|
|
|
|
Initialize(source);
|
|
|
|
}
|
|
|
|
void Initialize(const InputType& source) {
|
|
|
|
m_numHyposCreated = 0;
|
2014-01-03 22:45:31 +04:00
|
|
|
m_numHyposPopped = 0;
|
2011-02-24 16:14:42 +03:00
|
|
|
m_numHyposPruned = 0;
|
|
|
|
m_numHyposDiscarded = 0;
|
|
|
|
m_numHyposEarlyDiscarded = 0;
|
|
|
|
m_numHyposNotBuilt = 0;
|
|
|
|
m_totalSourceWords = source.GetSize();
|
|
|
|
m_recombinationInfos.clear();
|
|
|
|
m_deletedWords.clear();
|
|
|
|
m_insertedWords.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
* to be called after decoding a sentence
|
|
|
|
*/
|
|
|
|
void CalcFinalStats(const Hypothesis& bestHypo);
|
|
|
|
|
|
|
|
unsigned int GetTotalHypos() const {
|
|
|
|
return m_numHyposCreated + m_numHyposNotBuilt;
|
|
|
|
}
|
2014-01-03 22:45:31 +04:00
|
|
|
unsigned int GetNumHyposPopped() const {
|
|
|
|
return m_numHyposPopped;
|
|
|
|
}
|
2011-02-24 16:14:42 +03:00
|
|
|
size_t GetNumHyposRecombined() const {
|
|
|
|
return m_recombinationInfos.size();
|
|
|
|
}
|
|
|
|
unsigned int GetNumHyposPruned() const {
|
|
|
|
return m_numHyposPruned;
|
|
|
|
}
|
|
|
|
unsigned int GetNumHyposDiscarded() const {
|
|
|
|
return m_numHyposDiscarded;
|
|
|
|
}
|
|
|
|
unsigned int GetNumHyposEarlyDiscarded() const {
|
|
|
|
return m_numHyposEarlyDiscarded;
|
|
|
|
}
|
|
|
|
unsigned int GetNumHyposNotBuilt() const {
|
|
|
|
return m_numHyposNotBuilt;
|
|
|
|
}
|
2014-01-03 22:45:31 +04:00
|
|
|
double GetTimeCollectOpts() const {
|
|
|
|
return m_timeCollectOpts.get_elapsed_time();
|
|
|
|
}
|
|
|
|
double GetTimeBuildHyp() const {
|
|
|
|
return m_timeBuildHyp.get_elapsed_time();
|
2011-02-24 16:14:42 +03:00
|
|
|
}
|
2014-01-03 22:45:31 +04:00
|
|
|
double GetTimeCalcLM() const {
|
|
|
|
return m_timeCalcLM.get_elapsed_time();
|
2011-02-24 16:14:42 +03:00
|
|
|
}
|
2014-01-03 22:45:31 +04:00
|
|
|
double GetTimeOtherScore() const {
|
|
|
|
return m_timeOtherScore.get_elapsed_time();
|
2011-02-24 16:14:42 +03:00
|
|
|
}
|
2014-01-03 22:45:31 +04:00
|
|
|
double GetTimeEstimateScore() const {
|
|
|
|
return m_timeEstimateScore.get_elapsed_time();
|
2011-02-24 16:14:42 +03:00
|
|
|
}
|
2014-01-03 22:45:31 +04:00
|
|
|
double GetTimeStack() const {
|
|
|
|
return m_timeStack.get_elapsed_time();
|
2011-02-24 16:14:42 +03:00
|
|
|
}
|
2014-01-03 22:45:31 +04:00
|
|
|
double GetTimeSetupCubes() const {
|
|
|
|
return m_timeSetupCubes.get_elapsed_time();
|
2011-02-24 16:14:42 +03:00
|
|
|
}
|
2014-01-03 22:45:31 +04:00
|
|
|
double GetTimeManageCubes() const {
|
|
|
|
return m_timeManageCubes.get_elapsed_time();
|
|
|
|
}
|
|
|
|
double GetTimeTotal() const {
|
|
|
|
return m_timeTotal.get_elapsed_time();
|
2011-02-24 16:14:42 +03:00
|
|
|
}
|
|
|
|
size_t GetTotalSourceWords() const {
|
|
|
|
return m_totalSourceWords;
|
|
|
|
}
|
|
|
|
size_t GetNumWordsDeleted() const {
|
|
|
|
return m_deletedWords.size();
|
|
|
|
}
|
|
|
|
size_t GetNumWordsInserted() const {
|
|
|
|
return m_insertedWords.size();
|
|
|
|
}
|
|
|
|
const std::vector<const Phrase*>& GetDeletedWords() const {
|
|
|
|
return m_deletedWords;
|
|
|
|
}
|
|
|
|
const std::vector<std::string>& GetInsertedWords() const {
|
|
|
|
return m_insertedWords;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddRecombination(const Hypothesis& worseHypo, const Hypothesis& betterHypo) {
|
|
|
|
m_recombinationInfos.push_back(RecombinationInfo(worseHypo.GetWordsBitmap().GetNumWordsCovered(),
|
2015-11-09 15:44:39 +03:00
|
|
|
betterHypo.GetFutureScore(), worseHypo.GetFutureScore()));
|
2011-02-24 16:14:42 +03:00
|
|
|
}
|
|
|
|
void AddCreated() {
|
|
|
|
m_numHyposCreated++;
|
|
|
|
}
|
2014-01-03 22:45:31 +04:00
|
|
|
void AddPopped() {
|
|
|
|
m_numHyposPopped++;
|
|
|
|
}
|
2011-02-24 16:14:42 +03:00
|
|
|
void AddPruning() {
|
|
|
|
m_numHyposPruned++;
|
|
|
|
}
|
|
|
|
void AddEarlyDiscarded() {
|
|
|
|
m_numHyposEarlyDiscarded++;
|
|
|
|
}
|
|
|
|
void AddNotBuilt() {
|
|
|
|
m_numHyposNotBuilt++;
|
|
|
|
}
|
|
|
|
void AddDiscarded() {
|
|
|
|
m_numHyposDiscarded++;
|
|
|
|
}
|
|
|
|
|
2014-01-03 22:45:31 +04:00
|
|
|
void StartTimeCollectOpts() {
|
|
|
|
m_timeCollectOpts.start();
|
|
|
|
}
|
|
|
|
void StopTimeCollectOpts() {
|
|
|
|
m_timeCollectOpts.stop();
|
|
|
|
}
|
|
|
|
void StartTimeBuildHyp() {
|
|
|
|
m_timeBuildHyp.start();
|
|
|
|
}
|
|
|
|
void StopTimeBuildHyp() {
|
|
|
|
m_timeBuildHyp.stop();
|
|
|
|
}
|
|
|
|
void StartTimeCalcLM() {
|
|
|
|
m_timeCalcLM.start();
|
|
|
|
}
|
|
|
|
void StopTimeCalcLM() {
|
|
|
|
m_timeCalcLM.stop();
|
|
|
|
}
|
|
|
|
void StartTimeOtherScore() {
|
|
|
|
m_timeOtherScore.start();
|
|
|
|
}
|
|
|
|
void StopTimeOtherScore() {
|
|
|
|
m_timeOtherScore.stop();
|
|
|
|
}
|
|
|
|
void StartTimeEstimateScore() {
|
|
|
|
m_timeEstimateScore.start();
|
|
|
|
}
|
|
|
|
void StopTimeEstimateScore() {
|
|
|
|
m_timeEstimateScore.stop();
|
|
|
|
}
|
|
|
|
void StartTimeSetupCubes() {
|
|
|
|
m_timeSetupCubes.start();
|
|
|
|
}
|
|
|
|
void StopTimeSetupCubes() {
|
|
|
|
m_timeSetupCubes.stop();
|
2011-02-24 16:14:42 +03:00
|
|
|
}
|
2014-01-03 22:45:31 +04:00
|
|
|
void StartTimeManageCubes() {
|
|
|
|
m_timeManageCubes.start();
|
2011-02-24 16:14:42 +03:00
|
|
|
}
|
2014-01-03 22:45:31 +04:00
|
|
|
void StopTimeManageCubes() {
|
|
|
|
m_timeManageCubes.stop();
|
2011-02-24 16:14:42 +03:00
|
|
|
}
|
2014-01-03 22:45:31 +04:00
|
|
|
void StartTimeStack() {
|
|
|
|
m_timeStack.start();
|
2011-02-24 16:14:42 +03:00
|
|
|
}
|
2014-01-03 22:45:31 +04:00
|
|
|
void StopTimeStack() {
|
|
|
|
m_timeStack.stop();
|
2011-02-24 16:14:42 +03:00
|
|
|
}
|
2014-01-03 22:45:31 +04:00
|
|
|
void StartTimeTotal() {
|
|
|
|
m_timeTotal.start();
|
2011-02-24 16:14:42 +03:00
|
|
|
}
|
2014-01-03 22:45:31 +04:00
|
|
|
void StopTimeTotal() {
|
|
|
|
m_timeTotal.stop();
|
2011-02-24 16:14:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
/***
|
|
|
|
* auxiliary to CalcFinalStats()
|
|
|
|
*/
|
|
|
|
void AddDeletedWords(const Hypothesis& hypo);
|
|
|
|
|
|
|
|
//hypotheses
|
2012-08-10 23:32:00 +04:00
|
|
|
// TODO: Move away from clock_t in favor of just storing doubles of the number of seconds
|
|
|
|
// since clock seconds aren't reliable in a multi-threaded environment -Jon
|
|
|
|
// (see Manager.cpp for some initial work moving in this direction)
|
2011-02-24 16:14:42 +03:00
|
|
|
std::vector<RecombinationInfo> m_recombinationInfos;
|
|
|
|
unsigned int m_numHyposCreated;
|
2014-01-03 22:45:31 +04:00
|
|
|
unsigned int m_numHyposPopped;
|
2011-02-24 16:14:42 +03:00
|
|
|
unsigned int m_numHyposPruned;
|
|
|
|
unsigned int m_numHyposDiscarded;
|
|
|
|
unsigned int m_numHyposEarlyDiscarded;
|
|
|
|
unsigned int m_numHyposNotBuilt;
|
2014-01-03 22:45:31 +04:00
|
|
|
Timer m_timeCollectOpts;
|
|
|
|
Timer m_timeBuildHyp;
|
|
|
|
Timer m_timeEstimateScore;
|
|
|
|
Timer m_timeOtherScore;
|
|
|
|
Timer m_timeCalcLM;
|
|
|
|
Timer m_timeStack;
|
|
|
|
Timer m_timeSetupCubes;
|
|
|
|
Timer m_timeManageCubes;
|
|
|
|
Timer m_timeTotal;
|
2011-02-24 16:14:42 +03:00
|
|
|
|
|
|
|
//words
|
|
|
|
size_t m_totalSourceWords;
|
|
|
|
std::vector<const Phrase*> m_deletedWords; //count deleted words/phrases in the final hypothesis
|
|
|
|
std::vector<std::string> m_insertedWords; //count inserted words in the final hypothesis
|
2008-06-11 14:52:57 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
inline std::ostream& operator<<(std::ostream& os, const SentenceStats& ss)
|
|
|
|
{
|
2014-01-03 22:45:31 +04:00
|
|
|
double totalTime = ss.GetTimeTotal();
|
|
|
|
double otherTime = totalTime - (ss.GetTimeCollectOpts() + ss.GetTimeBuildHyp() + ss.GetTimeEstimateScore() + ss.GetTimeCalcLM() + ss.GetTimeOtherScore() + ss.GetTimeStack() + ss.GetTimeSetupCubes() + ss.GetTimeManageCubes());
|
2008-12-13 15:08:55 +03:00
|
|
|
|
|
|
|
return os << "total hypotheses considered = " << ss.GetTotalHypos() << std::endl
|
2014-01-03 22:45:31 +04:00
|
|
|
<< " number popped from cube = " << ss.GetNumHyposPopped() << std::endl
|
2011-02-24 16:14:42 +03:00
|
|
|
<< " number not built = " << ss.GetNumHyposNotBuilt() << std::endl
|
|
|
|
<< " number discarded early = " << ss.GetNumHyposEarlyDiscarded() << std::endl
|
|
|
|
<< " number discarded = " << ss.GetNumHyposDiscarded() << std::endl
|
|
|
|
<< " number recombined = " << ss.GetNumHyposRecombined() << std::endl
|
|
|
|
<< " number pruned = " << ss.GetNumHyposPruned() << std::endl
|
|
|
|
|
|
|
|
<< "time to collect opts " << ss.GetTimeCollectOpts() << " (" << (int)(100 * ss.GetTimeCollectOpts()/totalTime) << "%)" << std::endl
|
|
|
|
<< " create hyps " << ss.GetTimeBuildHyp() << " (" << (int)(100 * ss.GetTimeBuildHyp()/totalTime) << "%)" << std::endl
|
|
|
|
<< " estimate score " << ss.GetTimeEstimateScore() << " (" << (int)(100 * ss.GetTimeEstimateScore()/totalTime) << "%)" << std::endl
|
|
|
|
<< " calc lm " << ss.GetTimeCalcLM() << " (" << (int)(100 * ss.GetTimeCalcLM()/totalTime) << "%)" << std::endl
|
|
|
|
<< " other hyp score " << ss.GetTimeOtherScore() << " (" << (int)(100 * ss.GetTimeOtherScore()/totalTime) << "%)" << std::endl
|
2014-01-03 22:45:31 +04:00
|
|
|
<< " set up cubes " << ss.GetTimeSetupCubes() << " (" << (int)(100 * ss.GetTimeSetupCubes()/totalTime) << "%)" << std::endl
|
|
|
|
<< " manage cubes " << ss.GetTimeManageCubes() << " (" << (int)(100 * ss.GetTimeManageCubes()/totalTime) << "%)" << std::endl
|
2011-02-24 16:14:42 +03:00
|
|
|
<< " manage stacks " << ss.GetTimeStack() << " (" << (int)(100 * ss.GetTimeStack()/totalTime) << "%)" << std::endl
|
|
|
|
<< " other " << otherTime << " (" << (int)(100 * otherTime/totalTime) << "%)" << std::endl
|
|
|
|
|
|
|
|
<< "total source words = " << ss.GetTotalSourceWords() << std::endl
|
|
|
|
<< " words deleted = " << ss.GetNumWordsDeleted() << " (" << Join(" ", ss.GetDeletedWords()) << ")" << std::endl
|
|
|
|
<< " words inserted = " << ss.GetNumWordsInserted() << " (" << Join(" ", ss.GetInsertedWords()) << ")" << std::endl;
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
2008-10-09 03:51:26 +04:00
|
|
|
|
|
|
|
}
|
2010-02-24 14:15:44 +03:00
|
|
|
#endif
|