"Fossies" - the Fresh Open Source Software Archive 
Member "speech_tools/include/ling_class/EST_Relation.h" (4 Sep 2017, 8649 Bytes) of package /linux/misc/speech_tools-2.5.0-release.tar.gz:
1 /*************************************************************************/
2 /* */
3 /* Centre for Speech Technology Research */
4 /* University of Edinburgh, UK */
5 /* Copyright (c) 1998 */
6 /* All Rights Reserved. */
7 /* Permission is hereby granted, free of charge, to use and distribute */
8 /* this software and its documentation without restriction, including */
9 /* without limitation the rights to use, copy, modify, merge, publish, */
10 /* distribute, sublicense, and/or sell copies of this work, and to */
11 /* permit persons to whom this work is furnished to do so, subject to */
12 /* the following conditions: */
13 /* 1. The code must retain the above copyright notice, this list of */
14 /* conditions and the following disclaimer. */
15 /* 2. Any modifications must be clearly marked as such. */
16 /* 3. Original authors' names are not deleted. */
17 /* 4. The authors' names are not used to endorse or promote products */
18 /* derived from this software without specific prior written */
19 /* permission. */
20 /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
21 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
22 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
23 /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
24 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
25 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
26 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
27 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
28 /* THIS SOFTWARE. */
29 /* */
30 /*************************************************************************/
31 /* Author : Alan W Black */
32 /* Date : February 1998 */
33 /* --------------------------------------------------------------------- */
34 /* another architecture */
35 /* */
36 /*************************************************************************/
37 #ifndef __EST_RELATION_H__
38 #define __EST_RELATION_H__
39
40 #include "EST_String.h"
41 #include "EST_TList.h"
42 #include "EST_TKVL.h"
43 #include "EST_THash.h"
44 #include "EST_Val.h"
45 #include "EST_types.h"
46 #include "EST_Token.h"
47 #include "EST_Features.h"
48 #include "ling_class/EST_Item.h"
49
50 class EST_Utterance;
51
52 class EST_Relation_Iterator;
53
54 /** Relations are a container class for EST_Items. Three types of
55 relation structure are supported:
56
57 <variablelist>
58
59 <varlistentry><term>Linear lists</term><listitem></listitem></varlistentry>
60 <varlistentry><term>Trees</term><listitem></listitem></varlistentry>
61 <varlistentry><term>Multi-linear structures</term><listitem> as used
62 in autosegmental phonology etc</listitem></varlistentry>
63
64 </variablelist>
65 */
66
67 class EST_Relation
68 {
69 EST_String p_name;
70 EST_Utterance *p_utt;
71 EST_Item *p_head;
72 EST_Item *p_tail; // less meaningful in a tree
73
74 EST_Item *get_item_from_name(EST_THash<int,EST_Val> &inames,int name);
75 EST_Item *get_item_from_name(EST_TVector< EST_Item * > &inames,int name);
76 EST_write_status save_items(EST_Item *item,
77 ostream &outf,
78 EST_TKVL<void *,int> &contentnames,
79 EST_TKVL<void *,int> &itemnames,
80 int &node_count) const;
81
82 static void node_tidy_up_val(int &k, EST_Val &v);
83 static void node_tidy_up(int &k, EST_Item *node);
84
85 EST_read_status load_items(EST_TokenStream &ts,
86 const EST_THash<int,EST_Val> &contents);
87 EST_read_status load_items(EST_TokenStream &ts,
88 const EST_TVector < EST_Item_Content * > &contents
89 );
90 void copy(const EST_Relation &r);
91 public:
92
93 /** default constructor */
94 EST_Relation();
95 /** Constructor which sets name of relation */
96 EST_Relation(const EST_String &name);
97 /** Constructor which copies relation r */
98 EST_Relation(const EST_Relation &r) { copy(r); }
99 /** default destructor */
100 ~EST_Relation();
101
102 /** Features which belong to the relation rather than its items*/
103 EST_Features f;
104
105 /** Evaluate the relation's feature functions */
106 //void evaluate_features();
107 /** Evaluate the feature functions of all the items in the relation */
108 void evaluate_item_features();
109
110 /** Clear the relation of items */
111 void clear();
112
113 /** Return the <link linkend="est-utterance">EST_Utterance</link>
114 to which this relation belongs */
115 EST_Utterance *utt(void) { return p_utt; }
116
117 /** Set the <link linkend="est-utterance">EST_Utterance</link>
118 to which this relation belongs */
119 void set_utt(EST_Utterance *u) { p_utt = u; }
120
121 /** Return the name of the relation */
122 const EST_String &name() const { return (this == 0) ? EST_String::Empty : p_name; }
123
124 /** Return the head (first) item of the relation */
125 EST_Item *head() const {return (this == 0) ? 0 : p_head;}
126
127 /** Return the root item of the relation */
128 EST_Item *root() const {return head();}
129
130 /** Return the tail (last) item of the relation */
131 EST_Item *tail() const {return (this == 0) ? 0 : p_tail;}
132
133 // This have been replaced by Relation_Tree functions
134 EST_Item *first() const { return head(); }
135 EST_Item *rlast() const { return tail(); }
136
137 /** Return the tail (last) item of the relation */
138 // EST_Item *id(int i);
139
140 /** number of items in this relation */
141 int length() const;
142 // int num_nodes() const;
143 // int num_leafs() const;
144 /** return true if relation does not contain any items */
145 int empty() const { return p_head == 0; }
146
147 /** remove EST_Item <parameter>item</parameter> from relation */
148 void remove_item(EST_Item *item);
149
150 /** remove all occurrences of feature
151 <parameter>name</parameter> from relation's items
152 */
153 void remove_item_feature(const EST_String &name);
154
155 /** Load relation from file */
156 EST_read_status load(const EST_String &filename,
157 const EST_String &type="esps");
158
159 /** Load relation from already open tokenstream */
160 // EST_read_status load(EST_TokenStream &ts,
161 // const EST_THash<int,EST_Val> &contents);
162
163 /** Load relation from already open tokenstream */
164 EST_read_status load(EST_TokenStream &ts,
165 const EST_TVector < EST_Item_Content * > &contents
166 );
167
168 /** Load relation from already open tokenstream */
169 EST_read_status load(const EST_String &filename,
170 EST_TokenStream &ts,
171 const EST_String &type);
172
173 /** Save relation to file */
174 EST_write_status save(const EST_String &filename,
175 bool evaluate_ff = false) const;
176
177 /** Save relation to file, evaluating all feature functions before hand */
178 EST_write_status save(const EST_String &filename,
179 const EST_String &type,
180 bool evaluate_ff = false) const;
181
182 /** Save relation from already open ostream */
183 EST_write_status save(ostream &outf,EST_TKVL<void *,int> contents) const;
184
185 /** Save relation from already open ostream */
186 EST_write_status save(ostream &outf,
187 const EST_String &type,
188 bool evaluate_ff) const;
189 /** Iteration */
190 typedef EST_Relation_Iterator Iterator;
191
192 EST_Relation &operator=(const EST_Relation &s);
193 friend ostream& operator << (ostream &s, const EST_Relation &u);
194
195 EST_Item *append(EST_Item *si);
196 EST_Item *append();
197 EST_Item *prepend(EST_Item *si);
198 EST_Item *prepend();
199
200 friend class EST_Item;
201 };
202
203 VAL_REGISTER_CLASS_DCLS(relation,EST_Relation)
204
205 inline bool operator==(const EST_Relation &a, const EST_Relation &b)
206 { return (&a == &b); }
207
208 void copy_relation(const EST_Relation &from, EST_Relation &to);
209
210 EST_Utterance *get_utt(EST_Item *s);
211
212 class EST_Relation_Iterator
213 {
214 private:
215 const EST_Relation &rel;
216 EST_Item *next;
217
218 public:
219 EST_Relation_Iterator(const EST_Relation &r)
220 : rel(r), next(NULL) { reset();};
221
222 void reset()
223 { next=rel.head(); }
224 bool has_more_elements()
225 { return next != NULL; }
226
227 EST_Item *next_element();
228 };
229
230 typedef EST_TList<EST_Relation> EST_RelationList;
231
232 #endif