diff options
author | Ulrich Germann <Ulrich.Germann@gmail.com> | 2015-10-18 23:27:58 +0300 |
---|---|---|
committer | Ulrich Germann <Ulrich.Germann@gmail.com> | 2015-10-18 23:27:58 +0300 |
commit | bdb0227ee9648ea96e8ee266d32581c63762b8b0 (patch) | |
tree | 5aff9707a743b3ec5f0e16cba70771b7299516f0 /moses/Syntax | |
parent | 7a85126a926c4ffdbd0a956b3b4cec07eea33ed2 (diff) |
Life cycle of TargetPhraseCollection is now managed via shared pointers.
Diffstat (limited to 'moses/Syntax')
26 files changed, 148 insertions, 104 deletions
diff --git a/moses/Syntax/F2S/GlueRuleSynthesizer.cpp b/moses/Syntax/F2S/GlueRuleSynthesizer.cpp index 1e00c594a..e83daa81b 100644 --- a/moses/Syntax/F2S/GlueRuleSynthesizer.cpp +++ b/moses/Syntax/F2S/GlueRuleSynthesizer.cpp @@ -28,9 +28,9 @@ void GlueRuleSynthesizer::SynthesizeRule(const Forest::Hyperedge &e) HyperPath source; SynthesizeHyperPath(e, source); TargetPhrase *tp = SynthesizeTargetPhrase(e); - TargetPhraseCollection &tpc = GetOrCreateTargetPhraseCollection(m_hyperTree, - source); - tpc.Add(tp); + TargetPhraseCollection::shared_ptr tpc + = GetOrCreateTargetPhraseCollection(m_hyperTree, source); + tpc->Add(tp); } void GlueRuleSynthesizer::SynthesizeHyperPath(const Forest::Hyperedge &e, diff --git a/moses/Syntax/F2S/HyperTree.cpp b/moses/Syntax/F2S/HyperTree.cpp index a7d77eb4c..681ec561c 100644 --- a/moses/Syntax/F2S/HyperTree.cpp +++ b/moses/Syntax/F2S/HyperTree.cpp @@ -14,7 +14,7 @@ void HyperTree::Node::Prune(std::size_t tableLimit) p->second.Prune(tableLimit); } // Prune TargetPhraseCollection at this node. - m_targetPhraseCollection.Prune(true, tableLimit); + m_targetPhraseCollection->Prune(true, tableLimit); } void HyperTree::Node::Sort(std::size_t tableLimit) @@ -24,7 +24,7 @@ void HyperTree::Node::Sort(std::size_t tableLimit) p->second.Sort(tableLimit); } // Sort TargetPhraseCollection at this node. - m_targetPhraseCollection.Sort(true, tableLimit); + m_targetPhraseCollection->Sort(true, tableLimit); } HyperTree::Node *HyperTree::Node::GetOrCreateChild( @@ -40,7 +40,7 @@ const HyperTree::Node *HyperTree::Node::GetChild( return (p == m_map.end()) ? NULL : &p->second; } -TargetPhraseCollection &HyperTree::GetOrCreateTargetPhraseCollection( +TargetPhraseCollection::shared_ptr HyperTree::GetOrCreateTargetPhraseCollection( const HyperPath &hyperPath) { Node &node = GetOrCreateNode(hyperPath); diff --git a/moses/Syntax/F2S/HyperTree.h b/moses/Syntax/F2S/HyperTree.h index e32bfebbe..a000e9f9b 100644 --- a/moses/Syntax/F2S/HyperTree.h +++ b/moses/Syntax/F2S/HyperTree.h @@ -37,7 +37,7 @@ public: } bool HasRules() const { - return !m_targetPhraseCollection.IsEmpty(); + return !m_targetPhraseCollection->IsEmpty(); } void Prune(std::size_t tableLimit); @@ -47,11 +47,13 @@ public: const Node *GetChild(const HyperPath::NodeSeq &) const; - const TargetPhraseCollection &GetTargetPhraseCollection() const { + TargetPhraseCollection::shared_ptr + GetTargetPhraseCollection() const { return m_targetPhraseCollection; } - TargetPhraseCollection &GetTargetPhraseCollection() { + TargetPhraseCollection::shared_ptr + GetTargetPhraseCollection() { return m_targetPhraseCollection; } @@ -59,12 +61,14 @@ public: return m_map; } + Node() : m_targetPhraseCollection(new TargetPhraseCollection) { } + private: Map m_map; - TargetPhraseCollection m_targetPhraseCollection; + TargetPhraseCollection::shared_ptr m_targetPhraseCollection; }; - HyperTree(const RuleTableFF *ff) : RuleTable(ff) {} + HyperTree(const RuleTableFF *ff) : RuleTable(ff) { } const Node &GetRootNode() const { return m_root; @@ -73,7 +77,8 @@ public: private: friend class HyperTreeCreator; - TargetPhraseCollection &GetOrCreateTargetPhraseCollection(const HyperPath &); + TargetPhraseCollection::shared_ptr + GetOrCreateTargetPhraseCollection(const HyperPath &); Node &GetOrCreateNode(const HyperPath &); diff --git a/moses/Syntax/F2S/HyperTreeCreator.h b/moses/Syntax/F2S/HyperTreeCreator.h index 04dbaa6b1..a5111b90e 100644 --- a/moses/Syntax/F2S/HyperTreeCreator.h +++ b/moses/Syntax/F2S/HyperTreeCreator.h @@ -21,7 +21,7 @@ protected: // Provide access to HyperTree's private GetOrCreateTargetPhraseCollection // function. - TargetPhraseCollection &GetOrCreateTargetPhraseCollection( + TargetPhraseCollection::shared_ptr GetOrCreateTargetPhraseCollection( HyperTree &trie, const HyperPath &fragment) { return trie.GetOrCreateTargetPhraseCollection(fragment); } diff --git a/moses/Syntax/F2S/HyperTreeLoader.cpp b/moses/Syntax/F2S/HyperTreeLoader.cpp index 21d5b0447..6764963ac 100644 --- a/moses/Syntax/F2S/HyperTreeLoader.cpp +++ b/moses/Syntax/F2S/HyperTreeLoader.cpp @@ -130,9 +130,9 @@ bool HyperTreeLoader::Load(const std::vector<FactorType> &input, ff.GetFeaturesToApply()); // Add rule to trie. - TargetPhraseCollection &phraseColl = GetOrCreateTargetPhraseCollection( - trie, sourceFragment); - phraseColl.Add(targetPhrase); + TargetPhraseCollection::shared_ptr phraseColl + = GetOrCreateTargetPhraseCollection(trie, sourceFragment); + phraseColl->Add(targetPhrase); count++; } diff --git a/moses/Syntax/F2S/RuleMatcherHyperTree-inl.h b/moses/Syntax/F2S/RuleMatcherHyperTree-inl.h index 74f2347a6..bf05d4bcc 100644 --- a/moses/Syntax/F2S/RuleMatcherHyperTree-inl.h +++ b/moses/Syntax/F2S/RuleMatcherHyperTree-inl.h @@ -51,8 +51,8 @@ void RuleMatcherHyperTree<Callback>::EnumerateHyperedges( m_hyperedge.label.inputWeight += (*p)->weight; } // Set the output hyperedge label's translation set pointer. - m_hyperedge.label.translations = - &(item.trieNode->GetTargetPhraseCollection()); + m_hyperedge.label.translations + = item.trieNode->GetTargetPhraseCollection(); // Pass the output hyperedge to the callback. callback(m_hyperedge); } diff --git a/moses/Syntax/PLabel.h b/moses/Syntax/PLabel.h index 4537b86bb..c45f40dfd 100644 --- a/moses/Syntax/PLabel.h +++ b/moses/Syntax/PLabel.h @@ -9,7 +9,7 @@ namespace Syntax struct PLabel { float inputWeight; - const TargetPhraseCollection *translations; + TargetPhraseCollection::shared_ptr translations; }; } // Syntax diff --git a/moses/Syntax/S2T/OovHandler-inl.h b/moses/Syntax/S2T/OovHandler-inl.h index 76eed861e..e5ffe6370 100644 --- a/moses/Syntax/S2T/OovHandler-inl.h +++ b/moses/Syntax/S2T/OovHandler-inl.h @@ -32,9 +32,10 @@ boost::shared_ptr<RuleTrie> OovHandler<RuleTrie>::SynthesizeRuleTrie( // TODO Check ownership and fix any leaks. Word *tgtLHS = SynthesizeTargetLhs(targetLhsStr); TargetPhrase *tp = SynthesizeTargetPhrase(oov, *srcPhrase, *tgtLHS, prob); - TargetPhraseCollection &tpc = GetOrCreateTargetPhraseCollection( - *trie, *srcPhrase, *tp, NULL); // TODO Check NULL is valid argument - tpc.Add(tp); + TargetPhraseCollection::shared_ptr tpc; + tpc= GetOrCreateTargetPhraseCollection(*trie, *srcPhrase, *tp, NULL); + // TODO Check NULL is valid argument + tpc->Add(tp); } } diff --git a/moses/Syntax/S2T/Parsers/RecursiveCYKPlusParser/RecursiveCYKPlusParser-inl.h b/moses/Syntax/S2T/Parsers/RecursiveCYKPlusParser/RecursiveCYKPlusParser-inl.h index a84e16a54..c8f57c4d1 100644 --- a/moses/Syntax/S2T/Parsers/RecursiveCYKPlusParser/RecursiveCYKPlusParser-inl.h +++ b/moses/Syntax/S2T/Parsers/RecursiveCYKPlusParser/RecursiveCYKPlusParser-inl.h @@ -132,9 +132,9 @@ void RecursiveCYKPlusParser<Callback>::AddAndExtend( m_hyperedge.tail.push_back(const_cast<PVertex *>(&vertex)); // Add target phrase collection (except if rule is empty or unary). - const TargetPhraseCollection &tpc = node.GetTargetPhraseCollection(); - if (!tpc.IsEmpty() && !IsNonLexicalUnary(m_hyperedge)) { - m_hyperedge.label.translations = &tpc; + TargetPhraseCollection::shared_ptr tpc = node.GetTargetPhraseCollection(); + if (!tpc->IsEmpty() && !IsNonLexicalUnary(m_hyperedge)) { + m_hyperedge.label.translations = tpc; (*m_callback)(m_hyperedge, end); } diff --git a/moses/Syntax/S2T/Parsers/Scope3Parser/Parser-inl.h b/moses/Syntax/S2T/Parsers/Scope3Parser/Parser-inl.h index 24135c734..da81a5606 100644 --- a/moses/Syntax/S2T/Parsers/Scope3Parser/Parser-inl.h +++ b/moses/Syntax/S2T/Parsers/Scope3Parser/Parser-inl.h @@ -38,8 +38,8 @@ Scope3Parser<Callback>::~Scope3Parser() } template<typename Callback> -void Scope3Parser<Callback>::EnumerateHyperedges(const WordsRange &range, - Callback &callback) +void Scope3Parser<Callback>:: +EnumerateHyperedges(const WordsRange &range, Callback &callback) { const std::size_t start = range.GetStartPos(); const std::size_t end = range.GetEndPos(); @@ -64,8 +64,7 @@ void Scope3Parser<Callback>::EnumerateHyperedges(const WordsRange &range, // Ask the grammar for the mapping from label sequences to target phrase // collections for this pattern. - const RuleTrie::Node::LabelMap &labelMap = - patNode->m_node->GetLabelMap(); + const RuleTrie::Node::LabelMap &labelMap = patNode->m_node->GetLabelMap(); // For each label sequence, search the lattice for the set of PHyperedge // tails. @@ -73,7 +72,7 @@ void Scope3Parser<Callback>::EnumerateHyperedges(const WordsRange &range, RuleTrie::Node::LabelMap::const_iterator q = labelMap.begin(); for (; q != labelMap.end(); ++q) { const std::vector<int> &labelSeq = q->first; - const TargetPhraseCollection &tpc = q->second; + TargetPhraseCollection::shared_ptr tpc = q->second; // For many label sequences there won't be any corresponding paths through // the lattice. As an optimisation, we use m_quickCheckTable to test // for this and we don't begin a search if there are no paths to find. diff --git a/moses/Syntax/S2T/Parsers/Scope3Parser/TailLatticeSearcher.h b/moses/Syntax/S2T/Parsers/Scope3Parser/TailLatticeSearcher.h index 4f815c78d..407f04d5b 100644 --- a/moses/Syntax/S2T/Parsers/Scope3Parser/TailLatticeSearcher.h +++ b/moses/Syntax/S2T/Parsers/Scope3Parser/TailLatticeSearcher.h @@ -6,7 +6,7 @@ #include "moses/Syntax/PHyperedge.h" #include "TailLattice.h" - +#include "moses/TargetPhraseCollection.h" namespace Moses { namespace Syntax @@ -25,13 +25,14 @@ public: , m_key(key) , m_ranges(ranges) {} - void Search(const std::vector<int> &labels, const TargetPhraseCollection &tpc, + void Search(const std::vector<int> &labels, + const TargetPhraseCollection::shared_ptr tpc, Callback &callback) { m_labels = &labels; m_matchCB = &callback; m_hyperedge.head = 0; m_hyperedge.tail.clear(); - m_hyperedge.label.translations = &tpc; + m_hyperedge.label.translations = tpc; SearchInner(0, 0, 0); } diff --git a/moses/Syntax/S2T/RuleTrie.h b/moses/Syntax/S2T/RuleTrie.h index 27b0bc838..b9d031673 100644 --- a/moses/Syntax/S2T/RuleTrie.h +++ b/moses/Syntax/S2T/RuleTrie.h @@ -28,9 +28,10 @@ public: private: friend class RuleTrieCreator; - virtual TargetPhraseCollection &GetOrCreateTargetPhraseCollection( - const Phrase &source, const TargetPhrase &target, - const Word *sourceLHS) = 0; + virtual TargetPhraseCollection::shared_ptr + GetOrCreateTargetPhraseCollection(const Phrase &source, + const TargetPhrase &target, + const Word *sourceLHS) = 0; virtual void SortAndPrune(std::size_t) = 0; }; diff --git a/moses/Syntax/S2T/RuleTrieCYKPlus.cpp b/moses/Syntax/S2T/RuleTrieCYKPlus.cpp index 05f8758e9..7c8d08864 100644 --- a/moses/Syntax/S2T/RuleTrieCYKPlus.cpp +++ b/moses/Syntax/S2T/RuleTrieCYKPlus.cpp @@ -33,7 +33,7 @@ void RuleTrieCYKPlus::Node::Prune(std::size_t tableLimit) } // prune TargetPhraseCollection in this node - m_targetPhraseCollection.Prune(true, tableLimit); + m_targetPhraseCollection->Prune(true, tableLimit); } void RuleTrieCYKPlus::Node::Sort(std::size_t tableLimit) @@ -49,7 +49,7 @@ void RuleTrieCYKPlus::Node::Sort(std::size_t tableLimit) } // prune TargetPhraseCollection in this node - m_targetPhraseCollection.Sort(true, tableLimit); + m_targetPhraseCollection->Sort(true, tableLimit); } RuleTrieCYKPlus::Node *RuleTrieCYKPlus::Node::GetOrCreateChild( @@ -86,8 +86,11 @@ const RuleTrieCYKPlus::Node *RuleTrieCYKPlus::Node::GetNonTerminalChild( return (p == m_nonTermMap.end()) ? NULL : &p->second; } -TargetPhraseCollection &RuleTrieCYKPlus::GetOrCreateTargetPhraseCollection( - const Phrase &source, const TargetPhrase &target, const Word *sourceLHS) +TargetPhraseCollection::shared_ptr +RuleTrieCYKPlus:: +GetOrCreateTargetPhraseCollection(const Phrase &source, + const TargetPhrase &target, + const Word *sourceLHS) { Node &currNode = GetOrCreateNode(source, target, sourceLHS); return currNode.GetTargetPhraseCollection(); diff --git a/moses/Syntax/S2T/RuleTrieCYKPlus.h b/moses/Syntax/S2T/RuleTrieCYKPlus.h index 11cf4c199..0c11a1edb 100644 --- a/moses/Syntax/S2T/RuleTrieCYKPlus.h +++ b/moses/Syntax/S2T/RuleTrieCYKPlus.h @@ -38,7 +38,7 @@ public: } bool HasRules() const { - return !m_targetPhraseCollection.IsEmpty(); + return !m_targetPhraseCollection->IsEmpty(); } void Prune(std::size_t tableLimit); @@ -50,11 +50,13 @@ public: const Node *GetChild(const Word &sourceTerm) const; const Node *GetNonTerminalChild(const Word &targetNonTerm) const; - const TargetPhraseCollection &GetTargetPhraseCollection() const { + TargetPhraseCollection::shared_ptr + GetTargetPhraseCollection() const { return m_targetPhraseCollection; } - TargetPhraseCollection &GetTargetPhraseCollection() { + TargetPhraseCollection::shared_ptr + GetTargetPhraseCollection() { return m_targetPhraseCollection; } @@ -66,10 +68,12 @@ public: return m_nonTermMap; } + Node() : m_targetPhraseCollection(new TargetPhraseCollection) {} + private: SymbolMap m_sourceTermMap; SymbolMap m_nonTermMap; - TargetPhraseCollection m_targetPhraseCollection; + TargetPhraseCollection::shared_ptr m_targetPhraseCollection; }; RuleTrieCYKPlus(const RuleTableFF *ff) : RuleTrie(ff) {} @@ -81,8 +85,9 @@ public: bool HasPreterminalRule(const Word &) const; private: - TargetPhraseCollection &GetOrCreateTargetPhraseCollection( - const Phrase &source, const TargetPhrase &target, const Word *sourceLHS); + TargetPhraseCollection::shared_ptr + GetOrCreateTargetPhraseCollection + (const Phrase &source, const TargetPhrase &target, const Word *sourceLHS); Node &GetOrCreateNode(const Phrase &source, const TargetPhrase &target, const Word *sourceLHS); diff --git a/moses/Syntax/S2T/RuleTrieCreator.h b/moses/Syntax/S2T/RuleTrieCreator.h index e49a2cbde..62da519a1 100644 --- a/moses/Syntax/S2T/RuleTrieCreator.h +++ b/moses/Syntax/S2T/RuleTrieCreator.h @@ -21,8 +21,9 @@ protected: // Provide access to RuleTrie's private GetOrCreateTargetPhraseCollection // function. - TargetPhraseCollection &GetOrCreateTargetPhraseCollection( - RuleTrie &trie, const Phrase &source, const TargetPhrase &target, + TargetPhraseCollection::shared_ptr + GetOrCreateTargetPhraseCollection + ( RuleTrie &trie, const Phrase &source, const TargetPhrase &target, const Word *sourceLHS) { return trie.GetOrCreateTargetPhraseCollection(source, target, sourceLHS); } diff --git a/moses/Syntax/S2T/RuleTrieLoader.cpp b/moses/Syntax/S2T/RuleTrieLoader.cpp index a88c0f5fe..b523953c7 100644 --- a/moses/Syntax/S2T/RuleTrieLoader.cpp +++ b/moses/Syntax/S2T/RuleTrieLoader.cpp @@ -125,9 +125,10 @@ bool RuleTrieLoader::Load(const std::vector<FactorType> &input, targetPhrase->GetScoreBreakdown().Assign(&ff, scoreVector); targetPhrase->EvaluateInIsolation(sourcePhrase, ff.GetFeaturesToApply()); - TargetPhraseCollection &phraseColl = GetOrCreateTargetPhraseCollection( - trie, sourcePhrase, *targetPhrase, sourceLHS); - phraseColl.Add(targetPhrase); + TargetPhraseCollection::shared_ptr phraseColl + = GetOrCreateTargetPhraseCollection(trie, sourcePhrase, + *targetPhrase, sourceLHS); + phraseColl->Add(targetPhrase); // not implemented correctly in memory pt. just delete it for now delete sourceLHS; diff --git a/moses/Syntax/S2T/RuleTrieScope3.cpp b/moses/Syntax/S2T/RuleTrieScope3.cpp index 7318f09d6..aecaac3f7 100644 --- a/moses/Syntax/S2T/RuleTrieScope3.cpp +++ b/moses/Syntax/S2T/RuleTrieScope3.cpp @@ -33,7 +33,7 @@ void RuleTrieScope3::Node::Prune(std::size_t tableLimit) // Prune TargetPhraseCollections at this node. for (LabelMap::iterator p = m_labelMap.begin(); p != m_labelMap.end(); ++p) { - p->second.Prune(true, tableLimit); + p->second->Prune(true, tableLimit); } } @@ -50,7 +50,7 @@ void RuleTrieScope3::Node::Sort(std::size_t tableLimit) // Sort TargetPhraseCollections at this node. for (LabelMap::iterator p = m_labelMap.begin(); p != m_labelMap.end(); ++p) { - p->second.Sort(true, tableLimit); + p->second->Sort(true, tableLimit); } } @@ -75,9 +75,10 @@ RuleTrieScope3::Node *RuleTrieScope3::Node::GetOrCreateNonTerminalChild( return m_gapNode; } -TargetPhraseCollection & -RuleTrieScope3::Node::GetOrCreateTargetPhraseCollection( - const TargetPhrase &target) +TargetPhraseCollection::shared_ptr +RuleTrieScope3:: +Node:: +GetOrCreateTargetPhraseCollection(const TargetPhrase &target) { const AlignmentInfo &alignmentInfo = target.GetAlignNonTerm(); const std::size_t rank = alignmentInfo.GetSize(); @@ -94,12 +95,16 @@ RuleTrieScope3::Node::GetOrCreateTargetPhraseCollection( const Word &targetNonTerm = target.GetWord(targetNonTermIndex); vec.push_back(InsertLabel(i++, targetNonTerm)); } - - return m_labelMap[vec]; + TargetPhraseCollection::shared_ptr& ret = m_labelMap[vec]; + if (!ret) ret.reset(new TargetPhraseCollection); + return ret; } -TargetPhraseCollection &RuleTrieScope3::GetOrCreateTargetPhraseCollection( - const Phrase &source, const TargetPhrase &target, const Word *sourceLHS) +TargetPhraseCollection::shared_ptr +RuleTrieScope3:: +GetOrCreateTargetPhraseCollection(const Phrase &source, + const TargetPhrase &target, + const Word *sourceLHS) { Node &currNode = GetOrCreateNode(source, target, sourceLHS); return currNode.GetOrCreateTargetPhraseCollection(target); diff --git a/moses/Syntax/S2T/RuleTrieScope3.h b/moses/Syntax/S2T/RuleTrieScope3.h index 5909b6509..4684e8a78 100644 --- a/moses/Syntax/S2T/RuleTrieScope3.h +++ b/moses/Syntax/S2T/RuleTrieScope3.h @@ -35,7 +35,7 @@ public: SymbolEqualityPred> TerminalMap; typedef boost::unordered_map<std::vector<int>, - TargetPhraseCollection> LabelMap; + TargetPhraseCollection::shared_ptr> LabelMap; ~Node() { delete m_gapNode; @@ -61,8 +61,8 @@ public: Node *GetOrCreateNonTerminalChild(const Word &targetNonTerm); - TargetPhraseCollection &GetOrCreateTargetPhraseCollection( - const TargetPhrase &); + TargetPhraseCollection::shared_ptr + GetOrCreateTargetPhraseCollection(const TargetPhrase &); bool IsLeaf() const { return m_terminalMap.empty() && m_gapNode == NULL; @@ -106,8 +106,10 @@ public: bool HasPreterminalRule(const Word &) const; private: - TargetPhraseCollection &GetOrCreateTargetPhraseCollection( - const Phrase &source, const TargetPhrase &target, const Word *sourceLHS); + TargetPhraseCollection::shared_ptr + GetOrCreateTargetPhraseCollection(const Phrase &source, + const TargetPhrase &target, + const Word *sourceLHS); Node &GetOrCreateNode(const Phrase &source, const TargetPhrase &target, const Word *sourceLHS); diff --git a/moses/Syntax/SHyperedgeBundle.h b/moses/Syntax/SHyperedgeBundle.h index 54eda73bc..d6e903529 100644 --- a/moses/Syntax/SHyperedgeBundle.h +++ b/moses/Syntax/SHyperedgeBundle.h @@ -17,7 +17,7 @@ struct PVertex; struct SHyperedgeBundle { float inputWeight; std::vector<const SVertexStack*> stacks; - const TargetPhraseCollection *translations; + TargetPhraseCollection::shared_ptr translations; friend void swap(SHyperedgeBundle &x, SHyperedgeBundle &y) { using std::swap; diff --git a/moses/Syntax/T2S/GlueRuleSynthesizer.cpp b/moses/Syntax/T2S/GlueRuleSynthesizer.cpp index 0a0c07eea..7514852f2 100644 --- a/moses/Syntax/T2S/GlueRuleSynthesizer.cpp +++ b/moses/Syntax/T2S/GlueRuleSynthesizer.cpp @@ -17,9 +17,9 @@ void GlueRuleSynthesizer::SynthesizeRule(const InputTree::Node &node) const Word &sourceLhs = node.pvertex.symbol; boost::scoped_ptr<Phrase> sourceRhs(SynthesizeSourcePhrase(node)); TargetPhrase *tp = SynthesizeTargetPhrase(node, *sourceRhs); - TargetPhraseCollection &tpc = GetOrCreateTargetPhraseCollection( - m_ruleTrie, sourceLhs, *sourceRhs); - tpc.Add(tp); + TargetPhraseCollection::shared_ptr tpc + = GetOrCreateTargetPhraseCollection(m_ruleTrie, sourceLhs, *sourceRhs); + tpc->Add(tp); } Phrase *GlueRuleSynthesizer::SynthesizeSourcePhrase(const InputTree::Node &node) diff --git a/moses/Syntax/T2S/HyperTree.h b/moses/Syntax/T2S/HyperTree.h index 800700365..66e7a7de6 100644 --- a/moses/Syntax/T2S/HyperTree.h +++ b/moses/Syntax/T2S/HyperTree.h @@ -48,11 +48,11 @@ public: const Node *GetChild(const HyperPath::NodeSeq &) const; - const TargetPhraseCollection &GetTargetPhraseCollection() const + const TargetPhraseCollection::shared_ptr GetTargetPhraseCollection() const return m_targetPhraseCollection; } - TargetPhraseCollection &GetTargetPhraseCollection() + TargetPhraseCollection::shared_ptr GetTargetPhraseCollection() return m_targetPhraseCollection; } @@ -76,7 +76,7 @@ const Node &GetRootNode() const private: friend class RuleTrieCreator; -TargetPhraseCollection &GetOrCreateTargetPhraseCollection( +TargetPhraseCollection::shared_ptr GetOrCreateTargetPhraseCollection( const Word &sourceLHS, const Phrase &sourceRHS); Node &GetOrCreateNode(const Phrase &sourceRHS); diff --git a/moses/Syntax/T2S/RuleMatcherSCFG-inl.h b/moses/Syntax/T2S/RuleMatcherSCFG-inl.h index b782411a4..0eb7cbe2c 100644 --- a/moses/Syntax/T2S/RuleMatcherSCFG-inl.h +++ b/moses/Syntax/T2S/RuleMatcherSCFG-inl.h @@ -61,7 +61,7 @@ void RuleMatcherSCFG<Callback>::Match(const InputTree::Node &inNode, if (candidate.pvertex.span.GetEndPos() == inNode.pvertex.span.GetEndPos()) { // Check if the trie node has any rules with a LHS that match inNode. const Word &lhs = inNode.pvertex.symbol; - const TargetPhraseCollection *tpc = + TargetPhraseCollection::shared_ptr tpc = newTrieNode.GetTargetPhraseCollection(lhs); if (tpc) { m_hyperedge.label.translations = tpc; diff --git a/moses/Syntax/T2S/RuleTrie.cpp b/moses/Syntax/T2S/RuleTrie.cpp index 0fc7bf24c..e6fc5214c 100644 --- a/moses/Syntax/T2S/RuleTrie.cpp +++ b/moses/Syntax/T2S/RuleTrie.cpp @@ -35,7 +35,7 @@ void RuleTrie::Node::Prune(std::size_t tableLimit) // Prune TargetPhraseCollections at this node. for (TPCMap::iterator p = m_targetPhraseCollections.begin(); p != m_targetPhraseCollections.end(); ++p) { - p->second.Prune(true, tableLimit); + p->second->Prune(true, tableLimit); } } @@ -54,17 +54,21 @@ void RuleTrie::Node::Sort(std::size_t tableLimit) // Sort TargetPhraseCollections at this node. for (TPCMap::iterator p = m_targetPhraseCollections.begin(); p != m_targetPhraseCollections.end(); ++p) { - p->second.Sort(true, tableLimit); + p->second->Sort(true, tableLimit); } } -RuleTrie::Node *RuleTrie::Node::GetOrCreateChild( - const Word &sourceTerm) +RuleTrie::Node* +RuleTrie::Node:: +GetOrCreateChild(const Word &sourceTerm) { return &m_sourceTermMap[sourceTerm]; } -RuleTrie::Node *RuleTrie::Node::GetOrCreateNonTerminalChild(const Word &targetNonTerm) +RuleTrie::Node * +RuleTrie:: +Node:: +GetOrCreateNonTerminalChild(const Word &targetNonTerm) { UTIL_THROW_IF2(!targetNonTerm.IsNonTerminal(), "Not a non-terminal: " << targetNonTerm); @@ -72,42 +76,52 @@ RuleTrie::Node *RuleTrie::Node::GetOrCreateNonTerminalChild(const Word &targetNo return &m_nonTermMap[targetNonTerm]; } -TargetPhraseCollection &RuleTrie::Node::GetOrCreateTargetPhraseCollection( - const Word &sourceLHS) +TargetPhraseCollection::shared_ptr +RuleTrie:: +Node:: +GetOrCreateTargetPhraseCollection(const Word &sourceLHS) { UTIL_THROW_IF2(!sourceLHS.IsNonTerminal(), "Not a non-terminal: " << sourceLHS); - return m_targetPhraseCollections[sourceLHS]; + TargetPhraseCollection::shared_ptr& foo + = m_targetPhraseCollections[sourceLHS]; + if (!foo) foo.reset(new TargetPhraseCollection); + return foo; } -const RuleTrie::Node *RuleTrie::Node::GetChild( - const Word &sourceTerm) const +RuleTrie::Node const* +RuleTrie:: +Node:: +GetChild(const Word &sourceTerm) const { - UTIL_THROW_IF2(sourceTerm.IsNonTerminal(), - "Not a terminal: " << sourceTerm); - + UTIL_THROW_IF2(sourceTerm.IsNonTerminal(), "Not a terminal: " << sourceTerm); SymbolMap::const_iterator p = m_sourceTermMap.find(sourceTerm); return (p == m_sourceTermMap.end()) ? NULL : &p->second; } -const RuleTrie::Node *RuleTrie::Node::GetNonTerminalChild( - const Word &targetNonTerm) const +RuleTrie::Node const* +RuleTrie:: +Node:: +GetNonTerminalChild(const Word &targetNonTerm) const { UTIL_THROW_IF2(!targetNonTerm.IsNonTerminal(), "Not a non-terminal: " << targetNonTerm); - SymbolMap::const_iterator p = m_nonTermMap.find(targetNonTerm); return (p == m_nonTermMap.end()) ? NULL : &p->second; } -TargetPhraseCollection &RuleTrie::GetOrCreateTargetPhraseCollection( - const Word &sourceLHS, const Phrase &sourceRHS) +TargetPhraseCollection::shared_ptr +RuleTrie:: +GetOrCreateTargetPhraseCollection +( const Word &sourceLHS, const Phrase &sourceRHS ) { Node &currNode = GetOrCreateNode(sourceRHS); return currNode.GetOrCreateTargetPhraseCollection(sourceLHS); } -RuleTrie::Node &RuleTrie::GetOrCreateNode(const Phrase &sourceRHS) +RuleTrie::Node & +RuleTrie:: +GetOrCreateNode(const Phrase &sourceRHS) { const std::size_t size = sourceRHS.GetSize(); diff --git a/moses/Syntax/T2S/RuleTrie.h b/moses/Syntax/T2S/RuleTrie.h index 2807f6e0e..f9d857088 100644 --- a/moses/Syntax/T2S/RuleTrie.h +++ b/moses/Syntax/T2S/RuleTrie.h @@ -32,7 +32,7 @@ public: typedef boost::unordered_map<Word, Node, SymbolHasher, SymbolEqualityPred> SymbolMap; - typedef boost::unordered_map<Word, TargetPhraseCollection, + typedef boost::unordered_map<Word, TargetPhraseCollection::shared_ptr, SymbolHasher, SymbolEqualityPred> TPCMap; bool IsLeaf() const { @@ -48,15 +48,18 @@ public: Node *GetOrCreateChild(const Word &sourceTerm); Node *GetOrCreateNonTerminalChild(const Word &targetNonTerm); - TargetPhraseCollection &GetOrCreateTargetPhraseCollection(const Word &); + TargetPhraseCollection::shared_ptr GetOrCreateTargetPhraseCollection(const Word &); const Node *GetChild(const Word &sourceTerm) const; const Node *GetNonTerminalChild(const Word &targetNonTerm) const; - const TargetPhraseCollection *GetTargetPhraseCollection( - const Word &sourceLHS) const { + TargetPhraseCollection::shared_ptr + GetTargetPhraseCollection(const Word &sourceLHS) const { TPCMap::const_iterator p = m_targetPhraseCollections.find(sourceLHS); - return p == m_targetPhraseCollections.end() ? 0 : &(p->second); + if (p != m_targetPhraseCollections.end()) + return p->second; + else + return TargetPhraseCollection::shared_ptr(); } // FIXME IS there any reason to distinguish these two for T2S? @@ -83,8 +86,9 @@ public: private: friend class RuleTrieCreator; - TargetPhraseCollection &GetOrCreateTargetPhraseCollection( - const Word &sourceLHS, const Phrase &sourceRHS); + TargetPhraseCollection::shared_ptr + GetOrCreateTargetPhraseCollection + (const Word &sourceLHS, const Phrase &sourceRHS); Node &GetOrCreateNode(const Phrase &sourceRHS); diff --git a/moses/Syntax/T2S/RuleTrieCreator.h b/moses/Syntax/T2S/RuleTrieCreator.h index fd29d3838..af5e976b5 100644 --- a/moses/Syntax/T2S/RuleTrieCreator.h +++ b/moses/Syntax/T2S/RuleTrieCreator.h @@ -21,7 +21,7 @@ protected: // Provide access to RuleTrie's private // GetOrCreateTargetPhraseCollection function. - TargetPhraseCollection &GetOrCreateTargetPhraseCollection( + TargetPhraseCollection::shared_ptr GetOrCreateTargetPhraseCollection( RuleTrie &trie, const Word &sourceLHS, const Phrase &sourceRHS) { return trie.GetOrCreateTargetPhraseCollection(sourceLHS, sourceRHS); } diff --git a/moses/Syntax/T2S/RuleTrieLoader.cpp b/moses/Syntax/T2S/RuleTrieLoader.cpp index 81924f05d..c96c52b03 100644 --- a/moses/Syntax/T2S/RuleTrieLoader.cpp +++ b/moses/Syntax/T2S/RuleTrieLoader.cpp @@ -55,7 +55,9 @@ bool RuleTrieLoader::Load(const std::vector<FactorType> &input, std::vector<float> scoreVector; StringPiece line; - double_conversion::StringToDoubleConverter converter(double_conversion::StringToDoubleConverter::NO_FLAGS, NAN, NAN, "inf", "nan"); + int noflags = double_conversion::StringToDoubleConverter::NO_FLAGS; + double_conversion::StringToDoubleConverter + converter(noflags, NAN, NAN, "inf", "nan"); while(true) { try { @@ -132,9 +134,9 @@ bool RuleTrieLoader::Load(const std::vector<FactorType> &input, targetPhrase->GetScoreBreakdown().Assign(&ff, scoreVector); targetPhrase->EvaluateInIsolation(sourcePhrase, ff.GetFeaturesToApply()); - TargetPhraseCollection &phraseColl = GetOrCreateTargetPhraseCollection( - trie, *sourceLHS, sourcePhrase); - phraseColl.Add(targetPhrase); + TargetPhraseCollection::shared_ptr phraseColl + = GetOrCreateTargetPhraseCollection(trie, *sourceLHS, sourcePhrase); + phraseColl->Add(targetPhrase); // not implemented correctly in memory pt. just delete it for now delete sourceLHS; |