00001 // $Id$ 00002 00003 #include <sys/stat.h> 00004 #include <algorithm> 00005 #include "moses/TranslationModel/PhraseDictionaryTreeAdaptor.h" 00006 #include "moses/TranslationModel/PhraseDictionaryTree.h" 00007 #include "moses/Phrase.h" 00008 #include "moses/FactorCollection.h" 00009 #include "moses/InputFileStream.h" 00010 #include "moses/InputType.h" 00011 #include "moses/ConfusionNet.h" 00012 #include "moses/Sentence.h" 00013 #include "moses/StaticData.h" 00014 #include "moses/UniqueObject.h" 00015 #include "moses/PDTAimp.h" 00016 #include "moses/TranslationTask.h" 00017 #include "util/exception.hh" 00018 #include "util/string_stream.hh" 00019 00020 using namespace std; 00021 00022 namespace Moses 00023 { 00024 /************************************************************* 00025 function definitions of the interface class 00026 virtually everything is forwarded to the implementation class 00027 *************************************************************/ 00028 00029 PhraseDictionaryTreeAdaptor:: 00030 PhraseDictionaryTreeAdaptor(const std::string &line) 00031 : PhraseDictionary(line, true) 00032 { 00033 ReadParameters(); 00034 } 00035 00036 PhraseDictionaryTreeAdaptor::~PhraseDictionaryTreeAdaptor() 00037 { 00038 } 00039 00040 void PhraseDictionaryTreeAdaptor::Load(AllOptions::ptr const& opts) 00041 { 00042 m_options = opts; 00043 SetFeaturesToApply(); 00044 } 00045 00046 void PhraseDictionaryTreeAdaptor::InitializeForInput(ttasksptr const& ttask) 00047 { 00048 InputType const& source = *ttask->GetSource(); 00049 const StaticData &staticData = StaticData::Instance(); 00050 00051 ReduceCache(); 00052 00053 PDTAimp *obj = new PDTAimp(this); 00054 00055 vector<float> weight = staticData.GetWeights(this); 00056 if(m_numScoreComponents!=weight.size()) { 00057 util::StringStream strme; 00058 UTIL_THROW2("ERROR: mismatch of number of scaling factors: " << weight.size() 00059 << " " << m_numScoreComponents); 00060 } 00061 00062 obj->Create(m_input, m_output, m_filePath, weight); 00063 00064 obj->CleanUp(); 00065 // caching only required for confusion net 00066 if(ConfusionNet const* cn=dynamic_cast<ConfusionNet const*>(&source)) 00067 obj->CacheSource(*cn); 00068 00069 m_implementation.reset(obj); 00070 } 00071 00072 void PhraseDictionaryTreeAdaptor::CleanUpAfterSentenceProcessing(InputType const& source) 00073 { 00074 PDTAimp &obj = GetImplementation(); 00075 obj.CleanUp(); 00076 } 00077 00078 TargetPhraseCollection::shared_ptr 00079 PhraseDictionaryTreeAdaptor::GetTargetPhraseCollectionNonCacheLEGACY(Phrase const &src) const 00080 { 00081 return GetImplementation().GetTargetPhraseCollection(src); 00082 } 00083 00084 void PhraseDictionaryTreeAdaptor::EnableCache() 00085 { 00086 GetImplementation().useCache=1; 00087 } 00088 void PhraseDictionaryTreeAdaptor::DisableCache() 00089 { 00090 GetImplementation().useCache=0; 00091 } 00092 00093 PDTAimp& PhraseDictionaryTreeAdaptor::GetImplementation() 00094 { 00095 PDTAimp* dict; 00096 dict = m_implementation.get(); 00097 UTIL_THROW_IF2(dict == NULL, "Dictionary object not yet created for this thread"); 00098 return *dict; 00099 } 00100 00101 const PDTAimp& PhraseDictionaryTreeAdaptor::GetImplementation() const 00102 { 00103 PDTAimp* dict; 00104 dict = m_implementation.get(); 00105 UTIL_THROW_IF2(dict == NULL, "Dictionary object not yet created for this thread"); 00106 return *dict; 00107 } 00108 00109 // legacy 00110 TargetPhraseCollectionWithSourcePhrase::shared_ptr 00111 PhraseDictionaryTreeAdaptor:: 00112 GetTargetPhraseCollectionLEGACY(InputType const& src,Range const &range) const 00113 { 00114 TargetPhraseCollectionWithSourcePhrase::shared_ptr ret; 00115 if(GetImplementation().m_rangeCache.empty()) { 00116 ret = GetImplementation().GetTargetPhraseCollection(src.GetSubString(range)); 00117 } else { 00118 ret = GetImplementation().m_rangeCache[range.GetStartPos()][range.GetEndPos()]; 00119 } 00120 return ret; 00121 } 00122 00123 }