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