2010-04-12 19:22:50 +04:00
|
|
|
// $Id: SyntaxTree.h 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
|
|
|
|
***********************************************************************/
|
|
|
|
|
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
#pragma once
|
2010-04-12 19:22:50 +04:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
|
|
|
#include <sstream>
|
|
|
|
|
2012-06-30 18:43:47 +04:00
|
|
|
namespace MosesTraining
|
|
|
|
{
|
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
class SyntaxNode
|
|
|
|
{
|
2010-04-12 19:22:50 +04:00
|
|
|
protected:
|
2011-02-24 16:57:11 +03:00
|
|
|
int m_start, m_end;
|
|
|
|
std::string m_label;
|
|
|
|
std::vector< SyntaxNode* > m_children;
|
|
|
|
SyntaxNode* m_parent;
|
2012-05-25 20:29:47 +04:00
|
|
|
float m_pcfgScore;
|
2010-04-12 19:22:50 +04:00
|
|
|
public:
|
2011-02-24 16:57:11 +03:00
|
|
|
SyntaxNode( int startPos, int endPos, std::string label )
|
|
|
|
:m_start(startPos)
|
|
|
|
,m_end(endPos)
|
|
|
|
,m_label(label)
|
2012-04-23 17:24:54 +04:00
|
|
|
,m_parent(0)
|
2013-06-10 21:11:55 +04:00
|
|
|
,m_pcfgScore(0.0f) {
|
|
|
|
}
|
2011-02-24 16:57:11 +03:00
|
|
|
int GetStart() const {
|
|
|
|
return m_start;
|
|
|
|
}
|
|
|
|
int GetEnd() const {
|
|
|
|
return m_end;
|
|
|
|
}
|
|
|
|
std::string GetLabel() const {
|
|
|
|
return m_label;
|
|
|
|
}
|
2012-05-25 20:29:47 +04:00
|
|
|
float GetPcfgScore() const {
|
|
|
|
return m_pcfgScore;
|
|
|
|
}
|
|
|
|
void SetPcfgScore(float score) {
|
|
|
|
m_pcfgScore = score;
|
|
|
|
}
|
2012-04-23 17:24:54 +04:00
|
|
|
SyntaxNode *GetParent() {
|
|
|
|
return m_parent;
|
|
|
|
}
|
|
|
|
void SetParent(SyntaxNode *parent) {
|
|
|
|
m_parent = parent;
|
|
|
|
}
|
|
|
|
void AddChild(SyntaxNode* child) {
|
|
|
|
m_children.push_back(child);
|
|
|
|
}
|
|
|
|
const std::vector< SyntaxNode* > &GetChildren() const {
|
|
|
|
return m_children;
|
|
|
|
}
|
2010-04-12 19:22:50 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
typedef std::vector< int > SplitPoints;
|
|
|
|
typedef std::vector< SplitPoints > ParentNodes;
|
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
class SyntaxTree
|
|
|
|
{
|
2010-04-12 19:22:50 +04:00
|
|
|
protected:
|
2011-02-24 16:57:11 +03:00
|
|
|
std::vector< SyntaxNode* > m_nodes;
|
|
|
|
SyntaxNode* m_top;
|
2010-04-12 19:22:50 +04:00
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
typedef std::map< int, std::vector< SyntaxNode* > > SyntaxTreeIndex2;
|
|
|
|
typedef SyntaxTreeIndex2::const_iterator SyntaxTreeIndexIterator2;
|
|
|
|
typedef std::map< int, SyntaxTreeIndex2 > SyntaxTreeIndex;
|
|
|
|
typedef SyntaxTreeIndex::const_iterator SyntaxTreeIndexIterator;
|
|
|
|
SyntaxTreeIndex m_index;
|
2013-04-25 19:27:50 +04:00
|
|
|
int m_size;
|
2011-02-24 16:57:11 +03:00
|
|
|
std::vector< SyntaxNode* > m_emptyNode;
|
2010-04-12 19:22:50 +04:00
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
friend std::ostream& operator<<(std::ostream&, const SyntaxTree&);
|
2010-04-12 19:22:50 +04:00
|
|
|
|
|
|
|
public:
|
2014-12-07 15:56:41 +03:00
|
|
|
SyntaxTree()
|
2015-01-14 14:07:42 +03:00
|
|
|
: m_top(0) // m_top doesn't get set unless ConnectNodes is called.
|
|
|
|
, m_size(0) {}
|
2014-12-07 15:56:41 +03:00
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
~SyntaxTree();
|
|
|
|
|
2012-05-25 20:29:47 +04:00
|
|
|
SyntaxNode *AddNode( int startPos, int endPos, std::string label );
|
|
|
|
|
2012-04-23 17:24:54 +04:00
|
|
|
SyntaxNode *GetTop() {
|
|
|
|
return m_top;
|
|
|
|
}
|
|
|
|
|
2011-02-24 16:57:11 +03:00
|
|
|
ParentNodes Parse();
|
|
|
|
bool HasNode( int startPos, int endPos ) const;
|
|
|
|
const std::vector< SyntaxNode* >& GetNodes( int startPos, int endPos ) const;
|
|
|
|
const std::vector< SyntaxNode* >& GetAllNodes() {
|
|
|
|
return m_nodes;
|
|
|
|
};
|
|
|
|
size_t GetNumWords() const {
|
2013-04-25 19:27:50 +04:00
|
|
|
return m_size;
|
2011-02-24 16:57:11 +03:00
|
|
|
}
|
2012-04-23 17:24:54 +04:00
|
|
|
void ConnectNodes();
|
|
|
|
void Clear();
|
2011-02-24 16:57:11 +03:00
|
|
|
std::string ToString() const;
|
2010-04-12 19:22:50 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream&, const SyntaxTree&);
|
|
|
|
|
2012-06-30 18:43:47 +04:00
|
|
|
}
|
|
|
|
|