00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <fstream>
00023 #include <string>
00024 #include <iterator>
00025 #include <algorithm>
00026 #include "Loader.h"
00027 #include "LoaderFactory.h"
00028 #include "PhraseDictionarySCFG.h"
00029 #include "moses/FactorCollection.h"
00030 #include "moses/Word.h"
00031 #include "moses/Util.h"
00032 #include "moses/InputFileStream.h"
00033 #include "moses/StaticData.h"
00034 #include "moses/WordsRange.h"
00035 #include "moses/UserMessage.h"
00036 #include "moses/TranslationModel/CYKPlusParser/ChartRuleLookupManagerMemory.h"
00037
00038 using namespace std;
00039
00040 namespace Moses
00041 {
00042
00043 TargetPhraseCollection &PhraseDictionarySCFG::GetOrCreateTargetPhraseCollection(
00044 const Phrase &source
00045 , const TargetPhrase &target
00046 , const Word *sourceLHS)
00047 {
00048 PhraseDictionaryNodeSCFG &currNode = GetOrCreateNode(source, target, sourceLHS);
00049 return currNode.GetOrCreateTargetPhraseCollection();
00050 }
00051
00052 const TargetPhraseCollection *PhraseDictionarySCFG::GetTargetPhraseCollection(const Phrase& source) const
00053 {
00054
00055 const size_t size = source.GetSize();
00056
00057 const PhraseDictionaryNodeSCFG *currNode = &m_collection;
00058 for (size_t pos = 0 ; pos < size ; ++pos) {
00059 const Word& word = source.GetWord(pos);
00060 currNode = currNode->GetChild(word);
00061 if (currNode == NULL)
00062 return NULL;
00063 }
00064
00065 const TargetPhraseCollection *coll = currNode->GetTargetPhraseCollection();
00066
00067
00068
00069
00070
00071
00072 return coll;
00073 }
00074
00075 PhraseDictionaryNodeSCFG &PhraseDictionarySCFG::GetOrCreateNode(const Phrase &source
00076 , const TargetPhrase &target
00077 , const Word *sourceLHS)
00078 {
00079 const size_t size = source.GetSize();
00080
00081 const AlignmentInfo &alignmentInfo = target.GetAlignNonTerm();
00082 AlignmentInfo::const_iterator iterAlign = alignmentInfo.begin();
00083
00084 PhraseDictionaryNodeSCFG *currNode = &m_collection;
00085 for (size_t pos = 0 ; pos < size ; ++pos) {
00086 const Word& word = source.GetWord(pos);
00087
00088 if (word.IsNonTerminal()) {
00089
00090 const Word &sourceNonTerm = word;
00091
00092 CHECK(iterAlign != alignmentInfo.end());
00093 CHECK(iterAlign->first == pos);
00094 size_t targetNonTermInd = iterAlign->second;
00095 ++iterAlign;
00096 const Word &targetNonTerm = target.GetWord(targetNonTermInd);
00097
00098 currNode = currNode->GetOrCreateChild(sourceNonTerm, targetNonTerm);
00099 } else {
00100 currNode = currNode->GetOrCreateChild(word);
00101 }
00102
00103 CHECK(currNode != NULL);
00104 }
00105
00106
00107
00108
00109
00110
00111 return *currNode;
00112 }
00113
00114 ChartRuleLookupManager *PhraseDictionarySCFG::CreateRuleLookupManager(
00115 const InputType &sentence,
00116 const ChartCellCollectionBase &cellCollection)
00117 {
00118 return new ChartRuleLookupManagerMemory(sentence, cellCollection, *this);
00119 }
00120
00121 void PhraseDictionarySCFG::SortAndPrune()
00122 {
00123 if (GetTableLimit())
00124 {
00125 m_collection.Sort(GetTableLimit());
00126 }
00127 }
00128
00129 TO_STRING_BODY(PhraseDictionarySCFG);
00130
00131
00132 ostream& operator<<(ostream& out, const PhraseDictionarySCFG& phraseDict)
00133 {
00134 typedef PhraseDictionaryNodeSCFG::TerminalMap TermMap;
00135 typedef PhraseDictionaryNodeSCFG::NonTerminalMap NonTermMap;
00136
00137 const PhraseDictionaryNodeSCFG &coll = phraseDict.m_collection;
00138 for (NonTermMap::const_iterator p = coll.m_nonTermMap.begin(); p != coll.m_nonTermMap.end(); ++p) {
00139 const Word &sourceNonTerm = p->first.first;
00140 out << sourceNonTerm;
00141 }
00142 for (TermMap::const_iterator p = coll.m_sourceTermMap.begin(); p != coll.m_sourceTermMap.end(); ++p) {
00143 const Word &sourceTerm = p->first;
00144 out << sourceTerm;
00145 }
00146 return out;
00147 }
00148
00149 }