Initial import to Gerrit.
[profile/ivi/festival.git] / src / modules / parser / pparser.cc
1 /*************************************************************************/
2 /*                                                                       */
3 /*                Centre for Speech Technology Research                  */
4 /*                     University of Edinburgh, UK                       */
5 /*                       Copyright (c) 1996,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 /*  Probabilistic parser for (S)CFG                                      */
38 /*                                                                       */
39 /*=======================================================================*/
40 #include <cmath>
41 #include "festival.h"
42 #include "parser.h"
43 #include "EST_SCFG_Chart.h"
44
45 LISP FT_PParse_Utt(LISP utt)
46 {
47     // Parse Words (using part of speech tags) using given 
48     // probabilistic grammar
49     EST_Utterance *u = get_c_utt(utt);
50     LISP rules;
51
52     rules = siod_get_lval("scfg_grammar", NULL);
53     if (rules == NULL)
54         return utt;
55
56     EST_SCFG grammar(rules);
57     
58     scfg_parse(u->relation("Word"),"phr_pos",
59                u->create_relation("Syntax"),grammar);
60
61     return utt;
62 }
63
64 LISP FT_MultiParse_Utt(LISP utt)
65 {
66     // You give them a parser and they just want more ... 
67     // Because in some modes utterance may contain multiple sentences
68     // and the grammars we have only have only deal in more
69     // traditional sentences this tries to split the utterance into
70     // sentences and parse them individualls and add them to 
71     // a single Syntax relation as a list of trees.
72     EST_Utterance *u = get_c_utt(utt);
73     LISP rules, eos_tree;
74     EST_Item *s,*e,*st,*et;
75
76     rules = siod_get_lval("scfg_grammar", NULL);
77     if (rules == NULL)
78         return utt;
79     eos_tree = siod_get_lval("scfg_eos_tree",NULL);
80     u->create_relation("Syntax");
81     EST_SCFG_Chart chart;
82     chart.set_grammar_rules(rules);
83
84     for (st=u->relation("Token")->head(); st; st = st->next())
85     {
86         for (et=st->next(); et; et=et->next())
87             if (wagon_predict(et,eos_tree) != 0)
88                 break;
89         // Now find related words
90         s = first_leaf(st)->as_relation("Word");
91         e = first_leaf(et->next())->as_relation("Word");
92         chart.setup_wfst(s,e,"phr_pos");
93         chart.parse();
94         chart.extract_parse(u->relation("Syntax"),s,e,TRUE);
95         st = et;
96     }
97
98     return utt;
99 }
100
101 void MultiParse(EST_Utterance &u)
102 {
103     // You give them a parser and they just want more ... 
104     // Because in some modes utterance may contain multiple sentences
105     // and the grammars we have only have only deal in more
106     // traditional sentences this tries to split the utterance into
107     // sentences and parse them individualls and add them to 
108     // a single Syntax release as a list of trees.
109     LISP rules, eos_tree;
110     EST_Item *s, *w;
111
112     rules = siod_get_lval("scfg_grammar", NULL);
113     if (rules == NULL)
114         EST_error("Couldn't find grammar rules\n");
115     eos_tree = siod_get_lval("scfg_eos_tree",NULL);
116     u.create_relation("Syntax");
117     EST_SCFG_Chart chart;
118     chart.set_grammar_rules(rules);
119
120     // produce a parse wherever there is a sentence end marker or
121     // the end of utterance.
122
123     for (w = s = u.relation("Word")->head(); w; w = w->next())
124         if (w->f_present("sentence_end") || (w->next() == 0))
125         {
126             chart.setup_wfst(s, w->next(), "phr_pos");
127             chart.parse();
128             chart.extract_parse(u.relation("Syntax"), s, w->next(), TRUE);
129             s = w->next();
130         }
131 }
132
133 void festival_parser_init(void)
134 {
135     proclaim_module("parser");
136
137     festival_def_utt_module("ProbParse",FT_PParse_Utt,
138     "(ProbParse UTT)\n\
139   Parse part of speech tags in Word relation.  Loads the grammar \n\
140   from scfg_grammar_filename and saves the best parse\n\
141   in the Syntax Relation.");
142     festival_def_utt_module("MultiProbParse",FT_MultiParse_Utt,
143     "(MultiProbParse UTT)\n\
144   Parse part of speech tags in Word relation.  Unlike ProbParse this \n\
145   allows multiple sentences to appear in the one utterance.  The CART \n\
146   tree in eos_tree is used to define end of sentence.  Loads the \n\
147   grammar from scfg_grammar_filename and saves the best parse\n\
148   in the Syntax Relation.");
149 }