Edinburgh Speech Tools  2.1-release
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
feature_example.cc
1 /*************************************************************************/
2 /* */
3 /* Centre for Speech Technology Research */
4 /* University of Edinburgh, UK */
5 /* Copyright (c) 1996 */
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 
34 #include "EST_unix.h"
35 #include "EST_ling_class.h"
36 
37 
38 /** @name Feature and Val Classes Example Code
39  */
40 //@{
41 
42 int main(void)
43 {
44 
45  /** @name Adding basic information to an EST_Item
46  *
47  * An item such as
48  * \f[
49 \left [
50 \begin{array}{ll}
51 \mbox{POS} & \mbox{\emph{Noun}} \\
52 \mbox{NAME} & \mbox{\emph{example}} \\
53 \mbox{FOCUS} & \mbox{+} \\
54 \end{array} \right ]
55 \f]
56  * is constructed as follows: (note that
57  * the attributes are in capitals by linguistic convention only:
58  * attribute names are case sensitive and can be upper or lower
59  * case).
60  */
61  //@{
62 
63  //@{ code
64  EST_Item p;
65 
66  p.set("POS", "Noun");
67  p.set("NAME", "example");
68  p.set("FOCUS", "+");
69  p.set("DURATION", 2.76);
70  p.set("STRESS", 2);
71 
72  //@} code
73 
74  /** The type of the values in features is a
75  * <classname>EST_Val</classname> class, which is a union which can
76  * store ints, floats, EST_Strings, void pointers, and
77  * <classname>EST_Features</classname>. The overloaded function
78  * facility of C++ means that the <function>set()</function> can be
79  * used for all of these.
80  */
81 
82  //@}
83 
84  /** @name Accessing basic information in an Item
85  *
86  * When accessing the features, the type must be
87  * specified. This is done most easily by using of a series of
88  * functions whose type is coded by a capital letter:
89  * </para>
90  * <formalpara><title><function>F()</function></title><para> return value as a
91  * float</para></formalpara>
92  * <formalpara><title><function>I()</function></title><para> return value as a
93  * integer</para></formalpara>
94  * <formalpara><title><function>S()</function></title><para> return value as a
95  * <formalpara><title><function>A()</function></title><para> return value as a
96  * EST_Features</para></formalpara>
97  * <para>
98  */
99 
100  //@{
101 
102  //@{ code
103  cout << "Part of speech for p is " << p.S("POS") << endl;
104  cout << "Duration for p is " << p.F("DURATION") << endl;
105  cout << "Stress value for p is " << p.I("STRESS") << endl;
106  //@} code
107 
108  /** </para>
109  * <SIDEBAR>
110  * <TITLE>Output</TITLE>
111  * <screen>
112  * "Noun"
113  * 2.75
114  * 1
115  * </screen>
116  * </SIDEBAR>
117  * <para>
118  * A optional default value can be given if a result is always desired
119  */
120 
121  //@{ code
122  cout << "Part of speech for p is "
123  << p.S("POS") << endl;
124  cout << "Syntactic Category for p is "
125  << p.S("CAT", "Noun") << endl; // noerror
126  //@} code
127 
128  //@}
129 
130  /** @name Nested feature structures in items
131  *
132  * Nested feature structures such as
133 \f[
134 \left [
135 \begin{array}{ll}
136 \mbox{NAME} & \mbox{\emph{d}} \\
137 \mbox{PLACE OF ARTICULATION \boxed{1} } &
138  \left [ \begin{array}{ll}
139  \mbox{CORONAL} & \mbox{\emph{+}} \\
140  \mbox{ANTERIOR} & \mbox{\emph{+}} \\
141  \end{array} \right ] \\
142 \mbox{VOICE} & \mbox{\emph{+}} \\
143 \mbox{CONTINUANT} & \mbox{\emph{--}} \\
144 \mbox{SONORANT} & \mbox{\emph{--}} \\
145 \end{array} \right ]
146 \f]
147  * can be created in a number of ways:
148  */
149  //@{
150 
151  //@{ code
152 
153  p.set("NAME", "d");
154  p.set("VOICE", "+");
155  p.set("CONTINUANT", "-");
156  p.set("SONORANT", "-");
157 
158  EST_Features f;
159  p.set("PLACE OF ARTICULATION", f); // copy in empty feature set here
160 
161  p.A("PLACE OF ARTICULATION").set("CORONAL", "+");
162  p.A("PLACE OF ARTICULATION").set("ANTERIOR", "+");
163  //@} code
164 
165  /** or by filling the values in an EST_Features object and
166  * copying it in:
167  */
168 
169  //@{ code
170  EST_Features f2;
171 
172  f2.set("CORONAL", "+");
173  f2.set("ANTERIOR", "+");
174 
175  p.set("PLACE OF ARTICULATION", f2);
176  //@} code
177 
178 
179  /** Nested features can be accessed by multiple calls to the
180  * accessing commands:
181  */
182 
183  //@{ code
184  cout << "Anterior value is: " << p.A("PLACE OF ARTICULATION").S("ANTERIOR");
185  cout << "Coronal value is: " << p.A("PLACE OF ARTICULATION").S("CORONAL");
186  //@} code
187 
188  /** The first command is <function>A()</function> because PLACE is a
189  * feature structure, and the second command is
190  * <function>S()</function> because it returns a string (the
191  * value or ANTRIOR or CORONAL). A shorthand is provided to
192  * extract the value in a single statement:
193  */
194 
195  //@{ code
196  cout << "Anterior value is: " << p.S("PLACE OF ARTICULATION.ANTERIOR");
197  cout << "Coronal value is: " << p.S("PLACE OF ARTICULATION.CORONAL");
198  //@} code
199 
200  /** Again, as the last value to be returned is a string
201  * <function>S()</function> must be used. This shorthand can also be used
202  * to set the features:
203  */
204 
205  //@{ code
206 
207  p.set("PLACE OF ARTICULATION.CORONAL", "+");
208  p.set("PLACE OF ARTICULATION.ANTERIOR", "+");
209  //@} code
210 
211  /** this is the easiest and most commonly used method. */
212 
213 
214  //@}
215 
216  /** @name Utility functions for items
217  *
218  * The presence of a attribute can be checked using
219  * <function>f_present()</function>, which returns true if the
220  * attribute is in the item:
221  */
222  //@{
223 
224  //@{ code
225  cout << "This is true: " << p.f_present("PLACE OF ARTICULATION");
226  cout << "This is false: " << p.f_present("MANNER");
227  //@} code
228 
229  /** A attribute can be removed by <function>f_remove</function>
230  */
231 
232  //@{ code
233  p.f_remove("PLACE OF ARTICULATION");
234  //@} code
235 
236  //@}
237 
238  exit(0);
239 
240 }
241 //@}
void set(const EST_String &name, int ival)
Definition: EST_Features.h:186
const EST_String S(const EST_String &path) const
Definition: EST_Features.h:158
void set(const EST_String &name, int ival)
Definition: EST_Item.h:180
const int I(const EST_String &name) const
Definition: EST_Item.h:155
EST_Features & A(const EST_String &name) const
Definition: EST_Item.h:164
const EST_String S(const EST_String &name) const
Definition: EST_Item.h:144
const float F(const EST_String &name) const
Definition: EST_Item.h:135
void f_remove(const EST_String &name)
Definition: EST_Item.h:223
int f_present(const EST_String &name) const
Definition: EST_Item.h:231