Edinburgh Speech Tools  2.1-release
EST_SCFG_Chart.h
Go to the documentation of this file.
1 /*************************************************************************/
2 /* */
3 /* Centre for Speech Technology Research */
4 /* University of Edinburgh, UK */
5 /* Copyright (c) 1997 */
6 /* All Rights Reserved. */
7 /* */
8 /* Permission is hereby granted, free of charge, to use and distribute */
9 /* this software and its documentation without restriction, including */
10 /* without limitation the rights to use, copy, modify, merge, publish, */
11 /* distribute, sublicense, and/or sell copies of this work, and to */
12 /* permit persons to whom this work is furnished to do so, subject to */
13 /* the following conditions: */
14 /* 1. The code must retain the above copyright notice, this list of */
15 /* conditions and the following disclaimer. */
16 /* 2. Any modifications must be clearly marked as such. */
17 /* 3. Original authors' names are not deleted. */
18 /* 4. The authors' names are not used to endorse or promote products */
19 /* derived from this software without specific prior written */
20 /* permission. */
21 /* */
22 /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
23 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25 /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
26 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30 /* THIS SOFTWARE. */
31 /* */
32 /*************************************************************************/
33 /* Author : Alan W Black */
34 /* Date : June 1997 */
35 /*-----------------------------------------------------------------------*/
36 /* */
37 /* A SCFG chart parser, general functions */
38 /* */
39 /*=======================================================================*/
40 #ifndef __EST_SCFG_CHART_H__
41 #define __EST_SCFG_CHART_H__
42 
43 #include "EST_String.h"
44 #include "EST_simplestats.h"
45 #include "EST_string_aux.h"
46 #include "EST_SCFG.h"
48 
50 
51 /** \class EST_SCFG_Chart_Edge
52  \brief An internal class for \ref EST_SCFG_Chart for representing edges
53  in the chart during parsing with SCFGs.
54 
55  A standard Earley type chart edge, with representations for two
56  daughters and a position or what has been recognised. A probability
57  is also included.
58 */
60  private:
61  int p_d1;
62  int p_d2;
63  int p_pos;
64  double p_prob;
65  public:
66  /**@name Constructor and initialisation functions */
67  ///@{
69  EST_SCFG_Chart_Edge(double prob, int d1, int d2, int pos);
71  ///@}
72 
73  /// Postion, 0 1 or 2, where 0 is empty, 1 is incomplete 2 is complete.
74  int pos(void) { return p_pos; }
75  /// Edge probability
76  double prob(void) { return p_prob; }
77  /// (Non)terminal of daughter 1
78  int d1() { return p_d1; }
79  /// (Non)terminal of daughter 2
80  int d2() { return p_d2; }
81 
82 };
83 
84 /** \class EST_SCFG_Chart
85  \brief A class for parsing with a probabilistic grammars.
86 
87  The chart (sort of closer to CKY table) consists of indexes of
88  edges indexed by vertex number of mother non-terminal.
89 
90  The initial values (well-formed substring table) are taken from
91  an \ref EST_String with a given feature. The grammar may be
92  specified as LISP rules or as an already constructed \ref EST_SCFG.
93 
94  This produces a single best parse. It treats the grammar as
95  strictly context free in that the probability of a nonterminal
96  over vertex `n` to `m`, is the sum of all the possible analyses
97  of that sub-tree. Only the best analysis is kept for the
98  resulting parse tree.
99 
100  @author Alan W Black (awb@cstr.ed.ac.uk): October 1997
101 */
103  private:
104  /// pointer to grammar
105  EST_SCFG *grammar;
106  /// TRUE is grammar was created internally, FALSE is can't be freed
107  int grammar_local;
108  /// Number of vertices (number of words + 1)
109  int n_vertices;
110  /// Index of edges by vertex start x vertex end x nonterminal
111  EST_SCFG_Chart_Edge ****edges;
112  /// Index of basic symbols indexed by (start) vertex.
113  EST_SCFG_Chart_Edge **wfst;
114  /// An empty edge, denotes 0 probability edge.
115  EST_SCFG_Chart_Edge *emptyedge;
116 
117  // Find the best analysis of nonterminal `p` from `start`
118  // to `end`. Used after parsing
119  double find_best_tree(int start,int end,int p)
120  { EST_SCFG_Chart_Edge *r;
121  if ((r=edges[start][end][p]) != 0) return r->prob();
122  else return find_best_tree_cal(start,end,p); }
123  // Calculate best tree/probability
124  double find_best_tree_cal(int start,int end,int p);
125  void setup_edge_table();
126  void delete_edge_table();
127  LISP print_edge(int start, int end, int name, EST_SCFG_Chart_Edge *e);
128  // Extract edge from chart and add it to stream
129  void extract_edge(int start, int end, int p,
131  EST_Item *s,
132  EST_Item **word);
133  // Build parse from distinguished symbol alone
134  void extract_forced_parse(int start, int end, EST_Item *s, EST_Item *w);
135  public:
136  /**@name Constructor and initialisation functions */
137  ///@{
138  EST_SCFG_Chart();
139  ~EST_SCFG_Chart();
140  ///@}
141 
142  /**@name Grammar and parse string initialisation functions */
143  ///@{
144  /// Initialize from LISP rules set
145  void set_grammar_rules(LISP r);
146  /// Initialize from existing \ref EST_SCFG grammar
147  void set_grammar_rules(EST_SCFG &grammar);
148  /** Initialize for parsing from relation using `name` feature
149  setting up the "Well Formed Substring Table" */
150  void setup_wfst(EST_Relation *s,const EST_String &name="name");
151  /** Initialize for parsing from s to e using `name` feature
152  setting up the "Well Formed Substring Table" */
153  void setup_wfst(EST_Item *s, EST_Item *e,const EST_String &name="name");
154  ///@}
155 
156  /**@name parsing functions */
157  ///@{
158  /// Parses the loaded WFST with the loaded grammar.
159  void parse();
160  /// Return the parse in full LISP form.
161  LISP find_parse();
162  /// Extract parse tree and add it to syn linking leafs to word
163  void extract_parse(EST_Relation *syn,EST_Relation *word,int force=0);
164  /// Extract parse tree and add it to syn linking leafs to items s to e
165  void extract_parse(EST_Relation *syn,EST_Item *s,
166  EST_Item *e,int force=0);
167  ///@}
168 };
169 
170 /** Build a relation from a LISP list of items.
171 */
172 void EST_SCFG_chart_load_relation(EST_Relation *s,LISP sent);
173 
174 /** Parse a given string using the given grammar.
175 */
176 LISP scfg_parse(LISP string,LISP grammar);
177 /** Parse the given string using the given \ref EST_SCFG .
178 */
179 LISP scfg_parse(LISP string,EST_SCFG &grammar);
180 /** Parse named features in (list) relation Word into (tree)
181  ** relation Syntax
182  */
183 void scfg_parse(class EST_Relation *Word, const EST_String &name,
184  class EST_Relation *Syntax, EST_SCFG &grammar);
185 
186 #endif
float end(const EST_Item &item)
Definition: EST_item_aux.cc:96
void EST_SCFG_chart_load_relation(EST_Relation *s, LISP sent)
int d2()
(Non)terminal of daughter 2
int pos(void)
Postion, 0 1 or 2, where 0 is empty, 1 is incomplete 2 is complete.
int d1()
(Non)terminal of daughter 1
A class representing a stochastic context free grammar (SCFG).
Definition: EST_SCFG.h:182
float start(const EST_Item &item)
Definition: EST_item_aux.cc:52
double prob(void)
Edge probability.
A class for parsing with a probabilistic grammars.
LISP scfg_parse(LISP string, LISP grammar)
An internal class for EST_SCFG_Chart for representing edges in the chart during parsing with SCFGs...
Utility EST_String Functions header file.