Initial import to Tizen
[profile/ivi/sphinxbase.git] / include / sphinxbase / jsgf.h
1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* ====================================================================
3  * Copyright (c) 2007 Carnegie Mellon University.  All rights
4  * reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer. 
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  *
18  * This work was supported in part by funding from the Defense Advanced 
19  * Research Projects Agency and the National Science Foundation of the 
20  * United States of America, and the CMU Sphinx Speech Consortium.
21  *
22  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 
23  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
24  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
26  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
28  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
32  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  * ====================================================================
35  *
36  */
37
38 #ifndef __JSGF_H__
39 #define __JSGF_H__
40
41 /**
42  * @file jsgf.h JSGF grammar compiler
43  *
44  * This file defines the data structures for parsing JSGF grammars
45  * into Sphinx finite-state grammars.
46  **/
47
48 #include <stdio.h>
49
50 /* Win32/WinCE DLL gunk */
51 #include <sphinxbase/sphinxbase_export.h>
52 #include <sphinxbase/hash_table.h>
53 #include <sphinxbase/fsg_model.h>
54 #include <sphinxbase/logmath.h>
55
56 #ifdef __cplusplus
57 extern "C" {
58 #endif
59 #if 0
60 /* Fool Emacs. */
61 }
62 #endif
63
64 typedef struct jsgf_s jsgf_t;
65 typedef struct jsgf_rule_s jsgf_rule_t;
66
67 /**
68  * Create a new JSGF grammar.
69  *
70  * @param parent optional parent grammar for this one (NULL, usually).
71  * @return new JSGF grammar object, or NULL on failure.
72  */
73 SPHINXBASE_EXPORT
74 jsgf_t *jsgf_grammar_new(jsgf_t *parent);
75
76 /**
77  * Parse a JSGF grammar from a file.
78  *
79  * @param filename the name of the file to parse.
80  * @param parent optional parent grammar for this one (NULL, usually).
81  * @return new JSGF grammar object, or NULL on failure.
82  */
83 SPHINXBASE_EXPORT
84 jsgf_t *jsgf_parse_file(const char *filename, jsgf_t *parent);
85
86 /**
87  * Free a JSGF grammar.
88  */
89 SPHINXBASE_EXPORT
90 void jsgf_grammar_free(jsgf_t *jsgf);
91
92 /**
93  * Iterator over rules in a grammar.
94  */
95 typedef hash_iter_t jsgf_rule_iter_t;
96
97 /**
98  * Get an iterator over all rules in a grammar.
99  */
100 SPHINXBASE_EXPORT
101 jsgf_rule_iter_t *jsgf_rule_iter(jsgf_t *grammar);
102
103 /**
104  * Advance an iterator to the next rule in the grammar.
105  */
106 #define jsgf_rule_iter_next(itor) hash_table_iter_next(itor)
107
108 /**
109  * Get the current rule in a rule iterator.
110  */
111 #define jsgf_rule_iter_rule(itor) ((jsgf_rule_t *)(itor)->ent->val)
112
113 /**
114  * Free a rule iterator (if the end hasn't been reached).
115  */
116 #define jsgf_rule_iter_free(itor) hash_table_iter_free(itor)
117
118 /**
119  * Get a rule by name from a grammar.
120  */
121 SPHINXBASE_EXPORT
122 jsgf_rule_t *jsgf_get_rule(jsgf_t *grammar, char const *name);
123
124 /**
125  * Get the rule name from a rule.
126  */
127 SPHINXBASE_EXPORT
128 char const *jsgf_rule_name(jsgf_rule_t *rule);
129
130 /**
131  * Test if a rule is public or not.
132  */
133 SPHINXBASE_EXPORT
134 int jsgf_rule_public(jsgf_rule_t *rule);
135
136 /**
137  * Build a Sphinx FSG object from a JSGF rule.
138  */
139 SPHINXBASE_EXPORT
140 fsg_model_t *jsgf_build_fsg(jsgf_t *grammar, jsgf_rule_t *rule,
141                             logmath_t *lmath, float32 lw);
142
143 /**
144  * Build a Sphinx FSG object from a JSGF rule.
145  *
146  * This differs from jsgf_build_fsg() in that it does not do closure
147  * on epsilon transitions or any other postprocessing.  For the time
148  * being this is necessary in order to write it to a file - the FSG
149  * code will be fixed soon.
150  */
151 SPHINXBASE_EXPORT
152 fsg_model_t *jsgf_build_fsg_raw(jsgf_t *grammar, jsgf_rule_t *rule,
153                                 logmath_t *lmath, float32 lw);
154
155 /**
156  * Convert a JSGF rule to Sphinx FSG text form.
157  *
158  * This does a direct conversion without doing transitive closure on
159  * null transitions and so forth.
160  */
161 SPHINXBASE_EXPORT
162 int jsgf_write_fsg(jsgf_t *grammar, jsgf_rule_t *rule, FILE *outfh);
163
164 #ifdef __cplusplus
165 }
166 #endif
167
168
169 #endif /* __JSGF_H__ */