Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SyntaxTree.cpp « phrase-extract.5 « training « scripts - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 16eeda2bd7da4c9e0b5b5f61b986073f6129aa91 (plain)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// $Id: SyntaxTree.cpp 1960 2008-12-15 12:52:38Z phkoehn $
// vim:tabstop=2

/***********************************************************************
  Moses - factored phrase-based language decoder
  Copyright (C) 2009 University of Edinburgh

  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 <iostream>
#include <cassert>
#include "SyntaxTree.h"
//#include "extract.h"
#include "Global.h"

//extern const Global g_debug;
extern const Global *g_global;

using namespace std;

bool SyntaxNode::IsSyntax() const
{
	bool ret = GetLabel() != "[X]";
	return ret;
}

SyntaxTree::SyntaxTree() 
:m_defaultLHS(0,0, "[X]")
{
	m_emptyNode.clear();
}

SyntaxTree::~SyntaxTree()
{
	// loop through all m_nodes, delete them
	for(int i=0; i<m_nodes.size(); i++)
	{
		delete m_nodes[i];
	}
}

bool HasDuplicates(const SyntaxNodes &nodes)
{
	string prevLabel;
	SyntaxNodes::const_iterator iter;
	for (iter = nodes.begin(); iter != nodes.end(); ++iter)
	{
		const SyntaxNode &node = **iter;
		string label = node.GetLabel();
		if (label == prevLabel)
			return true;
	}
	return false;
}

void SyntaxTree::AddNode( int startPos, int endPos, std::string label ) 
{	
	SyntaxNode* newNode = new SyntaxNode( startPos, endPos, "[" + label + "]");
	m_nodes.push_back( newNode );
	
	SyntaxNodes &nodesChart = m_index[ startPos ][ endPos ];
	
	if (!g_global->uppermostOnly)
	{
		nodesChart.push_back( newNode );	
		//assert(!HasDuplicates(m_index[ startPos ][ endPos ]));
	}
	else 
	{
		if (nodesChart.size() > 0)
		{
			assert(nodesChart.size() == 1);
			//delete nodes[0];
			nodesChart.resize(0);
		}
		assert(nodesChart.size() == 0);
		nodesChart.push_back( newNode );	
	}
}

ParentNodes SyntaxTree::Parse() {
	ParentNodes parents;

	int size = m_index.size();

	// looping through all spans of size >= 2
	for( int length=2; length<=size; length++ )
	{
		for( int startPos = 0; startPos <= size-length; startPos++ )
		{
			if (HasNode( startPos, startPos+length-1 ))
			{
				// processing one (parent) span

				//std::cerr << "# " << startPos << "-" << (startPos+length-1) << ":";
				SplitPoints splitPoints;
				splitPoints.push_back( startPos );
				//std::cerr << " " << startPos;

				int first = 1;
				int covered = 0;
				while( covered < length )
				{
					// find largest covering subspan (child)
					// starting at last covered position
					for( int midPos=length-first; midPos>covered; midPos-- )
					{
						if( HasNode( startPos+covered, startPos+midPos-1 ) )
						{							
							covered = midPos;							
							splitPoints.push_back( startPos+covered );
							// std::cerr << " " << ( startPos+covered );
							first = 0;
						}
					}
				}
				// std::cerr << std::endl;
				parents.push_back( splitPoints );
			}
		}
	}
	return parents;
}

bool SyntaxTree::HasNode( int startPos, int endPos ) const 
{
	return GetNodes( startPos, endPos).size() > 0;
}

const SyntaxNodes &SyntaxTree::GetNodes( int startPos, int endPos ) const 
{
	SyntaxTreeIndexIterator startIndex = m_index.find( startPos );
	if (startIndex == m_index.end() )
		return m_emptyNode;
	
	SyntaxTreeIndexIterator2 endIndex = startIndex->second.find( endPos );
	if (endIndex == startIndex->second.end())
		return m_emptyNode;
	
	return endIndex->second;
}

// for printing out tree
std::string SyntaxTree::ToString() const
{
	std::stringstream out;
	out << *this;
	return out.str();
}

void SyntaxTree::AddDefaultNonTerms(size_t phraseSize)
{
	for (size_t startPos = 0; startPos < phraseSize; ++startPos)
	{
		for (size_t endPos = startPos; endPos < phraseSize; ++endPos)
		{
			AddNode(startPos, endPos, "X");
		}
	}
}

void SyntaxTree::AddDefaultNonTerms(bool isSyntax, bool mixed, size_t phraseSize)
{
	if (isSyntax)
	{
		AddDefaultNonTerms(!mixed);
	}
	else 
	{ // add X everywhere
		AddDefaultNonTerms(phraseSize);
	}
}

void SyntaxTree::AddDefaultNonTerms(bool addEverywhere)
{
	size_t phraseSize = GetNumWords();
	for (size_t startPos = 0; startPos < phraseSize; ++startPos)
	{
		for (size_t endPos = startPos; endPos < phraseSize; ++endPos)
		{
			const SyntaxNodes &nodes = GetNodes(startPos, endPos);
			if (!addEverywhere && nodes.size() > 0)
			{ // only add if no label
				continue;
			}
			AddNode(startPos, endPos, "X");
		}
	}
}

const SyntaxNodes SyntaxTree::GetNodesForLHS( int startPos, int endPos ) const
{
	SyntaxNodes ret(GetNodes(startPos, endPos));
	
	if (ret.size() == 0)
		ret.push_back(&m_defaultLHS);
	
	return ret;
}

std::ostream& operator<<(std::ostream& os, const SyntaxTree& t)
{
	int size = t.m_index.size();
	for(size_t length=1; length<=size; length++)
	{
		for(size_t space=0; space<length; space++)
		{
			os << "    ";
		}
		for(size_t start=0; start<=size-length; start++)
		{
			
			if (t.HasNode( start, start+(length-1) ))
			{
				std::string label = t.GetNodes( start, start+(length-1) )[0]->GetLabel() + "#######";
				
				os << label.substr(0,7) << " ";
			}
			else
			{
				os << "------- ";
			}		
		}
		os << std::endl;
	}
  return os;
}