1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
// $Id$
// vim:tabstop=2
/***********************************************************************
Moses - factored phrase-based language decoder
Copyright (C) 2010 Hieu Hoang
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
***********************************************************************/
#include "ChartTrellisPath.h"
#include "ChartHypothesis.h"
#include "ChartTrellisPathCollection.h"
#include "StaticData.h"
#include "Word.h"
using namespace std;
namespace Moses
{
ChartTrellisPath::ChartTrellisPath(const ChartHypothesis *hypo)
:m_finalNode(new ChartTrellisNode(hypo))
,m_scoreBreakdown(hypo->GetScoreBreakdown())
,m_totalScore(hypo->GetTotalScore())
,m_prevNodeChanged(NULL)
,m_prevPath(NULL)
{
}
ChartTrellisPath::ChartTrellisPath(const ChartTrellisPath &origPath
, const ChartTrellisNode &soughtNode
, const ChartHypothesis &replacementHypo
, ScoreComponentCollection &scoreChange)
:m_scoreBreakdown(origPath.GetScoreBreakdown())
,m_prevPath(&origPath)
{
m_finalNode = new ChartTrellisNode(origPath.GetFinalNode()
, soughtNode
, replacementHypo
, scoreChange
, m_prevNodeChanged);
m_scoreBreakdown.PlusEquals(scoreChange);
m_totalScore = m_scoreBreakdown.GetWeightedScore();
}
ChartTrellisPath::~ChartTrellisPath()
{
delete m_finalNode;
}
Phrase ChartTrellisPath::GetOutputPhrase() const
{
Phrase ret = GetFinalNode().GetOutputPhrase();
return ret;
}
void ChartTrellisPath::CreateDeviantPaths(ChartTrellisPathCollection &pathColl, const ChartTrellisNode &soughtNode) const
{
// copy this path but replace startHypo with its arc
const ChartArcList *arcList = soughtNode.GetHypothesis().GetArcList();
if (arcList) {
ChartArcList::const_iterator iterChartArcList;
for (iterChartArcList = arcList->begin(); iterChartArcList != arcList->end(); ++iterChartArcList) {
ScoreComponentCollection scoreChange;
const ChartHypothesis &replacementHypo = **iterChartArcList;
ChartTrellisPath *newPath = new ChartTrellisPath(*this, soughtNode, replacementHypo, scoreChange);
pathColl.Add(newPath);
}
}
// recusively create deviant paths for child nodes
const ChartTrellisNode::NodeChildren &children = soughtNode.GetChildren();
ChartTrellisNode::NodeChildren::const_iterator iter;
for (iter = children.begin(); iter != children.end(); ++iter) {
const ChartTrellisNode &child = **iter;
CreateDeviantPaths(pathColl, child);
}
}
void ChartTrellisPath::CreateDeviantPaths(ChartTrellisPathCollection &pathColl) const
{
if (m_prevNodeChanged == NULL) {
// initial enumeration from a pure hypo
CreateDeviantPaths(pathColl, GetFinalNode());
} else {
// don't change m_prevNodeChanged, just it's children
const ChartTrellisNode::NodeChildren &children = m_prevNodeChanged->GetChildren();
ChartTrellisNode::NodeChildren::const_iterator iter;
for (iter = children.begin(); iter != children.end(); ++iter) {
const ChartTrellisNode &child = **iter;
CreateDeviantPaths(pathColl, child);
}
}
}
std::ostream& operator<<(std::ostream &out, const ChartTrellisPath &path)
{
out << &path << " " << path.m_prevPath << " " << path.GetOutputPhrase() << endl;
if (path.m_prevNodeChanged) {
out << "changed " << path.m_prevNodeChanged->GetHypothesis() << endl;
}
out << path.GetFinalNode() << endl;
return out;
}
};
|