Edinburgh Speech Tools  2.1-release
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 /** @class EST_Relation
55  @ingroup estlingclasses
56 
57 Relations are a container class for EST_Item. Three types of
58 relation structure are supported:
59 
60  - Linear lists
61  - Trees
62  - Multi-linear structures as used in autosegmental phonology etc
63 */
65 {
66  EST_String p_name;
67  EST_Utterance *p_utt;
68  EST_Item *p_head;
69  EST_Item *p_tail; // less meaningful in a tree
70 
71  EST_Item *get_item_from_name(EST_THash<int,EST_Val> &inames,int name);
72  EST_Item *get_item_from_name(EST_TVector< EST_Item * > &inames,int name);
73  EST_write_status save_items(EST_Item *item,
74  ostream &outf,
75  EST_TKVL<void *,int> &contentnames,
76  EST_TKVL<void *,int> &itemnames,
77  int &node_count) const;
78 
79  static void node_tidy_up_val(int &k, EST_Val &v);
80  static void node_tidy_up(int &k, EST_Item *node);
81 
82  EST_read_status load_items(EST_TokenStream &ts,
83  const EST_THash<int,EST_Val> &contents);
84  EST_read_status load_items(EST_TokenStream &ts,
86  );
87  void copy(const EST_Relation &r);
88  public:
89 
90  /** default constructor */
91  EST_Relation();
92  /** Constructor which sets name of relation */
93  EST_Relation(const EST_String &name);
94  /** Constructor which copies relation r */
95  EST_Relation(const EST_Relation &r) { copy(r); }
96  /** default destructor */
97  ~EST_Relation();
98 
99  /** Features which belong to the relation rather than its items*/
101 
102  /** Evaluate the relation's feature functions */
103  //void evaluate_features();
104  /** Evaluate the feature functions of all the items in the relation */
105  void evaluate_item_features();
106 
107  /** Clear the relation of items */
108  void clear();
109 
110  /** Return the <link linkend="est-utterance">EST_Utterance</link>
111  to which this relation belongs */
112  EST_Utterance *utt(void) { return p_utt; }
113 
114  /** Set the <link linkend="est-utterance">EST_Utterance</link>
115  to which this relation belongs */
116  void set_utt(EST_Utterance *u) { p_utt = u; }
117 
118  /** Return the name of the relation */
119  const EST_String &name() const { return (this == 0) ? EST_String::Empty : p_name; }
120 
121  /** Return the head (first) item of the relation */
122  EST_Item *head() const {return (this == 0) ? 0 : p_head;}
123 
124  /** Return the root item of the relation */
125  EST_Item *root() const {return head();}
126 
127  /** Return the tail (last) item of the relation */
128  EST_Item *tail() const {return (this == 0) ? 0 : p_tail;}
129 
130  // This have been replaced by Relation_Tree functions
131  EST_Item *first() const { return head(); }
132  EST_Item *first_leaf() const;
133  EST_Item *last() const { return tail(); }
134  EST_Item *last_leaf() const;
135 
136  /** Return the tail (last) item of the relation */
137 // EST_Item *id(int i);
138 
139  /** number of items in this relation */
140  int length() const;
141 // int num_nodes() const;
142 // int num_leafs() const;
143  /** return true if relation does not contain any items */
144  int empty() const { return p_head == 0; }
145 
146  /** remove EST_Item `item` from relation */
147  void remove_item(EST_Item *item);
148 
149  /** remove all occurrences of feature
150  `name` from relation's items
151  */
152  void remove_item_feature(const EST_String &name);
153 
154  /** Load relation from file */
155  EST_read_status load(const EST_String &filename,
156  const EST_String &type="esps");
157 
158  /** Load relation from already open tokenstream */
159 // EST_read_status load(EST_TokenStream &ts,
160 // const EST_THash<int,EST_Val> &contents);
161 
162  /** Load relation from already open tokenstream */
163  EST_read_status load(EST_TokenStream &ts,
164  const EST_TVector < EST_Item_Content * > &contents
165  );
166 
167  /** Load relation from already open tokenstream */
168  EST_read_status load(const EST_String &filename,
169  EST_TokenStream &ts,
170  const EST_String &type);
171 
172  /** Save relation to file */
173  EST_write_status save(const EST_String &filename,
174  bool evaluate_ff = false) const;
175 
176  /** Save relation to file, evaluating all feature functions before hand */
177  EST_write_status save(const EST_String &filename,
178  const EST_String &type,
179  bool evaluate_ff = false) const;
180 
181  /** Save relation from already open ostream */
182  EST_write_status save(ostream &outf,EST_TKVL<void *,int> contents) const;
183 
184  /** Save relation from already open ostream */
185  EST_write_status save(ostream &outf,
186  const EST_String &type,
187  bool evaluate_ff) const;
188  /** Iteration */
190 
191  EST_Relation &operator=(const EST_Relation &s);
192  friend ostream& operator << (ostream &s, const EST_Relation &u);
193 
194  EST_Item *append(EST_Item *si);
195  EST_Item *append();
196  EST_Item *prepend(EST_Item *si);
197  EST_Item *prepend();
198 
199  friend class EST_Item;
200 };
201 
202 VAL_REGISTER_CLASS_DCLS(relation,EST_Relation)
203 
204 inline bool operator==(const EST_Relation &a, const EST_Relation &b)
205  { return (&a == &b); }
206 
207 void copy_relation(const EST_Relation &from, EST_Relation &to);
208 
209 EST_Utterance *get_utt(EST_Item *s);
210 
212 {
213 private:
214  const EST_Relation &rel;
215  EST_Item *next;
216 
217 public:
219  : rel(r), next(NULL) { reset();};
220 
221  void reset()
222  { next=rel.head(); }
223  bool has_more_elements()
224  { return next != NULL; }
225 
226  EST_Item *next_element();
227 };
228 
230 
231 #endif
EST_Item * head() const
Definition: EST_Relation.h:122
void remove_item(EST_Item *item)
EST_Item * tail() const
Definition: EST_Relation.h:128
An open hash table. The number of buckets should be set to allow enough space that there are relative...
Definition: EST_THash.h:71
EST_Item * root() const
Definition: EST_Relation.h:125
int length() const
EST_Relation(const EST_Relation &r)
Definition: EST_Relation.h:95
EST_Relation_Iterator Iterator
Definition: EST_Relation.h:189
void set_utt(EST_Utterance *u)
Definition: EST_Relation.h:116
void evaluate_item_features()
const EST_String & name() const
Definition: EST_Relation.h:119
int empty() const
Definition: EST_Relation.h:144
Templated Key-Value list. Objects of type EST_TKVL contain lists which are accessed by a key of type ...
Definition: EST_TKVL.h:75
EST_Relation * relation(void) const
The relation of this particular item.
Definition: EST_Item.h:325
EST_Utterance * utt(void)
Definition: EST_Relation.h:112
EST_Features f
Definition: EST_Relation.h:100
Template vector.
Definition: EST_TVector.h:145
EST_write_status save(const EST_String &filename, bool evaluate_ff=false) const
void remove_item_feature(const EST_String &name)
EST_read_status load(const EST_String &filename, const EST_String &type="esps")
static const EST_String Empty
Constant empty string.
Definition: EST_String.h:116