00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "PhraseDictionaryNodeSCFG.h"
00023 #include "moses/TargetPhrase.h"
00024 #include "moses/TranslationModel/PhraseDictionary.h"
00025
00026 using namespace std;
00027
00028 namespace Moses
00029 {
00030
00031 PhraseDictionaryNodeSCFG::~PhraseDictionaryNodeSCFG()
00032 {
00033 for (TerminalMap::iterator iter = m_sourceTermMap.begin(); iter != m_sourceTermMap.end(); ++iter) {
00034 const PhraseDictionaryNodeSCFG *node = iter->second;
00035 delete node;
00036 }
00037 for (NonTerminalMap::iterator iter = m_nonTermMap.begin(); iter != m_nonTermMap.end(); ++iter) {
00038 const PhraseDictionaryNodeSCFG *node = iter->second;
00039 delete node;
00040 }
00041 delete m_targetPhraseCollection;
00042 }
00043
00044 TargetPhraseCollection &PhraseDictionaryNodeSCFG::GetOrCreateTargetPhraseCollection() {
00045 if (m_targetPhraseCollection == NULL)
00046 m_targetPhraseCollection = new TargetPhraseCollection();
00047 return *m_targetPhraseCollection;
00048 }
00049
00050 void PhraseDictionaryNodeSCFG::Prune(size_t tableLimit)
00051 {
00052
00053 for (TerminalMap::iterator p = m_sourceTermMap.begin(); p != m_sourceTermMap.end(); ++p) {
00054 p->second->Prune(tableLimit);
00055 }
00056 for (NonTerminalMap::iterator p = m_nonTermMap.begin(); p != m_nonTermMap.end(); ++p) {
00057 p->second->Prune(tableLimit);
00058 }
00059
00060
00061 if (m_targetPhraseCollection != NULL)
00062 m_targetPhraseCollection->Prune(true, tableLimit);
00063 }
00064
00065 void PhraseDictionaryNodeSCFG::Sort(size_t tableLimit)
00066 {
00067
00068 for (TerminalMap::iterator p = m_sourceTermMap.begin(); p != m_sourceTermMap.end(); ++p) {
00069 p->second->Sort(tableLimit);
00070 }
00071 for (NonTerminalMap::iterator p = m_nonTermMap.begin(); p != m_nonTermMap.end(); ++p) {
00072 p->second->Sort(tableLimit);
00073 }
00074
00075
00076 if (m_targetPhraseCollection != NULL) {
00077 m_targetPhraseCollection->Sort(true, tableLimit);
00078 }
00079 }
00080
00081 PhraseDictionaryNodeSCFG *PhraseDictionaryNodeSCFG::GetOrCreateChild(const Word &sourceTerm)
00082 {
00083
00084
00085 std::pair <TerminalMap::iterator,bool> insResult;
00086 PhraseDictionaryNodeSCFG *node = new PhraseDictionaryNodeSCFG();
00087 insResult = m_sourceTermMap.insert( std::make_pair(sourceTerm, node) );
00088
00089 if (!insResult.second) {
00090 delete node;
00091 }
00092
00093 const TerminalMap::iterator &iter = insResult.first;
00094 PhraseDictionaryNodeSCFG &ret = *iter->second;
00095 return &ret;
00096 }
00097
00098 PhraseDictionaryNodeSCFG *PhraseDictionaryNodeSCFG::GetOrCreateChild(const Word &sourceNonTerm, const Word &targetNonTerm)
00099 {
00100 CHECK(sourceNonTerm.IsNonTerminal());
00101 CHECK(targetNonTerm.IsNonTerminal());
00102
00103 NonTerminalMapKey key(sourceNonTerm, targetNonTerm);
00104 std::pair <NonTerminalMap::iterator,bool> insResult;
00105 PhraseDictionaryNodeSCFG *node = new PhraseDictionaryNodeSCFG();
00106
00107 insResult = m_nonTermMap.insert( std::make_pair(key, node) );
00108
00109 if (!insResult.second) {
00110 delete node;
00111 }
00112
00113 const NonTerminalMap::iterator &iter = insResult.first;
00114 PhraseDictionaryNodeSCFG &ret = *iter->second;
00115 return &ret;
00116 }
00117
00118 const PhraseDictionaryNodeSCFG *PhraseDictionaryNodeSCFG::GetChild(const Word &sourceTerm) const
00119 {
00120 CHECK(!sourceTerm.IsNonTerminal());
00121
00122 TerminalMap::const_iterator p = m_sourceTermMap.find(sourceTerm);
00123 return (p == m_sourceTermMap.end()) ? NULL : p->second;
00124 }
00125
00126 const PhraseDictionaryNodeSCFG *PhraseDictionaryNodeSCFG::GetChild(const Word &sourceNonTerm, const Word &targetNonTerm) const
00127 {
00128 CHECK(sourceNonTerm.IsNonTerminal());
00129 CHECK(targetNonTerm.IsNonTerminal());
00130
00131 NonTerminalMapKey key(sourceNonTerm, targetNonTerm);
00132 NonTerminalMap::const_iterator p = m_nonTermMap.find(key);
00133 return (p == m_nonTermMap.end()) ? NULL : p->second;
00134 }
00135
00136 void PhraseDictionaryNodeSCFG::Clear()
00137 {
00138 m_sourceTermMap.clear();
00139 m_nonTermMap.clear();
00140 delete m_targetPhraseCollection;
00141
00142 }
00143
00144 std::ostream& operator<<(std::ostream &out, const PhraseDictionaryNodeSCFG &node)
00145 {
00146 out << node.GetTargetPhraseCollection();
00147 return out;
00148 }
00149
00150 TO_STRING_BODY(PhraseDictionaryNodeSCFG)
00151
00152 }
00153