00001
00002
00003 #include "PhraseDictionaryTreeAdaptor.h"
00004 #include <sys/stat.h>
00005 #include <algorithm>
00006 #include "PhraseDictionaryTree.h"
00007 #include "Phrase.h"
00008 #include "FactorCollection.h"
00009 #include "InputFileStream.h"
00010 #include "InputType.h"
00011 #include "ConfusionNet.h"
00012 #include "Sentence.h"
00013 #include "StaticData.h"
00014 #include "UniqueObject.h"
00015 #include "PDTAimp.h"
00016 #include "UserMessage.h"
00017
00018 namespace Moses
00019 {
00020
00021
00022
00023
00024
00025 PhraseDictionaryTreeAdaptor::
00026 PhraseDictionaryTreeAdaptor(size_t numScoreComponent, unsigned numInputScores, const PhraseDictionaryFeature* feature)
00027 : PhraseDictionary(numScoreComponent,feature), imp(new PDTAimp(this,numInputScores))
00028 {
00029 }
00030
00031 PhraseDictionaryTreeAdaptor::~PhraseDictionaryTreeAdaptor()
00032 {
00033 imp->CleanUp();
00034 delete imp;
00035 }
00036
00037
00038 bool PhraseDictionaryTreeAdaptor::Load(const std::vector<FactorType> &input
00039 , const std::vector<FactorType> &output
00040 , const std::string &filePath
00041 , const std::vector<float> &weight
00042 , size_t tableLimit
00043 , const LMList &languageModels
00044 , float weightWP)
00045 {
00046 if(m_numScoreComponent!=weight.size()) {
00047 std::stringstream strme;
00048 strme << "ERROR: mismatch of number of scaling factors: "<<weight.size()
00049 <<" "<<m_numScoreComponent<<"\n";
00050 UserMessage::Add(strme.str());
00051 return false;
00052 }
00053
00054
00055
00056 m_tableLimit=tableLimit;
00057
00058 imp->Create(input,output,filePath,
00059 weight,languageModels,weightWP);
00060 return true;
00061 }
00062
00063 void PhraseDictionaryTreeAdaptor::InitializeForInput(InputType const& source)
00064 {
00065 imp->CleanUp();
00066
00067 if(ConfusionNet const* cn=dynamic_cast<ConfusionNet const*>(&source))
00068 imp->CacheSource(*cn);
00069 }
00070
00071 TargetPhraseCollection const*
00072 PhraseDictionaryTreeAdaptor::GetTargetPhraseCollection(Phrase const &src) const
00073 {
00074 return imp->GetTargetPhraseCollection(src);
00075 }
00076
00077 TargetPhraseCollection const*
00078 PhraseDictionaryTreeAdaptor::GetTargetPhraseCollection(InputType const& src,WordsRange const &range) const
00079 {
00080 if(imp->m_rangeCache.empty()) {
00081 return imp->GetTargetPhraseCollection(src.GetSubString(range));
00082 } else {
00083 return imp->m_rangeCache[range.GetStartPos()][range.GetEndPos()];
00084 }
00085 }
00086
00087 void PhraseDictionaryTreeAdaptor::EnableCache()
00088 {
00089 imp->useCache=1;
00090 }
00091 void PhraseDictionaryTreeAdaptor::DisableCache()
00092 {
00093 imp->useCache=0;
00094 }
00095
00096
00097
00098 size_t PhraseDictionaryTreeAdaptor::GetNumInputScores() const
00099 {
00100 return imp->GetNumInputScores();
00101 }
00102
00103 std::string PhraseDictionaryTreeAdaptor::GetScoreProducerDescription(unsigned idx) const{
00104 if (idx < imp->GetNumInputScores()){
00105 return "InputScore";
00106 }else{
00107 return "PhraseModel";
00108 }
00109 }
00110
00111 std::string PhraseDictionaryTreeAdaptor::GetScoreProducerWeightShortName(unsigned idx) const
00112 {
00113 if (idx < imp->GetNumInputScores()){
00114 return "I";
00115 }else{
00116 return "tm";
00117 }
00118 }
00119
00120 }