00001 // $Id: $ 00002 00003 /*********************************************************************** 00004 Moses - factored phrase-based language decoder 00005 Copyright (C) 2010 University of Edinburgh 00006 00007 This library is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU Lesser General Public 00009 License as published by the Free Software Foundation; either 00010 version 2.1 of the License, or (at your option) any later version. 00011 00012 This library is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 Lesser General Public License for more details. 00016 00017 You should have received a copy of the GNU Lesser General Public 00018 License along with this library; if not, write to the Free Software 00019 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 ***********************************************************************/ 00021 00022 #include <stdexcept> 00023 #include <iostream> 00024 00025 #include "DecodeGraph.h" 00026 #include "DecodeStep.h" 00027 #include "DummyScoreProducers.h" 00028 #include "GlobalLexicalModel.h" 00029 #include "LexicalReordering.h" 00030 #include "StaticData.h" 00031 #include "TranslationSystem.h" 00032 #include "Util.h" 00033 00034 using namespace std; 00035 00036 namespace Moses 00037 { 00038 00039 const string TranslationSystem::DEFAULT = "default"; 00040 00041 TranslationSystem::TranslationSystem(const std::string& id, 00042 const WordPenaltyProducer* wpProducer, 00043 const UnknownWordPenaltyProducer* uwpProducer, 00044 const DistortionScoreProducer* distortionProducer) 00045 : m_id(id), m_wpProducer(wpProducer), m_unknownWpProducer(uwpProducer), m_distortionScoreProducer(distortionProducer) 00046 { 00047 AddFeatureFunction(wpProducer); 00048 AddFeatureFunction(uwpProducer); 00049 if (distortionProducer) { 00050 AddFeatureFunction(distortionProducer); 00051 } 00052 } 00053 00054 //Insert core 'big' features 00055 void TranslationSystem::AddLanguageModel(LanguageModel* languageModel) 00056 { 00057 m_languageModels.Add(languageModel); 00058 AddFeatureFunction(languageModel); 00059 } 00060 00061 00062 void TranslationSystem::AddDecodeGraph(DecodeGraph* decodeGraph, size_t backoff) 00063 { 00064 m_decodeGraphs.push_back(decodeGraph); 00065 m_decodeGraphBackoff.push_back(backoff); 00066 } 00067 00068 00069 void TranslationSystem::AddReorderModel(LexicalReordering* reorderModel) 00070 { 00071 m_reorderingTables.push_back(reorderModel); 00072 AddFeatureFunction(reorderModel); 00073 } 00074 00075 00076 void TranslationSystem::AddGlobalLexicalModel(GlobalLexicalModel* globalLexicalModel) 00077 { 00078 m_globalLexicalModels.push_back(globalLexicalModel); 00079 AddFeatureFunction(globalLexicalModel); 00080 } 00081 00082 00083 00084 00085 void TranslationSystem::AddFeatureFunction(const FeatureFunction* ff) 00086 { 00087 if (ff->IsStateless()) { 00088 const StatelessFeatureFunction* statelessFF = static_cast<const StatelessFeatureFunction*>(ff); 00089 if (!statelessFF->ComputeValueInTranslationOption()) { 00090 m_statelessFFs.push_back(statelessFF); 00091 } 00092 } else { 00093 m_statefulFFs.push_back(static_cast<const StatefulFeatureFunction*>(ff)); 00094 } 00095 } 00096 00097 void TranslationSystem::ConfigDictionaries() 00098 { 00099 for (vector<DecodeGraph*>::const_iterator i = m_decodeGraphs.begin(); i != m_decodeGraphs.end(); ++i) { 00100 for (DecodeGraph::const_iterator j = (*i)->begin(); j != (*i)->end(); ++j) { 00101 const DecodeStep* step = *j; 00102 PhraseDictionaryFeature* pdict = const_cast<PhraseDictionaryFeature*>(step->GetPhraseDictionaryFeature()); 00103 if (pdict) { 00104 m_phraseDictionaries.push_back(pdict); 00105 AddFeatureFunction(pdict); 00106 const_cast<PhraseDictionaryFeature*>(pdict)->InitDictionary(this); 00107 } 00108 GenerationDictionary* gdict = const_cast<GenerationDictionary*>(step->GetGenerationDictionaryFeature()); 00109 if (gdict) { 00110 m_generationDictionaries.push_back(gdict); 00111 AddFeatureFunction(gdict); 00112 } 00113 } 00114 } 00115 } 00116 00117 void TranslationSystem::InitializeBeforeSentenceProcessing(const InputType& source) const 00118 { 00119 for (vector<PhraseDictionaryFeature*>::const_iterator i = m_phraseDictionaries.begin(); 00120 i != m_phraseDictionaries.end(); ++i) { 00121 (*i)->InitDictionary(this,source); 00122 } 00123 00124 for(size_t i=0; i<m_reorderingTables.size(); ++i) { 00125 m_reorderingTables[i]->InitializeForInput(source); 00126 } 00127 for(size_t i=0; i<m_globalLexicalModels.size(); ++i) { 00128 m_globalLexicalModels[i]->InitializeForInput((Sentence const&)source); 00129 } 00130 00131 LMList::const_iterator iterLM; 00132 for (iterLM = m_languageModels.begin() ; iterLM != m_languageModels.end() ; ++iterLM) { 00133 LanguageModel &languageModel = **iterLM; 00134 languageModel.InitializeBeforeSentenceProcessing(); 00135 } 00136 } 00137 00138 void TranslationSystem::CleanUpAfterSentenceProcessing() const 00139 { 00140 00141 for(size_t i=0; i<m_phraseDictionaries.size(); ++i) { 00142 PhraseDictionaryFeature &phraseDictionaryFeature = *m_phraseDictionaries[i]; 00143 PhraseDictionary* phraseDictionary = const_cast<PhraseDictionary*>(phraseDictionaryFeature.GetDictionary()); 00144 phraseDictionary->CleanUp(); 00145 00146 } 00147 00148 for(size_t i=0; i<m_generationDictionaries.size(); ++i) 00149 m_generationDictionaries[i]->CleanUp(); 00150 00151 //something LMs could do after each sentence 00152 LMList::const_iterator iterLM; 00153 for (iterLM = m_languageModels.begin() ; iterLM != m_languageModels.end() ; ++iterLM) { 00154 LanguageModel &languageModel = **iterLM; 00155 languageModel.CleanUpAfterSentenceProcessing(); 00156 } 00157 } 00158 00159 float TranslationSystem::GetWeightWordPenalty() const 00160 { 00161 //const ScoreComponentCollection weights = StaticData::Instance().GetAllWeights(); 00162 size_t wpIndex = StaticData::Instance().GetScoreIndexManager().GetBeginIndex(m_wpProducer->GetScoreBookkeepingID()); 00163 return StaticData::Instance().GetAllWeights()[wpIndex]; 00164 } 00165 00166 float TranslationSystem::GetWeightUnknownWordPenalty() const 00167 { 00168 size_t uwpIndex = StaticData::Instance().GetScoreIndexManager(). 00169 GetBeginIndex(m_unknownWpProducer->GetScoreBookkeepingID()); 00170 return StaticData::Instance().GetAllWeights()[uwpIndex]; 00171 } 00172 00173 float TranslationSystem::GetWeightDistortion() const 00174 { 00175 CHECK(m_distortionScoreProducer); 00176 size_t distIndex = StaticData::Instance().GetScoreIndexManager(). 00177 GetBeginIndex(m_distortionScoreProducer->GetScoreBookkeepingID()); 00178 return StaticData::Instance().GetAllWeights()[distIndex]; 00179 } 00180 00181 };
1.5.9