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 "RuleTable/Loader.h"
00027 #include "RuleTable/LoaderFactory.h"
00028 #include "PhraseDictionarySCFG.h"
00029 #include "FactorCollection.h"
00030 #include "Word.h"
00031 #include "Util.h"
00032 #include "InputFileStream.h"
00033 #include "StaticData.h"
00034 #include "WordsRange.h"
00035 #include "UserMessage.h"
00036 #include "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 PhraseDictionaryNodeSCFG &PhraseDictionarySCFG::GetOrCreateNode(const Phrase &source
00053 , const TargetPhrase &target
00054 , const Word &sourceLHS)
00055 {
00056 const size_t size = source.GetSize();
00057
00058 const AlignmentInfo &alignmentInfo = target.GetAlignmentInfo();
00059 AlignmentInfo::const_iterator iterAlign = alignmentInfo.begin();
00060
00061 PhraseDictionaryNodeSCFG *currNode = &m_collection;
00062 for (size_t pos = 0 ; pos < size ; ++pos) {
00063 const Word& word = source.GetWord(pos);
00064
00065 if (word.IsNonTerminal()) {
00066
00067 const Word &sourceNonTerm = word;
00068
00069 CHECK(iterAlign != target.GetAlignmentInfo().end());
00070 CHECK(iterAlign->first == pos);
00071 size_t targetNonTermInd = iterAlign->second;
00072 ++iterAlign;
00073 const Word &targetNonTerm = target.GetWord(targetNonTermInd);
00074
00075 currNode = currNode->GetOrCreateChild(sourceNonTerm, targetNonTerm);
00076 } else {
00077 currNode = currNode->GetOrCreateChild(word);
00078 }
00079
00080 CHECK(currNode != NULL);
00081 }
00082
00083
00084
00085
00086
00087
00088 return *currNode;
00089 }
00090
00091 ChartRuleLookupManager *PhraseDictionarySCFG::CreateRuleLookupManager(
00092 const InputType &sentence,
00093 const ChartCellCollection &cellCollection)
00094 {
00095 return new ChartRuleLookupManagerMemory(sentence, cellCollection, *this);
00096 }
00097
00098 void PhraseDictionarySCFG::SortAndPrune()
00099 {
00100 if (GetTableLimit())
00101 {
00102 m_collection.Sort(GetTableLimit());
00103 }
00104 }
00105
00106 TO_STRING_BODY(PhraseDictionarySCFG);
00107
00108
00109 ostream& operator<<(ostream& out, const PhraseDictionarySCFG& phraseDict)
00110 {
00111 typedef PhraseDictionaryNodeSCFG::TerminalMap TermMap;
00112 typedef PhraseDictionaryNodeSCFG::NonTerminalMap NonTermMap;
00113
00114 const PhraseDictionaryNodeSCFG &coll = phraseDict.m_collection;
00115 for (NonTermMap::const_iterator p = coll.m_nonTermMap.begin(); p != coll.m_nonTermMap.end(); ++p) {
00116 const Word &sourceNonTerm = p->first.first;
00117 out << sourceNonTerm;
00118 }
00119 for (TermMap::const_iterator p = coll.m_sourceTermMap.begin(); p != coll.m_sourceTermMap.end(); ++p) {
00120 const Word &sourceTerm = p->first;
00121 out << sourceTerm;
00122 }
00123 return out;
00124 }
00125
00126 }