Frog
ckyparser.h
Go to the documentation of this file.
1 /* ex: set tabstop=8 expandtab: */
2 /*
3  Copyright (c) 2006 - 2020
4  CLST - Radboud University
5  ILK - Tilburg University
6 
7  This file is part of frog:
8 
9  A Tagger-Lemmatizer-Morphological-Analyzer-Dependency-Parser for
10  several languages
11 
12  frog is free software; you can redistribute it and/or modify
13  it under the terms of the GNU General Public License as published by
14  the Free Software Foundation; either version 3 of the License, or
15  (at your option) any later version.
16 
17  frog is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  GNU General Public License for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with this program. If not, see <http://www.gnu.org/licenses/>.
24 
25  For questions and suggestions, see:
26  https://github.com/LanguageMachines/frog/issues
27  or send mail to:
28  lamasoftware (at ) science.ru.nl
29 
30 */
31 #ifndef CKYPARSER_H
32 #define CKYPARSER_H
33 
34 #include <set>
35 #include <vector>
36 #include <ostream>
37 #include "ticcutils/LogStream.h"
38 
39 enum dirType { ROOT, LEFT, RIGHT, ERROR };
40 
41 class Constraint {
42  public:
44  Constraint( double w, int i ): weight(w),tokenIndex(i){
45  };
46  virtual ~Constraint(){};
47  virtual void put( std::ostream& os ) const {
48  os << tokenIndex << " " << weight;
49  };
50  virtual ConstraintType type() const = 0;
51  virtual dirType direct() const { return ERROR; };
52  virtual std::string rel() const { return "NOREL"; };
53  virtual int hIndex() const { abort(); };
54  int tIndex() const { return tokenIndex; };
55  double wght() const { return weight; };
56  protected:
57  double weight;
59 };
60 
61 std::ostream& operator<<( std::ostream& os, const Constraint& c );
62 std::ostream& operator<<( std::ostream& os, const Constraint* c );
63 
64 class HasIncomingRel: public Constraint {
65  public:
66  HasIncomingRel( int i, const std::string& r, double w ):
67  Constraint( w, i ), relType(r){
68  };
69  void put( std::ostream& ) const;
70  ConstraintType type() const { return Incoming; }
71  std::string rel() const { return relType; };
72  private:
73  std::string relType;
74 };
75 
76 class HasDependency: public Constraint {
77  public:
78  HasDependency( int i, int h, const std::string& r, double w ):
79  Constraint( w, i ), relType(r), headType(h) {
80  };
81  void put( std::ostream& ) const;
82  ConstraintType type() const { return Dependency; };
83  int hIndex() const { return headType; };
84  std::string rel() const { return relType; };
85  private:
86  std::string relType;
87  int headType;
88 };
89 
91  public:
92  DependencyDirection( int i, const std::string& d, double w ):
93  Constraint( w, i ), direction(toEnum(d)){
94  }
95  void put( std::ostream& ) const;
96  ConstraintType type() const { return Direction; };
97  dirType direct() const { return direction; };
98  private:
99  dirType toEnum( const std::string& s ){
100  if ( s == "ROOT" )
101  return ROOT;
102  else if ( s == "LEFT" )
103  return LEFT;
104  else if ( s == "RIGHT" )
105  return RIGHT;
106  else {
107  abort();
108  }
109  }
110  dirType direction;
111 };
112 
113 class SubTree {
114  public:
115  SubTree( double score, int r, const std::string& label ):
116  _score( score ), _r( r ), _edgeLabel( label ){
117  }
119  _score( 0.0 ), _r( -1 ), _edgeLabel( "" ){
120  }
121  std::set<const Constraint*> satisfiedConstraints;
122  double score() const { return _score; };
123  int r() const { return _r; };
124  std::string edgeLabel() const { return _edgeLabel; };
125  private:
126  double _score;
127  int _r;
128  std::string _edgeLabel;
129 };
130 
131 struct parsrel {
132  std::string deprel;
133  int head;
134 };
135 
136 class chart_rec {
137  public:
142 };
143 
144 class CKYParser {
145 public:
146  CKYParser( size_t, const std::vector<const Constraint*>&, TiCC::LogStream* );
147  ~CKYParser(){ delete ckyLog; };
148  void parse();
149  void leftIncomplete( int , int , std::vector<parsrel>& );
150  void rightIncomplete( int , int , std::vector<parsrel>& );
151  void leftComplete( int , int , std::vector<parsrel>& );
152  void rightComplete( int , int , std::vector<parsrel>& );
153 
154 private:
155  void addConstraint( const Constraint * );
156  std::string bestEdge( const SubTree& , const SubTree& , size_t , size_t,
157  std::set<const Constraint*>&, double& );
158  size_t numTokens;
159  std::vector< std::vector<const Constraint*>> inDepConstraints;
160  std::vector< std::vector<const Constraint*>> outDepConstraints;
161  std::vector< std::vector< std::vector<const Constraint*>>> edgeConstraints;
162  std::vector< std::vector<chart_rec>> chart;
163 
164  TiCC::LogStream *ckyLog;
165 };
166 
167 #endif
chart_rec::l_True
SubTree l_True
Definition: ckyparser.h:138
DependencyDirection::DependencyDirection
DependencyDirection(int i, const std::string &d, double w)
Definition: ckyparser.h:92
ROOT
@ ROOT
Definition: ckyparser.h:39
CKYParser
Definition: ckyparser.h:144
CKYParser::rightComplete
void rightComplete(int, int, std::vector< parsrel > &)
Definition: ckyparser.cxx:328
chart_rec::r_False
SubTree r_False
Definition: ckyparser.h:141
CKYParser::leftComplete
void leftComplete(int, int, std::vector< parsrel > &)
Definition: ckyparser.cxx:320
HasDependency::hIndex
int hIndex() const
Definition: ckyparser.h:83
SubTree::SubTree
SubTree(double score, int r, const std::string &label)
Definition: ckyparser.h:115
Constraint::hIndex
virtual int hIndex() const
Definition: ckyparser.h:53
chart_rec
Definition: ckyparser.h:136
Constraint::direct
virtual dirType direct() const
Definition: ckyparser.h:51
HasDependency::rel
std::string rel() const
Definition: ckyparser.h:84
Constraint::~Constraint
virtual ~Constraint()
Definition: ckyparser.h:46
CKYParser::leftIncomplete
void leftIncomplete(int, int, std::vector< parsrel > &)
Definition: ckyparser.cxx:297
CKYParser::~CKYParser
~CKYParser()
Definition: ckyparser.h:147
parsrel::deprel
std::string deprel
Definition: ckyparser.h:132
Constraint::Direction
@ Direction
Definition: ckyparser.h:43
Constraint::wght
double wght() const
Definition: ckyparser.h:55
DependencyDirection::type
ConstraintType type() const
Definition: ckyparser.h:96
HasDependency::HasDependency
HasDependency(int i, int h, const std::string &r, double w)
Definition: ckyparser.h:78
HasDependency::put
void put(std::ostream &) const
Definition: ckyparser.cxx:71
Constraint::type
virtual ConstraintType type() const =0
HasIncomingRel::HasIncomingRel
HasIncomingRel(int i, const std::string &r, double w)
Definition: ckyparser.h:66
HasIncomingRel::put
void put(std::ostream &) const
Definition: ckyparser.cxx:64
chart_rec::l_False
SubTree l_False
Definition: ckyparser.h:139
dirType
dirType
Definition: ckyparser.h:39
SubTree::satisfiedConstraints
std::set< const Constraint * > satisfiedConstraints
Definition: ckyparser.h:121
Constraint::ConstraintType
ConstraintType
Definition: ckyparser.h:43
Constraint::Dependency
@ Dependency
Definition: ckyparser.h:43
chart_rec::r_True
SubTree r_True
Definition: ckyparser.h:140
Constraint::weight
double weight
Definition: ckyparser.h:55
operator<<
std::ostream & operator<<(std::ostream &os, const Constraint &c)
Definition: ckyparser.cxx:57
SubTree::SubTree
SubTree()
Definition: ckyparser.h:118
Constraint::Constraint
Constraint(double w, int i)
Definition: ckyparser.h:44
LEFT
@ LEFT
Definition: ckyparser.h:39
SubTree
Definition: ckyparser.h:113
HasIncomingRel::type
ConstraintType type() const
Definition: ckyparser.h:70
Constraint::rel
virtual std::string rel() const
Definition: ckyparser.h:52
parsrel
Definition: ckyparser.h:131
SubTree::score
double score() const
Definition: ckyparser.h:122
HasIncomingRel::rel
std::string rel() const
Definition: ckyparser.h:71
SubTree::r
int r() const
Definition: ckyparser.h:123
DependencyDirection::put
void put(std::ostream &) const
Definition: ckyparser.cxx:77
parsrel::head
int head
Definition: ckyparser.h:133
HasIncomingRel
Definition: ckyparser.h:64
DependencyDirection
Definition: ckyparser.h:90
HasDependency
Definition: ckyparser.h:76
Constraint::tokenIndex
int tokenIndex
Definition: ckyparser.h:58
SubTree::edgeLabel
std::string edgeLabel() const
Definition: ckyparser.h:124
Constraint
Definition: ckyparser.h:41
DependencyDirection::direct
dirType direct() const
Definition: ckyparser.h:97
CKYParser::rightIncomplete
void rightIncomplete(int, int, std::vector< parsrel > &)
Definition: ckyparser.cxx:308
HasDependency::type
ConstraintType type() const
Definition: ckyparser.h:82
ERROR
@ ERROR
Definition: ckyparser.h:39
CKYParser::CKYParser
CKYParser(size_t, const std::vector< const Constraint * > &, TiCC::LogStream *)
Definition: ckyparser.cxx:84
CKYParser::parse
void parse()
Definition: ckyparser.cxx:205
RIGHT
@ RIGHT
Definition: ckyparser.h:39
Constraint::Incoming
@ Incoming
Definition: ckyparser.h:43
Constraint::tIndex
int tIndex() const
Definition: ckyparser.h:54
Constraint::put
virtual void put(std::ostream &os) const
Definition: ckyparser.h:47