00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "Base.h"
00023 #include "moses/TypeDef.h"
00024 #include "moses/Util.h"
00025 #include "moses/Manager.h"
00026 #include "moses/ChartManager.h"
00027 #include "moses/FactorCollection.h"
00028 #include "moses/Phrase.h"
00029 #include "util/exception.hh"
00030
00031 using namespace std;
00032
00033 namespace Moses
00034 {
00035
00036 LanguageModel::LanguageModel(const std::string &line) :
00037 StatefulFeatureFunction(line, false),
00038 m_enableOOVFeature(false)
00039 {
00040
00041
00042 this->m_numScoreComponents = this->m_numTuneableComponents = m_enableOOVFeature ? 2 : 1;
00043
00044
00045 }
00046
00047
00048 LanguageModel::~LanguageModel() {}
00049
00050 void LanguageModel::IncrementalCallback(Incremental::Manager &manager) const
00051 {
00052 UTIL_THROW(util::Exception, "Incremental search is only supported by KenLM.");
00053 }
00054
00055 void LanguageModel::ReportHistoryOrder(std::ostream &out,const Phrase &phrase) const
00056 {
00057
00058 }
00059
00060 void
00061 LanguageModel::
00062 EvaluateInIsolation(Phrase const& source, TargetPhrase const& targetPhrase,
00063 ScoreComponentCollection &scoreBreakdown,
00064 ScoreComponentCollection &estimatedScores) const
00065 {
00066
00067 float fullScore, nGramScore;
00068 size_t oovCount;
00069
00070 CalcScore(targetPhrase, fullScore, nGramScore, oovCount);
00071
00072 float estimateScore = fullScore - nGramScore;
00073
00074 if (m_enableOOVFeature) {
00075 vector<float> scores(2), estimateScores(2);
00076 scores[0] = nGramScore;
00077 scores[1] = oovCount;
00078 scoreBreakdown.Assign(this, scores);
00079
00080 estimateScores[0] = estimateScore;
00081 estimateScores[1] = 0;
00082 estimatedScores.Assign(this, estimateScores);
00083 } else {
00084 scoreBreakdown.Assign(this, nGramScore);
00085 estimatedScores.Assign(this, estimateScore);
00086 }
00087 }
00088
00089 const LanguageModel &LanguageModel::GetFirstLM()
00090 {
00091 static const LanguageModel *lmStatic = NULL;
00092
00093 if (lmStatic) {
00094 return *lmStatic;
00095 }
00096
00097
00098 const std::vector<const StatefulFeatureFunction*> &statefulFFs = StatefulFeatureFunction::GetStatefulFeatureFunctions();
00099 for (size_t i = 0; i < statefulFFs.size(); ++i) {
00100 const StatefulFeatureFunction *ff = statefulFFs[i];
00101 const LanguageModel *lm = dynamic_cast<const LanguageModel*>(ff);
00102
00103 if (lm) {
00104 lmStatic = lm;
00105 return *lmStatic;
00106 }
00107 }
00108
00109 throw std::logic_error("Incremental search only supports one language model.");
00110 }
00111
00112 void LanguageModel::SetParameter(const std::string& key, const std::string& value)
00113 {
00114 if(key == "oov-feature") {
00115 m_enableOOVFeature = Scan<bool>(value);
00116 this->m_numScoreComponents = this->m_numTuneableComponents = m_enableOOVFeature ? 2 : 1;
00117 } else {
00118 StatefulFeatureFunction::SetParameter(key, value);
00119 }
00120 }
00121
00122 }