File: EST_SCFG_Chart.h

package info (click to toggle)
speech-tools 1%3A2.5.0-11
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 9,988 kB
  • sloc: cpp: 67,350; ansic: 12,174; sh: 4,055; java: 3,748; makefile: 1,111; lisp: 711; perl: 396; awk: 85; xml: 9
file content (184 lines) | stat: -rw-r--r-- 8,017 bytes parent folder | download | duplicates (8)
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
/*************************************************************************/
/*                                                                       */
/*                Centre for Speech Technology Research                  */
/*                     University of Edinburgh, UK                       */
/*                         Copyright (c) 1997                            */
/*                        All Rights Reserved.                           */
/*                                                                       */
/*  Permission is hereby granted, free of charge, to use and distribute  */
/*  this software and its documentation without restriction, including   */
/*  without limitation the rights to use, copy, modify, merge, publish,  */
/*  distribute, sublicense, and/or sell copies of this work, and to      */
/*  permit persons to whom this work is furnished to do so, subject to   */
/*  the following conditions:                                            */
/*   1. The code must retain the above copyright notice, this list of    */
/*      conditions and the following disclaimer.                         */
/*   2. Any modifications must be clearly marked as such.                */
/*   3. Original authors' names are not deleted.                         */
/*   4. The authors' names are not used to endorse or promote products   */
/*      derived from this software without specific prior written        */
/*      permission.                                                      */
/*                                                                       */
/*  THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK        */
/*  DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING      */
/*  ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT   */
/*  SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE     */
/*  FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES    */
/*  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN   */
/*  AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,          */
/*  ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF       */
/*  THIS SOFTWARE.                                                       */
/*                                                                       */
/*************************************************************************/
/*             Author :  Alan W Black                                    */
/*             Date   :  June 1997                                       */
/*-----------------------------------------------------------------------*/
/*                                                                       */
/* A SCFG chart parser, general functions                                */
/*                                                                       */
/*=======================================================================*/
#ifndef __EST_SCFG_CHART_H__
#define __EST_SCFG_CHART_H__

#include "EST_String.h"
#include "EST_simplestats.h"
#include "EST_string_aux.h"
#include "EST_SCFG.h"
#include "ling_class/EST_Relation.h"

class EST_SCFG_Chart_Edge;

/** An internal class for \Ref{EST_SCFG_Chart} for representing edges
    in the chart during parsing with SCFGs.

    A standard Earley type chart edge, with representations for two
    daughters and a position or what has been recognised.  A probability
    is also included.
*/
class EST_SCFG_Chart_Edge {
  private:
    int p_d1;
    int p_d2;
    int p_pos;
    double p_prob;
  public:
    /**@name Constructor and initialisation functions */
    //@{
    EST_SCFG_Chart_Edge();
    EST_SCFG_Chart_Edge(double prob, int d1, int d2, int pos);
    ~EST_SCFG_Chart_Edge();
    //@}

    /// Postion, 0 1 or 2, where 0 is empty, 1 is incomplete 2 is complete.
    int pos(void) { return p_pos; }
    /// Edge probability
    double prob(void) { return p_prob; }
    /// (Non)terminal of daughter 1
    int d1() { return p_d1; }
    /// (Non)terminal of daughter 2
    int d2() { return p_d2; }

};

/** A class for parsing with a probabilistic grammars.

    The chart (sort of closer to CKY table) consists of indexes of
    edges indexed by vertex number of mother non-terminal.

    The initial values (well-formed substring table) are taken from
    an \Ref{EST_Stream} with a given feature.  The grammar may be
    specified as LISP rules or as an already constructed \Ref{EST_SCFG}.

    This produces a single best parse.  It treats the grammar as
    strictly context free in that the probability of a nonterminal
    over vertex n to m, is the sum of all the possible analyses
    of that sub-tree.  Only the best analysis is kept for the
    resulting parse tree.

    @author Alan W Black (awb@cstr.ed.ac.uk): October 1997
*/
class EST_SCFG_Chart {
  private:
    /// pointer to grammar
    EST_SCFG *grammar;
    /// TRUE is grammar was created internally, FALSE is can't be freed
    int grammar_local;
    /// Number of vertices (number of words + 1)
    int n_vertices;
    /// Index of edges by vertex start x vertex end x nonterminal
    EST_SCFG_Chart_Edge ****edges;
    /// Index of basic symbols indexed by (start) vertex.
    EST_SCFG_Chart_Edge **wfst;
    /// An empty edge, denotes 0 probability edge.
    EST_SCFG_Chart_Edge *emptyedge;

    // Find the best analysis of nonterminal {\tt p} from {\tt start} 
    // to {\tt end}.  Used after parsing
    double find_best_tree(int start,int end,int p)
       { EST_SCFG_Chart_Edge *r;
	 if ((r=edges[start][end][p]) != 0) return r->prob();
	 else return find_best_tree_cal(start,end,p); }
    // Calculate best tree/probability
    double find_best_tree_cal(int start,int end,int p);
    void setup_edge_table();
    void delete_edge_table();
    LISP print_edge(int start, int end, int name, EST_SCFG_Chart_Edge *e);
    // Extract edge from chart and add it to stream
    void extract_edge(int start, int end, int p,
		      EST_SCFG_Chart_Edge *e,
		      EST_Item *s,
		      EST_Item **word);
    // Build parse from distinguished symbol alone
    void extract_forced_parse(int start, int end, EST_Item *s, EST_Item *w);
  public:
    /**@name Constructor and initialisation functions */
    //@{
    EST_SCFG_Chart();
    ~EST_SCFG_Chart();
    //@}

    /**@name Grammar and parse string initialisation functions */
    //@{
    /// Initialize from LISP rules set
    void set_grammar_rules(LISP r);
    /// Initialize from existing \Ref{EST_SCFG} grammar
    void set_grammar_rules(EST_SCFG &grammar);
    /** Initialize for parsing from relation using {\tt name} feature
        setting up the "Well Formed Substring Table" */
    void setup_wfst(EST_Relation *s,const EST_String &name="name");
    /** Initialize for parsing from s to e using {\tt name} feature
        setting up the "Well Formed Substring Table" */
    void setup_wfst(EST_Item *s, EST_Item *e,const EST_String &name="name");
    //@}

    /**@name parsing functions */
    //@{
    /// Parses the loaded WFST with the loaded grammar.
    void parse();
    /// Return the parse in full LISP form.
    LISP find_parse();
    /// Extract parse tree and add it to syn linking leafs to word
    void extract_parse(EST_Relation *syn,EST_Relation *word,int force=0);
    /// Extract parse tree and add it to syn linking leafs to items s to e
    void extract_parse(EST_Relation *syn,EST_Item *s, 
		       EST_Item *e,int force=0);
    //@}
};

/** Build a relation from a LISP list of items.
*/
void EST_SCFG_chart_load_relation(EST_Relation *s,LISP sent);

/** Parse a given string using the given grammar.
*/
LISP scfg_parse(LISP string,LISP grammar);
/** Parse the given string using the given \Ref{EST_SCFG}.
*/
LISP scfg_parse(LISP string,EST_SCFG &grammar);
/** Parse named features in (list) relation Word into (tree)
 ** relation Syntax
 */
void scfg_parse(class EST_Relation *Word, const EST_String &name, 
		class EST_Relation *Syntax, EST_SCFG &grammar);

#endif