00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "ChartTrellisNode.h"
00022
00023 #include "ChartHypothesis.h"
00024 #include "ChartTrellisDetour.h"
00025 #include "ChartTrellisPath.h"
00026 #include "StaticData.h"
00027
00028 namespace Moses
00029 {
00030
00031 ChartTrellisNode::ChartTrellisNode(const ChartHypothesis &hypo)
00032 : m_hypo(hypo)
00033 {
00034 CreateChildren();
00035 }
00036
00037 ChartTrellisNode::ChartTrellisNode(const ChartTrellisDetour &detour,
00038 ChartTrellisNode *&deviationPoint)
00039 : m_hypo((&detour.GetBasePath().GetFinalNode() == &detour.GetSubstitutedNode())
00040 ? detour.GetReplacementHypo()
00041 : detour.GetBasePath().GetFinalNode().GetHypothesis())
00042 {
00043 if (&m_hypo == &detour.GetReplacementHypo()) {
00044 deviationPoint = this;
00045 CreateChildren();
00046 } else {
00047 CreateChildren(detour.GetBasePath().GetFinalNode(),
00048 detour.GetSubstitutedNode(), detour.GetReplacementHypo(),
00049 deviationPoint);
00050 }
00051 }
00052
00053 ChartTrellisNode::ChartTrellisNode(const ChartTrellisNode &root,
00054 const ChartTrellisNode &substitutedNode,
00055 const ChartHypothesis &replacementHypo,
00056 ChartTrellisNode *&deviationPoint)
00057 : m_hypo((&root == &substitutedNode)
00058 ? replacementHypo
00059 : root.GetHypothesis())
00060 {
00061 if (&root == &substitutedNode) {
00062 deviationPoint = this;
00063 CreateChildren();
00064 } else {
00065 CreateChildren(root, substitutedNode, replacementHypo, deviationPoint);
00066 }
00067 }
00068
00069 ChartTrellisNode::~ChartTrellisNode()
00070 {
00071 RemoveAllInColl(m_children);
00072 }
00073
00074 Phrase ChartTrellisNode::GetOutputPhrase() const
00075 {
00076
00077 Phrase ret(ARRAY_SIZE_INCR);
00078
00079 const Phrase &currTargetPhrase = m_hypo.GetCurrTargetPhrase();
00080 const AlignmentInfo::NonTermIndexMap &nonTermIndexMap =
00081 m_hypo.GetCurrTargetPhrase().GetAlignNonTerm().GetNonTermIndexMap();
00082 for (size_t pos = 0; pos < currTargetPhrase.GetSize(); ++pos) {
00083 const Word &word = currTargetPhrase.GetWord(pos);
00084 if (word.IsNonTerminal()) {
00085
00086 size_t nonTermInd = nonTermIndexMap[pos];
00087 const ChartTrellisNode &childNode = GetChild(nonTermInd);
00088 Phrase childPhrase = childNode.GetOutputPhrase();
00089 ret.Append(childPhrase);
00090 } else {
00091 ret.AddWord(word);
00092 }
00093 }
00094
00095 return ret;
00096 }
00097
00098 void ChartTrellisNode::CreateChildren()
00099 {
00100 CHECK(m_children.empty());
00101 const std::vector<const ChartHypothesis*> &prevHypos = m_hypo.GetPrevHypos();
00102 m_children.reserve(prevHypos.size());
00103 for (size_t ind = 0; ind < prevHypos.size(); ++ind) {
00104 const ChartHypothesis *prevHypo = prevHypos[ind];
00105 ChartTrellisNode *child = new ChartTrellisNode(*prevHypo);
00106 m_children.push_back(child);
00107 }
00108 }
00109
00110 void ChartTrellisNode::CreateChildren(const ChartTrellisNode &rootNode,
00111 const ChartTrellisNode &substitutedNode,
00112 const ChartHypothesis &replacementHypo,
00113 ChartTrellisNode *&deviationPoint)
00114 {
00115 CHECK(m_children.empty());
00116 const NodeChildren &children = rootNode.GetChildren();
00117 m_children.reserve(children.size());
00118 for (size_t ind = 0; ind < children.size(); ++ind) {
00119 const ChartTrellisNode *origChild = children[ind];
00120 ChartTrellisNode *child = new ChartTrellisNode(*origChild, substitutedNode,
00121 replacementHypo,
00122 deviationPoint);
00123 m_children.push_back(child);
00124 }
00125 }
00126
00127 }