Edinburgh Speech Tools  2.4-release
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends Pages
EST_Relation.h
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 
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 
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,
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*/
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 *first_leaf() const;
136  EST_Item *last() const { return tail(); }
137  EST_Item *last_leaf() const;
138 
139  /** Return the tail (last) item of the relation */
140 // EST_Item *id(int i);
141 
142  /** number of items in this relation */
143  int length() const;
144 // int num_nodes() const;
145 // int num_leafs() const;
146  /** return true if relation does not contain any items */
147  int empty() const { return p_head == 0; }
148 
149  /** remove EST_Item <parameter>item</parameter> from relation */
150  void remove_item(EST_Item *item);
151 
152  /** remove all occurrences of feature
153  <parameter>name</parameter> from relation's items
154  */
155  void remove_item_feature(const EST_String &name);
156 
157  /** Load relation from file */
158  EST_read_status load(const EST_String &filename,
159  const EST_String &type="esps");
160 
161  /** Load relation from already open tokenstream */
162 // EST_read_status load(EST_TokenStream &ts,
163 // const EST_THash<int,EST_Val> &contents);
164 
165  /** Load relation from already open tokenstream */
166  EST_read_status load(EST_TokenStream &ts,
167  const EST_TVector < EST_Item_Content * > &contents
168  );
169 
170  /** Load relation from already open tokenstream */
171  EST_read_status load(const EST_String &filename,
172  EST_TokenStream &ts,
173  const EST_String &type);
174 
175  /** Save relation to file */
176  EST_write_status save(const EST_String &filename,
177  bool evaluate_ff = false) const;
178 
179  /** Save relation to file, evaluating all feature functions before hand */
180  EST_write_status save(const EST_String &filename,
181  const EST_String &type,
182  bool evaluate_ff = false) const;
183 
184  /** Save relation from already open ostream */
185  EST_write_status save(ostream &outf,EST_TKVL<void *,int> contents) const;
186 
187  /** Save relation from already open ostream */
188  EST_write_status save(ostream &outf,
189  const EST_String &type,
190  bool evaluate_ff) const;
191  /** Iteration */
193 
194  EST_Relation &operator=(const EST_Relation &s);
195  friend ostream& operator << (ostream &s, const EST_Relation &u);
196 
197  EST_Item *append(EST_Item *si);
198  EST_Item *append();
199  EST_Item *prepend(EST_Item *si);
200  EST_Item *prepend();
201 
202  friend class EST_Item;
203 };
204 
205 VAL_REGISTER_CLASS_DCLS(relation,EST_Relation)
206 
207 inline bool operator==(const EST_Relation &a, const EST_Relation &b)
208  { return (&a == &b); }
209 
210 void copy_relation(const EST_Relation &from, EST_Relation &to);
211 
212 EST_Utterance *get_utt(EST_Item *s);
213 
215 {
216 private:
217  const EST_Relation &rel;
218  EST_Item *next;
219 
220 public:
222  : rel(r), next(NULL) { reset();};
223 
224  void reset()
225  { next=rel.head(); }
226  bool has_more_elements()
227  { return next != NULL; }
228 
229  EST_Item *next_element();
230 };
231 
233 
234 #endif