Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TranslationSystem.cpp « src « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e017ba241ed831d87a47332eb38f90cce9a21c5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// $Id: $

/***********************************************************************
Moses - factored phrase-based language decoder
Copyright (C) 2010 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 <stdexcept>
#include <iostream>

#include "DecodeGraph.h"
#include "DecodeStep.h"
#include "DummyScoreProducers.h"
#include "GlobalLexicalModel.h"
#include "GlobalLexicalModelUnlimited.h"
#include "WordTranslationFeature.h"
#include "PhrasePairFeature.h"
#include "LexicalReordering.h"
#include "StaticData.h"
#include "TranslationSystem.h"
#include "Util.h"

using namespace std;

namespace Moses {
  
  const string TranslationSystem::DEFAULT = "default";

    TranslationSystem::TranslationSystem(const std::string& id, 
                      const WordPenaltyProducer* wpProducer,
                      const UnknownWordPenaltyProducer* uwpProducer,
                      const DistortionScoreProducer* distortionProducer)
    : m_id(id), m_wpProducer(wpProducer), m_unknownWpProducer(uwpProducer), m_distortionScoreProducer(distortionProducer)
    {
      AddFeatureFunction(wpProducer);
      AddFeatureFunction(uwpProducer);
      if (distortionProducer) {
        AddFeatureFunction(distortionProducer);
      }
    }
    
    //Insert core 'big' features
    void TranslationSystem::AddLanguageModel(LanguageModel* languageModel) {
      m_languageModels.Add(languageModel);
      AddFeatureFunction(languageModel);
    }

    void TranslationSystem::AddDecodeGraph(DecodeGraph* decodeGraph, size_t backoff) {
      m_decodeGraphs.push_back(decodeGraph);
      m_decodeGraphBackoff.push_back(backoff);
    }

    void TranslationSystem::AddReorderModel(LexicalReordering* reorderModel) {
      m_reorderingTables.push_back(reorderModel);
      AddFeatureFunction(reorderModel);
    }

    void TranslationSystem::AddGlobalLexicalModel(GlobalLexicalModel* globalLexicalModel) {
      m_globalLexicalModels.push_back(globalLexicalModel);
      AddFeatureFunction(globalLexicalModel);
    }
    
    void TranslationSystem::AddFeatureFunction(const FeatureFunction* ff) {
			m_producers.push_back(ff);

      if (ff->IsStateless()) {
        const StatelessFeatureFunction* statelessFF = static_cast<const StatelessFeatureFunction*>(ff);
        if (!statelessFF->ComputeValueInTranslationOption()) {
            m_statelessFFs.push_back(statelessFF);
        }
      } else {
        m_statefulFFs.push_back(static_cast<const StatefulFeatureFunction*>(ff));
      }
    }

    void TranslationSystem::ConfigDictionaries() {
      for (vector<DecodeGraph*>::const_iterator i = m_decodeGraphs.begin();
        i != m_decodeGraphs.end(); ++i) {
          for (DecodeGraph::const_iterator j = (*i)->begin(); j != (*i)->end(); ++j) {
            const DecodeStep* step = *j;
            PhraseDictionaryFeature* pdict = const_cast<PhraseDictionaryFeature*>(step->GetPhraseDictionaryFeature());
            if (pdict) {
              m_phraseDictionaries.push_back(pdict);
              AddFeatureFunction(pdict);
              const_cast<PhraseDictionaryFeature*>(pdict)->InitDictionary(this);
            }
            GenerationDictionary* gdict = const_cast<GenerationDictionary*>(step->GetGenerationDictionaryFeature());
            if (gdict) {
              m_generationDictionaries.push_back(gdict);
              AddFeatureFunction(gdict);
            }
          }
      }
    }
    
    void TranslationSystem::InitializeBeforeSentenceProcessing(const InputType& source) const {
      for (vector<PhraseDictionaryFeature*>::const_iterator i = m_phraseDictionaries.begin();
           i != m_phraseDictionaries.end(); ++i) {
             (*i)->InitDictionary(this,source);
           }
           
      for(size_t i=0;i<m_reorderingTables.size();++i) {
        m_reorderingTables[i]->InitializeForInput(source);
      }
      for(size_t i=0;i<m_globalLexicalModels.size();++i) {
        m_globalLexicalModels[i]->InitializeForInput((Sentence const&)source);
      }
      //for(size_t i=0;i<m_statefulFFs.size();++i) {
      //}
      for(size_t i=0;i<m_statelessFFs.size();++i) {
    	if (m_statelessFFs[i]->GetScoreProducerWeightShortName() == "pp")
	  ((PhrasePairFeature*)m_statelessFFs[i])->InitializeForInput((Sentence const&)source);
	else if (m_statelessFFs[i]->GetScoreProducerWeightShortName() == "glm")
	  ((GlobalLexicalModelUnlimited*)m_statelessFFs[i])->InitializeForInput((Sentence const&)source);
        else if (m_statelessFFs[i]->GetScoreProducerWeightShortName() == "wt")
	  ((WordTranslationFeature*)m_statelessFFs[i])->InitializeForInput((Sentence const&)source);
      }
      
      LMList::const_iterator iterLM;
      for (iterLM = m_languageModels.begin() ; iterLM != m_languageModels.end() ; ++iterLM)
      {
        LanguageModel &languageModel = **iterLM;
        languageModel.InitializeBeforeSentenceProcessing();
      }
    }
    
     void TranslationSystem::CleanUpAfterSentenceProcessing() const {
        
        for(size_t i=0;i<m_phraseDictionaries.size();++i)
        {
          PhraseDictionaryFeature &phraseDictionaryFeature = *m_phraseDictionaries[i];
          PhraseDictionary* phraseDictionary = const_cast<PhraseDictionary*>(phraseDictionaryFeature.GetDictionary());
          phraseDictionary->CleanUp();

        }
  
        for(size_t i=0;i<m_generationDictionaries.size();++i)
            m_generationDictionaries[i]->CleanUp();
  
        //something LMs could do after each sentence 
        LMList::const_iterator iterLM;
        for (iterLM = m_languageModels.begin() ; iterLM != m_languageModels.end() ; ++iterLM)
        {
          LanguageModel &languageModel = **iterLM;
          languageModel.CleanUpAfterSentenceProcessing();
        }
     }
    
    float TranslationSystem::GetWeightWordPenalty() const {
      float weightWP = StaticData::Instance().GetWeight(m_wpProducer);
      //VERBOSE(1, "Read weightWP from translation sytem: " << weightWP << std::endl);
      return weightWP;
    }
    
    float TranslationSystem::GetWeightUnknownWordPenalty() const {
      return StaticData::Instance().GetWeight(m_unknownWpProducer);
    }
    
    float TranslationSystem::GetWeightDistortion() const {
      CHECK(m_distortionScoreProducer);
      return StaticData::Instance().GetWeight(m_distortionScoreProducer);
    }

    std::vector<float> TranslationSystem::GetTranslationWeights(size_t index) const {
    	std::vector<float> weights = StaticData::Instance().GetWeights(GetTranslationScoreProducer(index));
    	//VERBOSE(1, "Read weightT from translation sytem.. ");
    	for (size_t i = 0; i < weights.size(); ++i)
	  //VERBOSE(1, weights[i] << " ");
	//VERBOSE(1, std::endl);
    	return weights;
    }
};